HOWTO: Install WordPress on Nginx

A few years ago, I moved all of my sites from a cheap shared host to a shiny new VPS, and have never looked back. As long as you’re comfortable on the command line, running your own VPS gives you a great amount of flexibility since you have full root access on the server and can configure everything exactly how you want it. That said, it also means that if you want to maintain high performance, you have to keep your resource usage to a minimum.

Apache is a very well-establish web server that can handle just about any situation. Unfortunately, that flexibility comes at the cost of size and relatively high demands on server resources. Nginx (“engine x”) is a lightweight web server/reverse proxy that is very efficient and perfect for hosting WordPress. Read on to see how that can be done…

Step One: FastCGI

First off, Nginx does not provide FastCGI for you (FastCGI is what your web server uses to interact with WordPress’s PHP code), so you’ve got to have a way to spawn your own FastCGI processes. My preferred method is to use the spawn-fcgi program provided by the lighttpd project. You can use PHP’s built-in FastCGI manager php-cgi to do the same thing, but it’s not as straight-forward. Plus, if you learn how to use spawn-fcgi, you can easily adapt it for use with other non-PHP web applications that require FastCGI.

Install spawn-fcgi

To download and install spawn-fcgi, either use your native package manager (this would be the recommended approach), or run the following commands to install the package from source. All of the building happens in your current directory and the binary will be installed under /usr/local/bin/ with a man page under /usr/local/share/man/.

$ wget https://www.lighttpd.net/download/spawn-fcgi-1.6.3.tar.bz2
$ tar xvjf spawn-fcgi-1.6.3.tar.bz2
$ cd spawn-fcgi-1.6.3/
$ ./configure
$ make
$ sudo make install

NOTE: If you’re following the steps above verbatim, you will need to have root privileges in order to perform the final installation step…everything else should work fine as a normal user. To gain root privileges, the program sudo was used in the example above; you may or may not have access to sudo on your machine.

After spawn-fcgi has been installed, you can safely remove the build directory and original source file:

$ cd ..
$ rm -rf spawn-fcgi-1.6.3/
$ rm spawn-fcgi-1.6.3.tar.bz2

Run spawn-fcgi

This part will be fairly distribution-specific, but I’ll provide the basic command that you’ll need. What you want to do is find a way to run this command as part of your init scripts so the processes will be spawned automatically when you reboot your server.

/usr/local/bin/spawn-fcgi -a 127.0.0.1 -p 53217 -P /var/run/fastcgi-php.pid -- /usr/bin/php-cgi

For better security, you can also spawn the processes as a non-privileged user by specifying the user/group with the -u and -g flags respectively. For more information on all the available options, run spawn-fcgi -h or man spawn-fcgi on the command line.

If you’re interested in seeing a complete example init script that I wrote for use with Arch Linux, you can download it here: fastcgi-php

Step Two: Complete 83.33% of the Famous 5-Minute Install

Next you should download the WordPress files and extract them to their final location on your server. Simply follow steps 1-5 of the Famous 5-Minute Install (the 6th and final step requires that your web server be up and running properly, so we’ll do it later). This guide will assume that you extracted the WordPress core files here: /srv/http/nginx/example.com/

Step Three: Nginx Configuration

To get the web server up and running properly, the file you need to edit is called “nginx.conf” and is installed in different places depending on your Linux distribution. If you install Nginx from source, the default location is /usr/local/nginx/conf/nginx.conf, however yours may be somewhere else like /etc/nginx/conf/nginx.conf.

Once you find that file, open it with your favorite text editor and add a server declaration that looks something like this (I’ll cover what each part means after posting the code):

server {
    listen       192.0.32.10:80;                # your server's public IP address
    server_name  example.com;                   # your domain name
    root         /srv/http/nginx/example.com/;  # absolute path to your WordPress installation

    try_files $uri $uri/ /index.php;

    location ~ \.php$ {
        include        fastcgi_params;
        fastcgi_pass   localhost:53217;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    }
}

You will need to edit the configuration above using your own information. The first part is merely the server declaration where you define what your server’s publicly available IP address is, and what domain name that address is associated with. Next we add some default settings which point Nginx to the absolute path of the WordPress installation on your server.

After that, the try_files directive automatically checks for the existence of files in order, and returns the first file that is found. The key part here is that WordPress uses the “Front Controller” design pattern, meaning that any request for a file that does not exist on the server should be handled by the main index.php file. So that’s exactly what we do…if the requested URI exists on our local filesystem (images, stylesheets, etc.), then we’ll serve it up directly; if the requested URI does not exist on the filesystem, we forward that request on to index.php.

Last, we add a location block that tells Nginx to dynamically forward PHP requests to the FastCGI processes we spawned earlier. You just have to make sure that the port number matches what we did above. That’s it!

Step Four: Finishing Up

Everything should be good to go…all you need to do now is start your Nginx server process (another distribution specific command), then complete the 6th step of the Famous 5-Minute Install and you should have WordPress up and running on Nginx!

If any of this needs further clarification or you’re just having trouble, drop me a line and I’ll see what I can do to help…

— Aaron Bull Schaefer