nginx and WordPress

In my last article, I took an initial look at nginx, the high-performance open-source HTTP that uses a single process and a single thread to service a large number of requests. nginx was designed for speed and scalability, as opposed to Apache, which was designed to maximize flexibility and configuration. But through the years, nginx has become increasingly flexible as well, with a growing number of plugins and modules that can be used to customize its configuration. Between the performance, increasingly good documentation and convenience, it's no wonder nginx has been increasingly popular.

It's also no surprise that WordPress, the open-source blogging and CMS platform, has become hugely popular. I've heard people say that 10% of websites are now run using WordPress. Even if that's not precisely true, there's no doubt that a huge number of sites are powered by WordPress. I'm a mostly satisfied WordPress user, having converted my main site and my two ebook sites to it in the past year after years of using it to power my blog.

So, I thought it would be interesting to demonstrate how easy it is to set up WordPress with nginx, given the popularity of each of these systems alone as well as together. In my last article, I described how you can set up a plain-vanilla PHP system with nginx; WordPress is a bit more complex, but less than you might think. Starting with a bare-bones Linux installation, let's walk through the configuration needed to get WordPress up and running.

The Basics

In order to install WordPress and nginx together, you're going to need three basic software systems installed: WordPress, nginx and MySQL. The first two are pretty obvious, given this article's goal; the third is a byproduct of using WordPress, which works exclusively with MySQL.

So, on my Ubuntu Linux machine, I would run the following:

$ sudo apt-get install mysql-server mysql-client nginx-core
 ↪php5-cli php5-fpm php5-mysql

This installs a very large number of packages, but it will give you the core of what you need to get your system up and running. Notice that you're not installing WordPress here, so that you can install it manually, using the source code. Indeed, installing WordPress via apt-get also means installing Apache; although it's certainly possible to undo this choice, the benefits of installing WordPress on your own outweigh those of doing it via a package manager.

You will, as part of this installation, need to choose a password for your MySQL root user. This is an important part of security on your system, so do try to use a strong password.

Once the package manager completes the installation of the above packages, you'll have a working nginx installation. Try it; you can point your browser at your server's port 80, and you should get the default nginx page indicating that it installed correctly.

Installing WordPress

Installing WordPress is quite straightforward; the complex part will be hooking together nginx with FPM, the PHP version of FastCGI. As you saw if you read my last article (in the June 2016 issue), FPM is the method through which nginx can run PHP in a separate process, without bloating the entire nginx process or reducing performance by very much.

The default location for HTML files in my nginx configuration is /usr/share/nginx/html. Within that directory, there's an index.html file, whose contents provide the default "welcome" page to nginx that you saw earlier.

The thing is, it's probably easiest just to install WordPress in a separate directory. So, I download WordPress and open it up under /usr/share/nginx/wordpress, which is a directory that'll be created anyway, when I open the tarfile. Here's what I did:

$ cd /usr/share/nginx
$ wget
$ tar -zvxf latest.tar.gz

Now that WordPress has been installed, you'll want to run it. But you can't do that until you have created a MySQL database, since part of the WordPress installation requires that your database be working and ready.

So, let's create a new MySQL database! There are several ways to do it. I typically prefer to use the mysqladmin program, which takes similar options to the MySQL client, including -u to indicate which user you want to use and -p to indicate that you want to enter a password. Both will be necessary:

$ mysqladmin create wordpress -u root -p

Note that when I say you want to use the "root" user here, I'm not referring to the UNIX-level root user. Rather, I'm talking about the MySQL "root" user, which has ultimate privileges on the database. When you installed MySQL earlier, you needed to choose a root password. It's this password that you must enter when prompted, thanks to the -p option above.