Installing Nginx + Php 7.2 FPM + Phpmyadmin + Varnish Cache on Ubuntu 18.04

Affiliate disclosure: In full transparency – some of the links on our website are affiliate links, if you use them to make a purchase we will earn a commission at no additional cost for you (none whatsoever!).

Hi, As we all know, hosting a WordPress site is easy but optimizing it to load faster with the minimum bugs is not a bad of roses.

Today, we are going to install LEMP stack on Ubuntu 18.04 + Varnish cache to facilitate the fastest loading speed.

Deploy the server from anywhere. Just make sure that you have ubuntu 18.04 droplet. It can be Google cloud, Vultr, DigitalOcean or any other VPS provider.

LOGIN as root in putty, terminal or browser SSH terminal.

Start with the commands below.

sudo apt-get update

sudo apt-get install nginx

sudo apt-get install mysql-server

sudo mysql_secure_installation

sudo apt-get install php-fpm php-mysql

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

You will see a line cgi.fix_pathinfo=1, if the line is quoted, remove the quote and replace ‘0’ with ‘1’


service php7.2-fpm start

sudo nano /etc/nginx/sites-available/default

listen 80 default_server;
listen [::]:80 default_server;

root /var/www/mydomain; index index.php
index.html index.htm index.nginx-debin.html;

server_name mydomain;

location / {
try_files $uri $uri/ /index.php$is_args$args;

location ~ \.php$ {
include $nippets/fastcgi.php.conf;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;

location ~ /\.ht {
deny all;


sudo mkdir /var/www/mydomain

sudo nginx -t

sudo systemctl reload nginx

Now, the server is setup. Lets install PhpMyadmin

sudo apt-get update

sudo apt-get install phpmyadmin

Lets link to Phpmyadmin

sudo ln -s /usr/share/phpmyadmin /var/www/mydomain

sudo phpenmod mcrypt

Do not panic if the above command does not work. Let’s move the next.

sudo service php7.2-fpm restart

Let’s change the phpmyadmin dashboard location

cd /var/www/mydomain

sudo mv phpmyadmin qu_db

Our PHPMyAdmin is also setup Now.

So far, we have installed Nginx, PHP 7.2 fpm and phpmyadmin on the Ubuntu 18.04.

Now is the time to access your MySQL through SSH and create database, user and grant all permissions.

LOGIN to your server as root and enter the mysql-

sudo mysql -u root -p

Enter password and hit enter.

Lets create a database (below are SQL commands)


CREATE USER ‘wordpress’@‘localhost’ IDENTIFIED BY ‘password’;

GRANT ALL ON wordpress.* TO ‘wordpress’@’localhost’ IDENTIFIED BY ‘password’;

Now is the time to login to your database through the phpmyadmin console.

I will use FileZilla to upload the WordPress files in the document root directory of the server.

Download WordPress

  • Edit the wp-config.php
  • Archive the whole WordPress
  • Rename it to
  • Upload it to /var/www/mydomain

I am back on the SSH terminal Now and I will install zip unzip on the server.

sudo apt-get update

sudo apt-get install zip unzip

Navigate to the document root and unzip the file here.

cd /var/www/mydomain/

sudo unzip

sudo rm -r

chown -R www-data:www-data /var/www/mydomain

Your server stack is all set to host the wordpress website.

  • Open
  • WordPress Installation wizard opens
  • Fill up the required fields and run the installation

Next – Install Varnish Cache on Nginx server

sudo apt -y install varnish nginx

ss -tlnf inet


After successful installation your Nginx server Line 4should be listening on port 80. Varnish on Lines 6,7is using both 6081 and 6082 ports.

sudo nano /etc/nginx/sites-available/default

In the default hosting block, the server should listen to port 8080

server {
listen 8080 default_server;
listen [::]:8080 default_server;

Save the file and exit.

sudo service nginx reload

ss -tlnf inet

Nginx should now be listening on new default port 8080 as shown on Line 4 from the ss command output:


If you want to replace the normal Nginx default page to a varnish default page, run below command. (optional)

$ sudo sed -i ‘s/nginx/Varnish cache server on Nginx/g’ /var/www/html/index.nginx-debian.html

sudo nano /lib/systemd/system/varnish.service

Edit Line 9 and change default port 6081 to port 80 as shown bellow:


Instructing Varnish to rely on nginx.

sudo nano /etc/varnish/default.vcl

The file should have port set to 8080. (check and set port to 8080 if it is 80.)

backend default {
.host = “”;
.port = “8080”;

All set! Lets reload the Nginx server.

sudo systemctl daemon-reload

sudo service varnish restart


curl -I varnish-server-ubuntu

It may ask you to first install curl. Run the below command if you see such error.

sudo apt-get install curl

The response should look something like this:

HTTP/1.1 200 OK Server: nginx/1.13.6 (Ubuntu)
Date: Thu, 22 Feb 2018 03:50:52 GMT
Content-Type: text/html
Last-Modified: Thu, 22 Feb 2018 03:08:27 GMT
ETag: W/“5a8e342b-324”
Vary: Accept-Encoding
X-Varnish: 2
Age: 0
Via: 1.1 varnish (Varnish/5.2)
Accept-Ranges: bytes
Connection: keep-alive

Your site is on one of the fastest WordPress server stack. Enjoy!


Vivek Dhiman

Leave a Comment