WordPress via Nginx

A few years ago I would never have recommended using Nginx with WordPress. But Nginx, the PHP-FPM server and WordPress have all matured and become positively stable over the last few years. Times change.

I’m not going to lecture here. People can go out and research the pros and cons of Nginx vs. Apache on their own.

Assuming you’ve decided to use Nginx (or someone else has made that decision for you, it turns out that getting Nginx to serve up WordPress is pretty easy. In fact most installations only need a single line added to the typical generic configuration. Note that mine was a very simple setup, a single subdirectory based blog. For more complex scenarios, checkout the Nginx Community: WordPress page.

Of course you’ll need to make sure you’ve got a php server like PHP-FPM up and running first. For a nice description of all the pieces, look here. While the example platform is RHEL 6.4, the nginx and php-fpm configurations are pretty generic. There’s a more detailed series of articles in the Rackspace Knowledge Center for an example Debian system.

Note that as a WordPress old-timer I usually create my database, database user and database permissions ahead of time.

This is the magic line to embed in your Nginx server block(s):

try_files $uri $uri/ /index.php?q=$uri&$args;

Here it is in context:

    server {
        listen              443 ssl;
        server_name         www.example.com;
        root                /var/www/html;
        ssl                 on;
        ssl_certificate     /etc/pki/tls/certs/example.crt;
        ssl_certificate_key /etc/pki/tls/private/example.key;
        ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers         HIGH:!aNULL:!MD5;
        error_log           /var/log/nginx/ssl_error.log;
        access_log          /var/log/nginx/ssl_access.log  main;
        include             /etc/nginx/default.d/*.conf;
        location / {
            autoindex on;
            try_files $uri $uri/ /index.php?q=$uri&$args;

NOTE: If you’re running a distribution in the Red Hat Family (Red Hat Enterprise, CentOS, Fedora), you’ll find that permissions on some system folders have been set with the assumption that the local web server is Apache. For example, the subfolders under /var/lib/php. For some applications to work correctly you will need to switch nginx for apache. So instead of root:apache being the owner of /var/lib/php/session, it will need to be root:nginx. I do not, however, recommend running nginx as the apache user, as that could result in unanticipated issues.

This entry was posted in System Administration on by .

About phil

My name is Phil Lembo. In my day job I’m an enterprise IT architect for a leading distribution and services company. The rest of my time I try to maintain a semi-normal family life in the suburbs of Raleigh, NC. E-mail me at philipATlembobrothersDOTcom. The opinions expressed here are entirely my own and not those of my employers, past, present or future (except where I quote others, who will need to accept responsibility for their own rants).