In the previous chapter we set up server monitoring and discussed ongoing maintenance for our Ubuntu web server. In this final chapter I offer a complete Nginx configuration optimized to configure WordPress websites.

In addition to amalgamating all information from the previous 9 chapters, I will be drawing upon best practices from my experience and various sources I’ve come across over the years. The following example domains are included, each demonstrating a different scenario:

  • ssl.com – WordPress on HTTPS
  • ssl-fastcgi-cache.com – WordPress on HTTPS with FastCGI page caching
  • multisite-subdomain.com – WordPress Multisite using subdomains
  • multisite-subdirectory.com – WordPress Multisite using subdirectories

Before diving into this configuration, we recommend double-checking that you have the latest version of MySQL by referencing the tutorial in chapter 2 of this guide. Once that’s confirmed, you’ll see that the configuration files contain inline documentation throughout and are structured in a way to reduce duplicate directives, which are common across multiple WordPress configurations. This should allow you to quickly create new WordPress sites with sensible defaults out of the box, which can be customized as required.

Usage

You can use these configs as a reference for creating your own configuration, or directly by copying into your etc directory. Follow the steps below to replace your existing Nginx server configuration.

Back up any existing config with the following command:

sudo mv /etc/nginx /etc/nginx.backup

Copy one of the example configurations from sites-available to sites-available/yourdomain.com:

sudo cp /etc/nginx/sites-available/ssl.com /etc/nginx/sites-available/yourdomain.com`

Edit the config as necessary, paying close attention to the server name and server paths. You will also need to create any directories used within the configuration and configure Nginx to have read/write permissions.

To enable the site, symlink the configuration into the sites-enabled directory:

sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/yourdomain.com

Test the configuration:

sudo nginx -t

If the configuration passes, reload Nginx:

sudo /etc/init.d/nginx reload

Nginx Config Preview

The following is the single-site-with-caching.com Nginx configuration file that’s contained in the package. It should give you a good idea of what it’s like to use our configs.

Warning: The following Nginx config will not work on its own. You’ll notice there are several include statements which require files contained in the package. Download the Complete Nginx Config Package

server {
    # Ports to listen on
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    # Server name to listen for
    server_name single-site.com;

    # Path to document root
    root /sites/single-site.com/public;

    # Paths to certificate files.
    ssl_certificate /etc/letsencrypt/live/single-site.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/single-site.com/privkey.pem;

    # File to be used as index
    index index.php;

    # Overrides logs defined in nginx.conf, allows per site logs.
    access_log /sites/single-site.com/logs/access.log;
    error_log /sites/single-site.com/logs/error.log;

    # Default server block rules
    include global/server/defaults.conf;

    # SSL rules
    include global/server/ssl.conf;

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

    location ~ \.php$ {
        try_files $uri =404;
        include global/fastcgi-params.conf;

        # Use the php pool defined in the upstream variable.
        # See global/php-pool.conf for definition.
        fastcgi_pass   $upstream;
    }
}

# Redirect http to https
server {
    listen 80;
    listen [::]:80;
    server_name single-site.com www.single-site.com;

    return 301 https://single-site.com$request_uri;
}

# Redirect www to non-www
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name www.single-site.com;

    return 301 https://single-site.com$request_uri;
}

Download the Complete Nginx Configuration Kit

Enter your name and email below and we’ll email you a zip of the Nginx configuration files. I promise we will only use your email to send you the config files, notify you of updates to the config files & this guide in the future and share helpful tips for managing your own server.

Unsubscribe any time from the footer of any email we send you. If you want news about SpinupWP, you’ll need to subscribe at the bottom of the page.

That’s All Folks!

Job done! I encourage you to explore the config files further and read through the documented configuration to get a feel for what’s going on. It should feel familiar as it follows the same conventions used throughout this guide.

Over time I will improve the configuration and add new best practices as they emerge. If you have any improvements, please let me know.

That concludes this chapter and the guide as a whole. It’s been quite a journey, but hopefully you’ve learned a lot and are more confident managing a server than when you started.