Running WordPress On Nginx (LEMP) On Debian Squeeze/Ubuntu 11.04

This tutorial shows how you can install and run a WordPress blog on a Debian Squeeze or Ubuntu 11.04 system that has nginx installed instead of Apache (LEMP = Linux + nginx (pronounced “engine x”) + MySQL + PHP). In addition to that I will also show you how you can use the WordPress plugins WP Super Cache and W3 Total Cache with nginx. nginx is a HTTP server that uses much less resources than Apache and delivers pages a lot of faster, especially static files.

I do not issue any guarantee that this will work for you!

1 Preliminary Note

I want to install WordPress in a vhost called www.example.com/example.com here with the document root /var/www/www.example.com/web.

You should have a working LEMP installation, as shown in these tutorials:

Installing Nginx With PHP5 And MySQL Support On Debian Squeeze

Installing Nginx With PHP5 (And PHP-FPM) And MySQL Support On Ubuntu 11.04

A note for Ubuntu users:

Because we must run all the steps from this tutorial with root privileges, we can either prepend all commands in this tutorial with the string sudo, or we become root right now by typing

sudo su

2 Installing APC

APC is a free and open PHP opcode cacher for caching and optimizing PHP intermediate code. It’s similar to other PHP opcode cachers, such as eAccelerator and XCache. It is strongly recommended to have one of these installed to speed up your PHP page.

APC can be installed as follows:

apt-get install php-apc

If you use PHP-FPM as your FastCGI daemon (like in Installing Nginx With PHP5 (And PHP-FPM) And MySQL Support On Ubuntu 11.04), restart it as follows:

/etc/init.d/php5-fpm restart

If you use lighttpd’s spawn-fcgi program as your FastCGI daemon (like in Installing Nginx With PHP5 And MySQL Support On Debian Squeeze), we must kill the current spawn-fcgi process (running on port 9000) and create a new one. Run

It is recommended to make the document root and the WordPress files in it writable by the nginx daemon (otherwise WordPress and its plugins cannot write configuration files) which is running as user www-data and group www-data:

If you haven’t already created a MySQL database for WordPress (including a MySQL WordPress user), you can do that as follows (I name the database wordpress in this example, and the user is called wp_admin, and his password is wp_admin_password):

mysqladmin -u root -p create wordpress

mysql -u root -p

GRANT ALL PRIVILEGES ON wordpress.* TO ‘wp_admin’@’localhost’ IDENTIFIED BY ‘wp_admin_password’;
GRANT ALL PRIVILEGES ON wordpress.* TO ‘wp_admin’@’localhost.localdomain’ IDENTIFIED BY ‘wp_admin_password’;
FLUSH PRIVILEGES;

Now we can launch the web-based WordPress installer by going to http://www.example.com/wp-admin/install.php – fill in the required details and submit the page:

That’s it already:

You can now log in to the WordPress admin interface with the username and password you specified during installation:

The first thing you should change are the Permalink Settings (Settings > Permalinks). I recommend to use a permalink structure where no index.php appears (because otherwise you might get problems with caching plugins such as WP Super Cache or W3 Total Cache – nginx will always bypass the cache because it will pass on the request to the PHP interpreter because of the appearance of index.php in the URL). I have used the following structure in my test:

/%year%/%monthnum%/%day%/%postname%/

Mark Custom Structure and fill in the above string, then save the permalink settings:

(If you don’t plan to use any caching plugin, then you can use any of the other options as well, including those that contain index.php.)

You can now browse your blog by going to www.example.com (it already contains some test posts/pages), and you should find that your new permalinks work without the need to specify any rewrite rules in your nginx configuration! That is because of this cool little line in our vhost configuration /etc/nginx/sites-available/www.example.com.vhost:

try_files $uri $uri/ /index.php?$args;

If you don’t want to use any caching plugins, you are finished now! The next chapter deals with the WordPress cache plugins WP Super Cache and W3 Total Cache.

4 Caching With WP Super Cache Or W3 Total Cache

This chapter deals with configuring nginx with WP Super Cache or nginx with W3Total Cache. Please do not use both plugins at the same time (I’m not sure if it is possible to use both at the same time – I didn’t try because it doesn’t make sense) – opt for one plugin and then follow the appropriate chapter here.

4.1 WP Super Cache

After you have installed and enabled WP Super Cache from your WordPress admin interface, go to Settings > WP Super Cache and then to the Advanced tab.

Enable caching by checking Cache hits to this website for quick access and Use mod_rewrite to serve cache files – this option is very important as it allows us to completely bypass the PHP backend and directly serve cached pages from the file system cache which makes your site very fast, especially because nginx is lightning fast at serving static files. I will show you how to configure nginx to do the necessary rewrites in a moment.

The other settings are up to you, although I recommend the following settings:

Further down the page, make sure you have

wp-.*\.php
index\.php

in the Add here strings (not a filename) that forces a page not to be cached field:

That’s it for the WP Super Cache configuration. Now we must reconfigure our nginx vhost. I’ve come up with two solutions that both work equally well:

4.2 W3 Total Cache

The good thing about W3 Total Cache is that it comes with nginx support already!

It is important the W3 Total Cache has write permissions for the document root /var/www/www.example.com/web because it tries to create the file /var/www/www.example.com/web/nginx.conf automatically which contains the necessary rewrite rules for nginx. (That’s why we ran

chown -R www-data:www-data /var/www/www.example.com/web

in chapter 3.)

If you are sure that your permissions are ok so that W3 Total Cache can write to your document root, you can proceed. After you have installed and enable W3 Total Cache, go to Performance in the WordPress admin interface. You will see the following error message that you can ignore (I think this is a little bug in W3 Total Cache because it correctly detects nginx instead of Apache and tries to write an nginx.conf file, but still it displays this error message):

It appears Page Cache URL rewriting is not working. If using apache, verify that the server configuration allows .htaccess or if using nginx verify all configuration files are included in the configuration.

In fact, the Page Cache does work, I tested it.

Scroll down and activate the following caches: Page Cache, (don’t enable Minify)…