The latest stable release and long-term support of Ubuntu 20.04 LTS (Focal Fossa) has officially been rolled out since late last month. It bring many new features, bug fix, and improvements over its predeccessor. The most importants is Ubuntu 20.04 LTS includes the Linux kernel version 5.4+.

In today’s guide, we will teach you on how to install and configure Nginx with FastCGI-Cache module on Ubuntu 20.04 LTS (Focal Fossa). It’s very easy to do, you just need virtual machine running on Ubuntu 20.04 LTS. I think the guide is not very much different with previous version of Ubuntu LTS server.

1.) Full Root Privileges

Make sure you have full root access on your cloud server.

2.) Add Launchpad PPA Repository

After you have logged in to your system as a root user, we must add Launchpad PPA repository (Personal Package Archieve) for NGINX Mainline. It’s officially maintenanced by Nginx team.

$ sudo add-apt-repository ppa:nginx/mainline

And then simply press ENTER.

3.) Run System Update and Let’s Install Nginx

$ sudo apt update
$ sudo apt install nginx-extras

4.) Start Nginx and Enable it from Boot Automatically

$ systemctl enable nginx.service
$ systemctl start nginx

Check your Nginx version

root@dev:~# nginx -V
nginx version: nginx/1.17.10 (Ubuntu)
built with OpenSSL 1.1.1f 31 Mar 2020
TLS SNI support enabled

5.) Install and Configure PHP 7.4 FPM (PHP FastCGI Process Manager)

Since Ubuntu 20.04 LTS is shipped with PHP 7.4 as its default PHP version, so we don’t need to use third-party repository on this guide. Simply run the following command to buid PHP 7.4 FPM on your Ubuntu machine.

$ apt install php7.4 php-apcu php7.4-common php7.4-mysql php7.4-xmlrpc php7.4-curl php7.4-gd php7.4-cgi php7.4-json php7.4-cli php7.4-fpm php-pear php7.4-dev php7.4-imap php7.4-mbstring php7.4-xml php7.4-tidy php7.4-sqlite3 php7.4-xsl php7.4-gmp php7.4-zip php7.4-soap

6.) Start PHP7.4-FPM and Enable it to Start from Boot Automatically

Sometimes after we reboot the server, some services like PHP-FPM don’t start automatically. For this reason we need to make sure that PHP-FPM can start automatically from boot.

$ systemctl enable php7.4-fpm.service
$ systemctl start php7.4-fpm

7.) Configure FastCGI-Cache to work with Nginx

Simply add the following snippet code under “http {” inside your Nginx main config file.

$ nano /etc/nginx/nginx.conf
	##
	# Fastcgi-Cache Configuration
	##
        
        fastcgi_cache_path /var/nginx/fastcgi-cache levels=1:2 keys_zone=fcgicache:256m inactive=60m;
        fastcgi_cache_key "$scheme$request_method$host$request_uri";
        fastcgi_cache_use_stale error timeout invalid_header http_500;

        ##
        # Add Response Header Status
        ##

        add_header X-Cache $upstream_cache_status;
        fastcgi_ignore_headers Cache-Control Expires Set-Cookie;

After that, don’t forget create fastcgi-cache directory

$ mkdir -p /var/nginx/fastcgi-cache

Test your Nginx configuration

$ nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Restart Nginx web server

$ systemctl restart nginx

8.) Setting Up Nginx ServerBlock for Your Domain

Next, we must create serverblock a.k.a vhosts inside Nginx sites-available directory. Make sure to replace knot35.com with your actual domain name.

However, don’t forget to create docs root or public folder to manage all of your web files.

$ mkdir -p /var/www/knot35.com/public/ /var/www/knot35.com/logs/
$ chown -R www-data:www-data /var/www/knot35.com/

Copy this simple snippet code into your serverblock.

$ nano /etc/nginx/sites-available/knot35.com
# ServerBlock Configuration for Knot35.com
server {
        listen   80; ## listen for IPv4 Address
        #listen   [::]:80 default ipv6only=on;

        root /var/www/knot35.com/public/;
        index index.php index.htm index.html;
        server_name knot35.com www.knot35.com;
        
        access_log   /var/log/nginx/knot35.com.access.log;
        error_log    /var/log/nginx/knot35.com.error.log;

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

	# Nginx Fastcgi Config
        #fastcgi_cache start
        set $no_cache 0;

        # POST requests and urls with a query string should handled by PHP
        if ($request_method = POST) {
        set $no_cache 1;
        }   
        if ($query_string != "") {
        set $no_cache 1;
        }   

        # Don't cache uris containing the following segments
        if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {
        set $no_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 $no_cache 1;
        }

        # Fastcgi-Cache Config
	location ~ \.php$ {
		try_files $uri =404;
                fastcgi_pass 127.0.0.1:9000;
		include fastcgi_params;
                include fastcgi.conf;
                fastcgi_param HTTP_PROXY "";
                fastcgi_index index.php;
                # fastcgi_intercept_errors on;
	        fastcgi_cache_bypass $no_cache;
	        fastcgi_no_cache $no_cache;
	        fastcgi_cache fcgicache;
	        fastcgi_cache_valid 200 60m;
	}

        # Fastcgi-Purge Config
        location ~ /purge(/.*) {
               # Uncomment the following two lines to allow purge only from the webserver
               #allow 127.0.0.1;
	       #deny all;
               fastcgi_cache_purge fcgicache "$scheme$request_method$host$1";
        } 

	location = /robots.txt { access_log off; log_not_found off; }
	location ~ /\. { deny  all; access_log off; log_not_found off; }

}

Let’s create symbolic link (symlink) to Nginx sites enabled directory.

$ ln -s /etc/nginx/sites-available/knot35.com /etc/nginx/sites-enabled/
$ ln -s /var/log/nginx/knot35.com.access.log /var/www/knot35.com/logs/access.log
$ ln -s /var/log/nginx/knot35.com.error.log /var/www/knot35.com/logs/error.log

Test again your Nginx configuration, and restart it

$ nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
$ systemctl restart nginx

9.) Change Unix Socket Connection to TCP Socket

Next, we must edit default pool directives for PHP7.4-FPM in order to make Nginx work properly with PHP-FPM.

$ nano /etc/php/7.4/fpm/pool.d/www.conf

Search for the line:

listen = /run/php/php7.4-fpm.sock

Then replace with this one:

listen = 127.0.0.1:9000

Then restart PHP7.4-FPM service

$ systemctl restart php7.4-fpm

Now you have Nginx which works with the FastCGI-Cache module and PHP 7.4 FPM inside your virtual machine or cloud server. Next you can install web aplication software like WordPress, Joomla or Drupal. However, be sure to install and configure the database server first.

LEAVE A REPLY

Please enter your comment!
Please enter your name here