Published by mkuplens on Monday, September 30th, 2013 in Resources

As I mentioned in my previous post, I spent some time this weekend migrating this site to a LEMP stack running on a Digital Ocean VPS (referral link).

One of the optimisations for WordPress that I researched involved combining Nginx’s FastCGI caching with ngx_cache_purge, a neat module that allows for targeted cache purging. Among other things, this means the cache of WordPress’ homepage gets refreshed whenever I post new content.

rtCamp have published a great starting tutorial on this, fastcgi_cache with conditional purging, but its initial steps are dependent on installation with apt-get, which is not available on my VPS. If you’re in the same boat as me, before you get to the configuration steps, you’ll have to build and compile Nginx from scratch!

I assume you’re somewhat familiar with the command line. Step-by-step instructions are below, but to summarise, I downloaded the source into /tmp, compiled and installed the new binary, configured Nginx, and restarted the service.

This will be easier if you can su to root, but sudo works fine.

Step 1: prepare your tools

Since we will be compiling from source we need some developer tools. These are easily installed with:

sudo yum groupinstall "Development Tools"

One that’s in place, fetch some required libraries (Nginx won’t compile without these):

sudo yum install pcre-devel zlib-devel openssl-devel

See? It’s already easy!

Step 2: get your source

Get SSH’d into your server, then switch to the tmp directory:

cd /tmp

We need the Nginx and ngx_cache_purge source code, so let’s download those now:

wget http://nginx.org/download/nginx-1.5.5.tar.gz

(Stay current, make sure you get the latest download link)

And next the ngx_cache_purge module:

wget http://labs.frickle.com/files/ngx_cache_purge-2.1.tar.gz

(Latest)

Step 3: Inflate and compile

The files we downloaded are tarred, gzipped archives that we need to unpack before we do anything else.

sudo tar -xzf nginx-1.5.5.tar.gz
sudo tar -xzf ngx_cache_purge-2.1.tar.gz

Now, switch into the Nginx source directory:

cd nginx-1.5.5

Compiling and installing software from source involves three steps: configure, in which the source code is prepared with the various modules and options we want enabled; make, in which the binary is prepared; and make install, in which the software is installed to a previously configured destination.

So, first configure (we’re sticking with conventions, and adding our ngx_cache_purge module at the end. Be sure to update the path if you get a newer version):

sudo ./configure --sbin-path=/usr/sbin --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx.lock --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/body --http-proxy-temp-path=/var/lib/nginx/proxy --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --with-debug --with-http_stub_status_module --with-http_flv_module --with-http_ssl_module --with-http_dav_module --with-ipv6 --add-module=/tmp/ngx_cache_purge-2.1

After many screens full of text, you’ll find yourself back at a command prompt. When we run make install, the script will replace your existing Nginx binary, so we need to stop the server now.

sudo service nginx stop

And now make & install:

sudo make && make install

You’ll see even more code and text scroll by. Wait until you’re back at the command prompt – your new version of Nginx should be installed now. You can check by running the following:

nginx -V

You should see ngx_cache_purge-2.1 listed at the end.

Now you can go back to the rtCamp post and continue from “Install Nginx Helper Plugin”. If you’re running WPtouch, don’t miss my previous post, Caching WordPress + WPtouch Pro on Nginx!