nginx and WordPress

Now that you know your configuration works, you'll set up your WordPress configuration in a file called wp-config.php. This file is in the directory /usr/share/nginx/wordpress, thanks to opening the WordPress tarfile earlier.

A new WordPress installation doesn't have a configuration file; you must copy it from the wp-config-sample.php that comes with the system:

$ cp wp-config-sample.php wp-config.php

Once that's done, open the file, and look for three lines that define DB_NAME, DB_USER and DB_PASSWORD. Change all three values to reflect the database, user name and password you have created here; this is how WordPress will connect to your database.

Configuring nginx

Next, you'll need to configure the UNIX-level permissions. Every process runs as a certain user, and nginx is no exception. On Ubuntu machines, both nginx and Apache run as the "www-data" user and group. Using a specific user ID to run such programs allows you to ensure that the correct permissions are in place. However, it also means you need to be sure that the WordPress directory and files are owned by that user.

So, you can say:

$ cd /usr/share/nginx
$ sudo chown -Rv www-data:www-data wordpress

The -R option tells chown to work recursively. The -v option turns on "verbose" mode, meaning that you get additional feedback from the program as it works. I generally prefer to run programs with -v to give me more feedback.

Now you have to configure your nginx server. When you installed it, the main configuration file was placed in /etc/nginx/nginx.conf. However, modern nginx configurations also include one file for each server it is running in /etc/nginx/sites-enabled/, with the file /etc/nginx/sites-enabled/default describing the default site.

For the purposes of simplicity, I'm going to assume here that you have a single site, which means you'll be able to modify just the individual "default" file, rather than the overall config file.

As is usual in nginx, the configuration is broken into individual blocks. server blocks describe how the HTTP requests coming in should be handled; in this case, you want anything that arrives on port 80 for any hostname to be passed to PHP. The following server block does the trick:

server {
        listen 80 default_server;

        root /usr/share/nginx/wordpress;
        index index.php index.html index.htm;

        server_name localhost;

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

        error_page 404 /404.html;

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root /usr/share/nginx/html;

        location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;

Let's go through the above, so you can understand what's happening.

First, you indicate that this server will listen on port 80. Unless you have a good reason to choose a different port, this is probably a good idea. Note that if and when you want to use SSL, that goes on port 443 and also requires a separate server block. For purposes of simplicity, let's ignore that here.

You also use default_server to indicate that if someone provides a hostname that does go to your IP address, but that is unhandled by any other hostname, then this server should be used. If your system handles only one HTTP server, this directive should be set in your configuration. If your system has multiple virtual HTTP servers, you'll need to decide which should be the default.

The root directive describes the directory containing the files you'll want to serve. After opening the tarfile into /usr/share/nginx/wordpress, you tell nginx that the directory contains the PHP programs you want to execute. Actually, it's just one PHP program in index.php that does all the heavy lifting.

The index command indicates what files should be read, and in what order, if you don't provide a filename. Note: indicate that index.php should be tried first to give WordPress a chance to run things before static alternatives are attempted.

The server_name directive tells nginx which name(s) should be recognized by this server. If you're using default_server and have only one virtual host, this doesn't matter all that much. However, if you have multiple servers, giving a name is a good idea.