The Best WordPress Nginx FastCGI Settings on Ubuntu 16.04 LTS Servers with PHP-FPM 7.1 Support

If you want to improve your WordPress site performance, you may want to enable FastCGI caching. Doing this will greatly improve WordPress performance. This brief tutorial is going to show students and new users how to enable FastCGI on Ubuntu 16.04 LTS with PHP7.1-FPM..

When you combine Nginx webserver and FastCGI module, you will greatly improve your PHP-based applications, including WordPress websites. FastCGI module caches dynamic PHP content that are served through Nginx backend.

When dynamic PHP content is cached, repeated requests for the same content is quickly returned from the cache store, instead of compiling all the dynamic data the make up the page each time a request is made.

So, when you’re running a website or blog powered by Nginx, make sure to include FastCGI caching.

Step 1: Install Nginx Latest Version

In order to get the latest version that is not available to Ubuntu server by default, you may want to install Nginx Mainline repository… the steps below show you how to do that…

To install Nginx web server from its Mainline repository, run the commands to add Nginx’s repository key to Ubuntu. You’ll need to install the key in order for Ubuntu to trust packages from that repository.

cd /tmp/ && wget http://nginx.org/keys/nginx_signing.key

After adding the key, run the commands below to install Nginx’s Mainline repository or branch on Ubuntu.

sudo sh -c "echo 'deb http://nginx.org/packages/mainline/ubuntu/ '$(lsb_release -cs)' nginx' > /etc/apt/sources.list.d/NginxMainline.list"

When you’re done, all you need to so is run the commands below to install the latest version of Nginx.

sudo apt-get update
sudo apt-get install nginx

Step 2: Install PHP7.-FPM Module

PHP 7.1 isn’t available on Ubuntu default repositories… in order to install it, you will have to get it from third-party repositories.

Run the commands below to add the below third party repository to upgrade to PHP 7.1

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php

Then update Ubuntu by running the commands below…

sudo apt update

Then run the commands below to install PHP7.1-FPM and related modules…

sudo apt install php7.1-fpm php7.1-common php7.1-mbstring php7.1-xmlrpc php7.1-gd php7.1-xml php7.1-mysql php7.1-cli php7.1-zip php7.1-curl

After install PHP 7.1, run the commands below to open PHP-FPM default file.

sudo nano /etc/php/7.1/fpm/php.ini

Then make the changes on the following lines below in the file and save. The value below are great settings to apply in your environments.

file_uploads = On
allow_url_fopen = On
memory_limit = 256M
upload_max_file_size = 100M
cgi.fix_pathinfo=0
max_execution_time = 360
date.timezone = America/Chicago

Step 3: Setup FastCGI Directive in Nginx.conf file.

On Ubuntu systems, Nginx configuration files are stored in the /etc/nginx directory. In that directory, Nginx’s main configuration file called Nginx.conf is also stored in there. In that file is where you setup Nginx global configurations.

In Nginx main configuration file at /etc/nginx/nginx.conf, place the block of code before the last line in the file and save.

Run the commands below to open the file…

sudo nane /etc/nginx/nginx.conf

Then copy and paste the block of lines below into the file and save… then close out

## Nginx FastCGI Cache
    fastcgi_cache_path /var/cache/nginx/fastcgi_temp/cache levels=1:2 keys_zone=cachezone:10m max_size=2g inactive=60m use_temp_path=off;
    fastcgi_cache_key $scheme$request_method$host$request_uri;
    fastcgi_cache_lock on;
    fastcgi_cache_revalidate on;
    fastcgi_cache_background_update on;
    fastcgi_cache_use_stale error timeout invalid_header updating http_500;
    fastcgi_cache_valid any 60m;
    fastcgi_pass_header Set-Cookie;
    fastcgi_pass_header Cookie;
    fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

Save the file when done.

Step 4: Setup FastCGI Directive in Nginx Web config file.

On Ubuntu systems, Nginx individual website configuration file or virtual host files are store in /etc/nginx/sites-available/

That is where you store your website configurations. There should already be a default configuration file there. When implementing FastCGI for a website, open the site configuration file and edit the PHP block as shown below.

Run the commands below to open the default site configuration file…

nano /etc/nginx/sites-available/default

Then make the php block look like the one below and save.

location ~ \.php$ {
         include snippets/fastcgi-php.conf;
         fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         fastcgi_cache_bypass $skip_cache;
         fastcgi_no_cache $skip_cache;
         fastcgi_cache cachezone;
         include fastcgi_params;
         fastcgi_buffer_size 128k;
         fastcgi_connect_timeout 60s;
         fastcgi_send_timeout 60s;
         fastcgi_read_timeout 60s;
         fastcgi_buffers 256 16k;
         fastcgi_busy_buffers_size 256k;
         fastcgi_temp_file_write_size 256k;
    }

Save the file when you’re done.

If everything is setup correctly, PHP should be going through FastCGI module and caching enabled.

For WordPress website, you should also include the block of code below into the site configuration file, just above the php block…

set $skip_cache 0;
   # POST requests and url's with a query string should always skip cache
    if ($request_method = POST) {
        set $skip_cache 1;
    }
    if ($query_string != "") {
        set $skip_cache 1;
    }
    # Don't cache url's containing the following segments
    if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
        set $skip_cache 1;
    }
    # Don't use the cache for logged in users or recent commenters
    if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
        set $skip_cache 1;
    }

Save the file and you’re done.

Reload Nginx and you’re done.

sudo systemctl reload  nginx.service

This is how to enable FastCGI support for Nginx and it’s the best setting that we’ve have and that works on all our sites.. It’s great.. you should try it.

You may also like the post below:

Install Drupal on Ubuntu 16.04 LTS with Lighttpd HTTP Server, MariaDB and PHP 7.1 Support