Recommended Nginx Configuration for High Traffic WordPress on Small VPS (512 RAM)

A mere half-year ago, I was published two article to guide you setup perfect VPS on Digital Ocean and the guidelines to optimize LAMP (Linux, Apache, MySQL, PHP) web server on lowest VPS hardware plan. My VPS that’s optimized with Varnish, W3 Total Cache, PHP-APC was powered on since November 2013 until now without any reboot or reset. I estimate my server could easily handle 50K – 100K page views per day. At that point I would be close to breaking 95% memory usage (with swap file). In just an afternoon last week, my server was survival with 280K page views, without any downtime.

Blitz Test Result
Blitz Test Result

Current Digital Ocean VPS

  • 512MB / 1 CPU / 20GB SSD Disk / 1T Transfer
  • Ubuntu 12.04, Apache, MySQL, PHP, Varnish, PHP-APC, ZPanel
  • 5 WordPress websites, W3 Total Cache + 1 Photo Gallery
  • 40K page views daily
  • Real-time backup with cloud storage services

From some previous articles, I’ve told you that I was planned transfer my website to new lightweight server (use Arch Linux instead Ubuntu), from Apache to Nginx, no Hosting Control Panel like ZPanel, Ajenti, ServerPilot … And here is Recommended Nginx Configuration for WordPress on Small Digital Ocean VPS (512MB RAM). Here is next web server specification:

  • 512MB / 1 CPU / 20GB SSD Disk / 1T Transfer
  • Arch Linux, Nginx, MariaDB, PHP, Varnish
  • Disable unnecessary services like FTP, SMTP … to decrease system resources usage
  • 5 WordPress websites, W3 Total Cache + 1 Photo Gallery
  • 40K page views daily
  • synchronize with local and cloud storage services

NGINX and APACHE together or only NGINX?

I wonder what are the adventages and disadventages of using NGINX only instead of NGINX & APACHE together. Here’s a detailed set of answers:

  • Not as much as the overhead incurred from loading apache. Apache itself, with its plethora of modules and options, is a memory and CPU hog compared to nginx. Apache 2.4 is less so, but still more.
  • This is one of the most missed features of Apache, but also one of the most disk-intensive ones. Did you know that Apache needs to traverse all the directories up until webroot in order to read all the htaccess files for every request? The convenience is the pay-off. If you prefer convenience over a slight gain, keep to it. There are ways to improve Apache efficiency by coalescing all your rules into a single htaccess or through the use of Location blocks in the config.

Because we’re building and optimizing a small VPS with very basic hardware requirement. Why do you need Apache? Only Nginx is the answer.
[digitalocean]

Get Arch Linux up to date

Currently, the Digital Ocean is deprecating Arch Linux as one of the standard base distributions. That means the Arch Linux image won’t update anymore (maybe change in future) but you still create new droplet based on Arch Linux 2013.05.
To view current kernel version:

# pacman -Qi linux

If you are Arch Linux user, you can upgrade the current Kernel version (3.8.12) to latest is 3.14.4 by comment the #IgnorePkg = linux in /etc/pacman.conf then following the pacman and system required actions.
If you aren’t Arch Linux user and felt confused with a lot of information and required actions, let’s try a bash scripts.

I found a bash scripts to update arch linux kernel. Very simple and just run, wait and reboot.
# curl https://raw.githubusercontent.com/sharow/archlinux-digitalocean/master/updater | sh -

Why I choose Arch Linux as my web server OS

Don’t like Ubuntu, Arch Linux is lightweight Linux distribution. I wanted something that wasnt bloated to the gills and I wanted to define exactly what was installed on my server. This is not a “XYZ is better than ubuntu” post, so don’t make it becomes web server OS war :d. The most important reason is I’ve used Arch Linux for 6 years ago.

Enable SWAP

Because we’re running a VPS with very basic hardware requirement, it just has 512 MB RAM, so we must enable SWAP file to increase the system memory.
As root use fallocate to create a swap file the size of your choosing (M = Megabytes, G = Gigabytes) (dd can also be used but will take longer).

# fallocate -l 512M /swapfile

if you like to use dd

# dd if=/dev/zero of=/swapfile bs=1M count=512

Set the right permissions (a world-readable swap file is a huge local vulnerability) and format it as swap file type.

# chmod 600 /swapfile && mkswap /swapfile

