Now powered by WordPress 3, Nginx and PHP-FPM

Speeding up my blog

Performance issues aren’t just for high traffic sites. I’m lucky if I get 50 visitors a day to this site, but by using scaling techniques popular with the big boys, I figured I could increase page load speeds, (good for visitors and good for SEO). If I could achieve this and use less resources, perhaps I could even save some money on my hosting bills. I currently run a 512MB VPS on Slicehost, and I’d rather not increase this right now.

With a few days off work, I decided to take the plunge and swap out some of the server tech powering this blog. From the bottom up, so to speak, this was as follows –

  1. Replace Apache with Nginx (below)
  2. Upgrade to PHP 5.3.3 and run as a FastCGI (next post)
  3. Upgrade to WordPress 3
  4. Deploy a CDN
  5. Add a Varnish cache for extra speed

I’ll go through my experience across a number of posts, starting with Nginx. I shan’t replicate any existing documentation; I’m just going to go through what I did and point you at the resources you’ll need.

Nginx (Engine X)

Apache has long been my comfort zone, as I imagine it is for most PHP developers, but I’ve been listening to people over the past couple of years saying the old work horse is bloated, and needs shooting. It has certainly been falling out of favour with high-traffic sites, while Nginx seems to be gaining popularity. Before this upgrade I’d already tried Nginx out on a few other sites, and it’s great. Read about Nginx here. Here’s an Nginx-Apache comparison if you want some stats – I have none to offer you.

Installing nginx

I found I could install a fairly recent version on Fedora using yum, so # yum install nginx was all I needed to do. I found it was totally unavailable in other repos, including CentOS, so you may have to build from source. I won’t replicate any such tutorials here, except to say that you will most likely have to update libevent. You’ll need a recent version of that for the rest of this stack anyway, so may as well build that upfront.

Virtual hosts

As I have a couple of other small sites on this sever, I needed to set up virtual hosting. I did this with an include directive in the main /etc/nginx.conf, as follows:

http {
  # ....
  include /home/vhosts/*/conf/nginx.include;

So each of my host directories has a server {…} block defined in nginx.include. I actually removed the default server block from the main config, although that is entirely up to you. All nginx core directives listed here.

Starting nginx is as simple as running # nginx, and a graceful reload of altered configs can be done with # nginx -s reload. If you install via yum, you may also have a service script at /etc/init.d/nginx. To ensure nginx is up on reboot, I just chucked a start command into /etc/rc.local although, I’m sure there’s a better way to do that.


So, nginx is running, what to do about PHP? Waving goodbye to Apache also means waving goodbye to mod_php. If you’ve previously run PHP as a FastCGI, then you’re ahead of the game on this one. If not, then you’re about to leave another comfort zone. I’ll cover running PHP-FPM next »»

2 thoughts on “Now powered by WordPress 3, Nginx and PHP-FPM”

  1. I tried using PHP-FPM but kept getting “nginx 504 gateway timeout”.

    Did you get this error? How did you get around it? I ended up reverting back to Apache after 4 days of searching and trying solutions.

Comments are closed.