Setup WordPress on Nginx with PHP-FPM

If you've followed my guide on setting up Nginx, Ruby, PHP on Ubuntu you might be wondering what to do next. How do you setup a PHP based site? For this example I'll use the popular blogging software Wordpress. This guide assumes you're already familiar with DNS management and have a domain you wish to use pointed at your server.

As before I'll be making use of the editor command. See the top part of the last guide if you don't know what this is.

Download and extract WordPress

First things first. We need a copy of WordPress on our box. We could just FTP it but what kind of server admin would we be if we did that? In the code below don't forget to substitute wordpressblog.com for your own domain name!

cd /var/www/
mkdir wordpressblog.com
cd wordpressblog.com
wget http://wordpress.org/latest.tar.gz
tar -xzvf latest.tar.gz
mv wordpress/* .
chmod 755 wp-content
rm -r wordpress/ latest.tar.gz

After running the above we'll have created a new directory for WordPress to live in. We've also got the latest WordPress download and extracted.

MySQL Configuration

We're going to need a database for WordPress to connect to. We're going to issue a few commands to MySQL in order to do this. You could of course connect to your server and do this with a GUI client or install PHPMyAdmin on your box.

First login to mysql:

mysql -u root -p

Now execute the following:

create database wordpress;
grant all on wordpress.* to 'wordpress' identified by 'w0rdpr3ss';
exit

We've also set up a user called wordpress with the most secure password in the world! Of course you can setup your own user however you wish.

Nginx Config

If you've use Apache and are familiar with V-hosts (Virtual Hosts) this will be very familiar to you. If not we're going to create which will hold configuration settings for out site allowing our server to serve up multiple websites.

Let's edit this file:

sudo editor /etc/nginx/sites-available/wordpressblog.com

You'll want to populate it with something very similar to this:

server {
        server_name     wordpressblog.com;
        access_log      /var/log/nginx/wordpressblog.com.access.log;
        error_log       /var/log/nginx/wordpressblog.com.error.log;
        root            /var/www/wordpressblog.com;
        index           index.html index.php;

        location / {
                try_files       $uri $uri/ /index.php;
        }

        location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                include         fastcgi_params;
                fastcgi_pass    php;
        }
}

As before, don't forget to substitute wordpressblog.com for your own domain name. Okay we're nearly done just need to enable this site.

sudo nginx_ensite wordpressblog.com
sudo restart nginx

If you didn't install nginx_ensite & nginx_dissite you can run sudo ln -s /etc/nginx/sites-available/wordpressblog.com /etc/nginx/sites-enabled/wordpressblog.com instead.

We've just enabled our site and restarted the server so visiting your domain should reveal a lovely WordPress blog installation which should be very straightforward.

WordPress Nginx Compatibility Plugin

You may have thought that was the end but not so. There is one last small thing you'll want to do if you want to setup your permalinks in a search engine friendly manner. In your WordPress admin area search for a plugin called "Nginx Compatibility" and install the plugin (its by Vladimir Kolesnikov). You can now setup your permalinks just as you would be able to as if you were running on Apache2.

Finally

You should now have a spiffing WordPress blog just waiting for you to publicise drivel about your dreary life. Hurrah! As WordPress is quite a bloated monster you'll want to look into page caching. In WordPress this generally means using one of two plugins W3 Total Cache or WP Super Cache. W3 Total Cache has a few more functions so you might wish to try that first.

Good luck with your blogging!