Activate the swap file:

# swapon /swapfile

Don’t forget to make it active when boot in /etc/fstab

/swapfile none swap defaults 0 0

Install LEMP stack

The acronym stands for Linux, nginx (pronounced Engine x), MariaDB, and PHP.

# pacman -S nginx php-fpm mariadb

Once you have installed MariaDB, we should activate it with this command:

# /usr/bin/mysql_secure_installation

If you run this command for the firs time, just enter because the default password is blank. Follow the install wizard to setup your MariaDB (MySQL).
To manage MySQL databases, I’m using Adminer that is a full-featured database management tool written in PHP. Conversely to phpMyAdmin, it consist of a single file ready to deploy to the target server. Adminer is available for MySQL, PostgreSQL, SQLite, MS SQL, Oracle, SimpleDB, Elasticsearch and MongoDB.

Configuring PHP

A number of commonly used PHP extensions can also be found in the official repository:

# pacman -Ss php-

We’re building this server for WordPress, uncomment the lines:

extension=gd.so
extension=mysql.so
extension=mysqli.so

don’t forget to install php-gd package.

Enable PHP in nginx

Inside each server block serving a PHP web application should appear a location block similar to:

location / {
     index index.html index.htm index.php;
}

location ~ \.(php|html|htm)$ {
     try_files      $uri = 404;
     fastcgi_pass 127.0.0.1:9000; # (depending on your php-fpm socket configuration, it's faster than normal method)
     #fastcgi_pass   unix:/run/php-fpm/php-fpm.sock;
     fastcgi_index  index.php;
     include        fastcgi.conf;
}

By default, PHP-FPM service is not using TCP/IP, you need enable it first by locate to /etc/php/php-fpm.conf then change:

listen = /run/php-fpm/php-fpm.sock

to

127.0.0.1:9000

Start LEMP Web Server

To start LEMP web server and enable the server to automatic start after boot.

# systemctl start nginx && systemctl start mysqld && systemctl start php-fpm
# systemctl enable nginx && systemctl enable mysqld && systemctl enable php-fpm

Pages: 1 2

, ,

17 Comments

  1. I am using apache and nginx side by side. Nginx handle caching, and apache as web server. I do not have that many pageview, but using blitz.io it could handle (est) 61 Million hit / day. I am using 512 MB of RAM, 3 v Core plan from VPS Provider called Crissic. Great share, btw. I am new follower of your website :-D

  2. Hello,
    What are the locations of the files you change at the bottom of this tutorial?
    Cant seem to find the right files.

    Frank.

  3. Hello (again),
    Also i cant find part 2, or are you working on that?

    Frank

    • Yes, I’m rewriting it to make it more cleary and separated some parts of its content to small post of series: Build and Optimize Perfect VPS with lowest hardware (low RAM and CPU) on Digital Ocean.
      I’m finishing it soon ASAP in this month.

  4. Is part 2 of this guide every coming out? Installing the LEMP is the easy part, we need the configurations!

    • Yes, I’m finishing it and make it online soon. It’s still in draft because I changed some configuration since it wrote.

  5. Hi there! very nice article,

    I would like to know what should I do to add more wordpress sites into your configuration, it seems than this configuration it’s just for 1 site, but If I seen that you have more on your VPS, and I’m interested about how can I could do that.

    Thanks in advanced
    Best Regards

  6. Hello, I have a vps from digital ocean. I installed vesta cpanel with default configuration. Since today I had the 10$ droplet. But because the cpu was 100% all the time I resized to the 20$ droplet (2 cpu, 2 gb ram). But now both cpu are 100% (not all the time but 70% of time yes). 60% of cpu is used by mysqld. What can you suggest?

    • I forgot to mention that I have 50 – 90 visits daily and 150k – 200k pageviews / day.

    • In my opinion, the cause of high CPU usage is CPanel, it’s not design for low-end hardware server like Digital Ocean’s basic plans. I recommend you using ServerPilot for VPS management. It’s remote service so you don’t pay any server resource for it.
      Or you can using pure nginx + php + mysql like me ^^

  7. How it is best to configure browser caching with nginx when using serverpilot? We have a VPS set up on Vultr.


Add a Comment

Your email address will not be published. Required fields are marked *

Comment *
Name *
Email *
Website

This site uses Akismet to reduce spam. Learn how your comment data is processed.