Covering nearly 30 percent of all websites, WordPress is certainly the Content Management System (CMS) of choice by many. Although it came from a blogging background, WordPress is a very powerful CMS for all content types and powers some of the world's busiest websites.
By combining it with NGINX, you can deploy a highly scalable web platform.
We'll also cover some of the more complex WordPress scenarios, including multisite configurations with subdomains and directories.
Let's get started.
Getting ready
To compile PHP code and run it via NGINX, the preferred method is via PHP-FPM, a high-speed FastCGI Process Manager. We'll also need to install PHP itself and, for the sake of simplicity, we'll stick with the OS-supplied version. Those seeking the highest possible performance should ensure they're running PHP 7 (released December 3, 2015), which can offer a 2-3 times speed improvement for WordPress, while at the same time being up to four times more memory efficient compared to PHP 5.6.
To install PHP-FPM, you should run the following on a Debian/Ubuntu system:
apt-get install php7.0-fpm
For those running CentOS/RHEL, you should run the following:
sudo yum install php-fpm
As PHP itself is a prerequisite for the php-fpm packages, it will also be installed.
How to do it...
In this instance, we're simply using a standalone WordPress site, which would be deployed in many personal and business scenarios. This is the typical deployment for WordPress.
For ease of management, I've created a dedicated config file just for the WordPress site (/etc/nginx/conf.d/wordpress.conf):
server { listen 80; server_name wordpressdemo.nginxcookbook.com; access_log /var/log/nginx/access.log combined; location / { root /var/www/html; try_files $uri $uri/ /index.php?$args; } location ~ \.php$ { fastcgi_pass unix:/var/run/php7.0-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name; include fastcgi_params; } }
Reload NGINX to read the new configuration file and check your log files if there are any errors. If you're installing WordPress from scratch, you should see the following:

You can complete the WordPress installation if you haven't already.
How it works...
For the most part, the configuration is the same as the static website configuration in Chapter 1, Let's Get Started. For the root URL call, we have a new try_files directive, which will attempt to load the files in the order specified, but will fall back to the last parameter if they all fail.
For this WordPress example, it means that any static files will be served if they exist on the system, then fall back to /index.php?args if this fails.
The args rewrite allows the permalinks of the site to be in a much more human form. For example, if you have a working WordPress installation, you can see links such as the one shown in the following screenshot:

Lastly, we process all PHP files via the FastCGI interface to PHP-FPM. In the preceding example, we're referencing the Ubuntu/Debian standard; if you're running CentOS/RHEL, then the path will be /var/run/php-fpm.sock.
NGINX is simply proxying the connection to the PHP-FPM instance, rather than being part of NGINX itself. This separation allows for greater resource control, especially since the number of incoming requests to the web server doesn't necessarily match the number of PHP requests for a typical website.
There's more...
Take care when copying and pasting any configuration files. It's very easy to miss something and have one thing slightly different in your environment, which will cause issues with the website working as expected. Here's a quick lookup table of various other issues which you may come across:
Error | What to check |
502 Bad Gateway | File ownership permissions for the PHP-FPM socket file |
404 File Not Found | Check for the missing index index.php directive |
403 Forbidden | Check for the correct path in the root directive and/or check for correct file permissions |
Your error log (defaults to /var/log/nginx/error.log) will generally contain a lot more detail regarding the issue you're seeing compared with what's displayed in the browser. Make sure you check the error log if you receive any errors.