Nginx Configuration Options and WordPress

If you want to use Nginx as your web server and you want to use WordPress to display your website, there are some NginX configuration options that will make administration as easy as it can get. Couple it with some specific WordPress plugins and your site will load fast and use fewer resources than you can imagine.

I’m going to give you the Nginx configuration options I’m using for this website, doing it the Ubuntu way. You can use this as a basis for your own Nginx configuration options or you can use some of my routines to enhance something you already have set up. Either way, I’ll try to explain why I’ve done certain things the way I’ve done them.

Installing Nginx

When you’re setting up the Nginx web server, it’s okay to log into SSH as root because it’s easier than using sudo in front of every command. I’ll be mentioning the commands without it, but if you feel safer logging in as a sudoer, make sure you put sudo in front of every command.

If you haven’t done so already, use apt-get install python-software-properties so you can easily add other sources to the repository list. After that, use these commands to add another source for the Nginx web server (and then install it) because the official Ubuntu repository is usually outdated:

The Main NginX Configuration File

The main Nginx configuration file is located at /etc/nginx/nginx.conf. You should copy it to a backup file before editing it. In my case, I did that and removed everything, pasting this Nginx configuration in its place:

You can visit the official nginx website or the less up-to-date nginx wiki to see what all the Nginx configuration options mean. You shouldn’t try to start Nginx unless you have php5-fpm already installed and I’ll get to that in the next article. When it comes to WordPress, you shouldn’t attempt to load your website unless you also have the MySQL server already installed.

In my Nginx configuration, I’ve added four lines for SSL because I use a single self-signed certificate for all of my WordPress admin sections, on all of my sites. The only issue with it is the WordPress JetPack plugin will not activate unless you have a real SSL certificate or you comment out anything SSL related. After the plugin activates, there isn’t any other problems with using a self-signed certificate. If you don’t want to use SSL, it’s safe to remove those lines. If you want to use real SSL certificates, you should copy the third and fourth lines to each virtual host configuration file (using the right file names, of course) and remove them from the main Nginx configuration file. The last line of the file is the place where some other specific Nginx configuration options are stored.

The conf.d Files

I use three files in this section:

I actually have more files in this section than I listed because each virtual host file has a separate rewrites file. That part isn’t important now.

Here’s the restrictions.conf file:

Here’s the wordpress.conf file:

You can uncomment the first three lines if you’re using SSL. The next file, the wordpress-ssl.conf file, isn’t necessary if you’re not using SSL. Here it is, if you are:

A Virtual Host File

The virtual host files are at /etc/nginx/sites-available. The don’t become active until they’re “symlinked” to /etc/nginx/sites-enabled. In a minute, I’ll give you a link to a shell script to make that part easy.

You should copy the default file to a backup file before editing it. Here’s mine:

I’ll use my domain website file as a real example, with some changes so I’m not publicizing my paths:

If you’re not using SSL, you can remove everything related to “443”, including the third server section. As I mentioned before, if you’re using real SSL certificates, you would place the third and fourth SSL directives I have in the main Nginx configuration file in the third section.

Nginx Manager

A shell script called nginx-manager, when installed at /usr/local/bin, makes it easy to enable or disable virtual hosts. For me, it’s easier to remember the commands for this script than how to create symbolic links.

Footnote

This is almost everything I have for the Nginx configuration options. There are other things you can add but they’re based on which WordPress plugins you use.

I didn’t explain every detail here because you really need to become familiar with the online documentation that explains what all the configuration directives mean.


 

Enter your e-mail address to receive new articles as I post them.