News Archive
PhpRiot Newsletter
Your Email Address:

More information

Lightning-fast WordPress with PHP-FPM and nginx

Note: This article was originally published at Planet PHP on 19 November 2010.
Planet PHP

Managed servers are slow. They run old versions of PHP on ancient copies of Apache, and loathe the Digg effect (or any similar sudden influx of traffic). In this tutorial, I'll show how to build a server capable of withstanding a front-page Digg placement, step by step. This will mean your business stays online when it's most important-when everyone is looking.

We'll go through the process of building a super-fast, bulletproof custom web server for WordPress. The technology stack we'll use is Ubuntu, nginx, PHP-FPM, and MySQL. In a future article we'll look at adding memcached to the mix to take performance even further.

Why VPS?

VPS stands for virtual private server. Basically, you receive a piece of a big, expensive machine for a low monthly price. You page[KS; this right?] for a guaranteed amount of RAM, and have access to a certain amount of CPU power.

This can be a much better deal than managed hosting once you have a few websites up and running. However, you have to manage everything yourself, and take care of your server when something goes wrong.

Why nginx?

Nginx is a small, lightweight web server and reverse proxy. It runs on 5.2% of the top one million web servers. In particular, nginx is well-suited to a high traffic site. Its lightweight nature, compared to Apache, means an nginx server can run in a much smaller memory footprint. This makes it the web server of choice for people looking to squeeze the most performance out of a VPS solution.


Included with version 5.3.3 of PHP (released in July of this year) is a new FastCGI manager called PHP-FPM. PHP-FPM is a daemon that spawns processes to manage your online applications. So, rather than have your web server running plugins to display and process your PHP code, your PHP code is now run natively, by PHP-FPM.

For our example WordPress installation, we'll set up an nginx server to serve our static files. When a user requests a PHP page, the nginx server will forward the request to PHP itself. PHP-FPM runs its own server, waiting for users to request their pages.

This separation of features means you can see some incredible speed gains.

Why MySQL?

Basically, because there's no other choice. At the moment, WordPress only supports MySQL. I always use memcached to lighten the load on MySQL. As I mentioned, I'll be covering the use of memcached in a future post.

Putting It All Together

Since all this software is relatively cutting edge, we're going to go ahead and build nearly everything from source. This means you'll need to have build-essential or an equivalent package installed on your system. I'll assume you have basic familiarity with Linux and SSH.

If you're stuck developing in Windows and want to give this setup a go, I'd recommend installing an Ubuntu server inside the free VirtualBox virtualization application.

Step One: Installing nginx

I recommend downloading and installing nginx from source, as the version in most Linux distributions' package managers are older than we'd like. Nginx is actively developed, and we might as well take advantage of the developers' hard work.

We'll start by getting the dependencies; then we'll grab nginx and build it (check the downloads page for the latest version available):

sudo apt-get install libpcre3 libpcre3-dev libpcrecpp0 libssl-dev zlib1g-dev cd ~/downloads wget tar zxvf nginx-0.8.53.tar.gz cd nginx-0.8.53

Now, before we run through configure, we need to set a few preferences. Specifically, where nginx should be installed to. We'll also set up nginx to use the SSL module, so https works for our server:

./configure --pid-path=/var/run/ --sbin-path=/usr/local/sbin --with-http_ssl_module

Finally, we'll do a make, and a make install:

make sudo make install

We'll make a few quick edits to the nginx configuration file, which is located at /usr/local/nginx/conf/nginx.conf. At the top of that file you'll see these two lines:

# user nobody; worker_processes 1;

Uncomment the user line and change nobody to www-data www-data, then change worker_processes to 2 instead of 1. Have a look at the rest of the file; there are a number of settings for logs and other options, a sample server declaration, and a few commented-out examples. We'll be coming back to this file later, but for now you can save it and close it.

nginx doesn't come with an init script that we can run when the

Truncated by Planet PHP, read more at the original (another 8892 bytes)