Install WordPress on NGINX with Varnish Cache in Ubuntu 20.04

Today I’ll show you how you can host a WordPress website on an NGINX server with varnish caching for a fast and optimized WordPress website. 

If you want to host a WordPress website that can handle lots of traffic without crashing on a cheap cloud hosting like digital ocean basic plan then this is the perfect tutorial for you, with the help of this tutorial your WordPress website will be able to handle 5000+ visitors daily at a cost of just 5$ per month.

Why NGINX instead of Apache?

The reason I recommended you to use Nginx instead of Apache for your WordPress site’s web server is that Nginx can handle more traffic and load compared to an Apache server, as Apache server uses more memory compared to Nginx because it creates a new thread for each request which consumes memory.

Creating a Ubuntu Server

Okay so this step is for those people who haven’t created an ubuntu server yet, I will show you how you can create a ubuntu 20.04 server on AWS and also on Digital Ocean. Skip this part if you already have a ubuntu server.

Amazon AWS

To create a new Ubuntu server or Instance on AWS you will need to first log in to your AWS account, here go to the EC2 dashboard and click on “Launch Instance” to start the process of creating a new Ubuntu instance.

First, you will need to choose an Amazon Machine Image (AMI), in the search bar search for Ubuntu, this will display a few ubuntu machine images, you will need to select the “Ubuntu Server 20.04 LTS (HVM)” which comes under free tier, click on select on the right side.

choose amazon machine image

After that you will need to choose an instance type, choose the instance that suits your need, like t2.micro or t.2small for a small and medium blog, choose the instance type that you can afford, and click on next.

In the Configure Instance Details, you don’t need to change anything, click on next to continue.

In the Storage Tab enter the amount of storage that you need and the storage type that you need, like SSD or Magnetic, once done click on next. In Tags, no need to do anything, click next again.

choose ssd and storage

Now in the Configure Security Group tab, you will need to add two security group rules, which are HTTP and HTTPS. Click on “Add Rule”, in type select “HTTP”, again add another rule for “HTTPS”. Click Review and Launch to continue.

add http and https rule

In the Review Tab, check if everything is correct or not, things like Instance type, storage, and Machine image are important things to check before launching the instance. If everything is correct then click on “Launch”.

It will now ask you to add a Key Pair to the instance, select create a new Key pair option from the dropdown, name the keypair and click on download to download a .pem or .ppk file. Save this file somewhere safe because you will need it to log in to your server.

create a new keypair

After downloading the Key pair, click on Launch to launch a ubuntu server.

It will take a few minutes for the instance to run so wait a few minutes. Once done you can proceed to log in to the server.

Digital Ocean

If you are using Digital ocean then here are the steps to create a ubuntu server or droplet. To begin, log in to your digital ocean dashboard.

Click on Create then select Droplets, to create a new droplet. In image Choose Ubuntu 20.04, after that in plans select the plan that suits your need.

choose droplet image in digital ocean

Scroll down and in the data center region select the server that is closest to your customers or users. After that in the Additional option check the “IpV6” and “Monitoring”.

check ipv6 and monitoring

Now in authentication select Password and enter a very secure password, make sure to note it down somewhere safe. Scroll down and in Hostname enter your domain name and scroll down again and click on Create Droplet.

This will create a new ubuntu server for you.

Connect Domain -

If you want to connect a domain name to your server then you can follow these steps otherwise you can skip this step if you just want to use the IP address.

  1. Login to your domain provider and select your domain name and go to its DNS settings.
  2. Add a new “A” Record, in the name field enter “@” and in value enter the IP address of your server.
  3. After that add a “CNAME” Record, in the name enter “www” and in value enter your domain name.
add dns records in domain dns settings

Wait for the DNS propagation to finish and your domain will be connected.

Install Putty

Now to login to the ubuntu server, we will need to install Putty, you can download putty from its official website at – https://www.putty.org/. Download the latest version of putty from the website and install it.

Login to Ubuntu Server

Once you have created the ubuntu server and installed putty, you can now log in to your server. This process is different for different cloud hostings, I will show you the steps for AWS and Digital Ocean.

You can google how to log in to your cloud server if you are using something else.

Amazon AWS -

If you want to log in to your ubuntu server hosted on AWS then here are the steps. 

Open putty, in the hostname, enter the IP address of your amazon instance, after that on the left side click on “SSH” twice, this will display some more options, click on “Auth”, after that click on Browse and select the .ppk or .pem Key Pair downloaded during instance launch.

enter hostname and port & select private keypair

After that in putty click on open to connect to your AWS instance. In the terminal, it will ask “Login as”, enter root and you will be logged in to your AWS ubuntu server.

Digital Ocean -

If you are using digital ocean then just copy the Ip address of the Droplet and open putty, in hostname enter the Ip address and click on open.

Login as Root, enter the password that you used when setting up the digital ocean droplet. That’s it you will be now logged into your ubuntu server.

Install NGINX

Okay now it’s time to set up the server, we will be installing WordPress on Lemp Stack so the first thing we will need to do is install NGINX. All you need to do is copy & paste the commands given below in the putty terminal.

First, update your ubuntu apt packages before starting the installation process, you can update your apt packages using this command – 

sudo apt-get update && sudo apt-get upgrade

After that you can now download Nginx, to do that use this command – 

sudo apt-get install nginx

Once Nginx is installed check its status using the command “sudo service nginx status”, if it shows an output that says “Active(running) then Nginx is installed and running properly. Exit the status screen by pressing “q”.

After this, you will need to set up a firewall for ubuntu and also add a rule for ufw.

sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'

After adding the rule enable the ufw firewall and also check its status.

sudo ufw enable
sudo ufw status

Once done you can now check if Nginx is working properly or not, to do that enter the IP address of your Ubuntu server in a browser. This will display the “Welcome to Nginx” page which means that Nginx is working perfectly.

welcome to nginx

Install MySQL

Now we will install the MySQL database, again before starting the installation make sure to update the apt packages again – 

sudo apt-get update

After that enter the MySQL installation command, you might need to press “y” after entering the command.

sudo apt-get install mysql-server

After MySQL finishes installing, check its status and exit status screen once done. You can exit status by pressing “q”.

sudo service mysql status

After this, you will need to now create a root password for your MySQL, and remove anonymous users, Disable remote root login, remove the test database and reload privilege tables.

First create a root password, to do that first enter the command given below – 

sudo mysql_secure_installation

In the terminal you will now get an output asking “Would you like to set up the VALIDATE PASSWORD component?”, just enter any key to continue. After that, you will be asked to enter a new password and confirm the password.

Create a strong password and confirm it.

After that you will be asked to remove the anonymous users, press y to continue. After that it will ask “Disallow remote root login”, press y to continue.

After that, in both remove test databases and reload privilege tables press and enter “y” to finish setting up the MySQL database.

Install PHP

Now we will install PHP for our Nginx server, to begin first you will need to add the universe repository of Ubuntu to your system, use the command given below for adding the universe repo.

sudo add-apt-repository universe

After adding the repo, update the apt packages and install php-fpm and php-mysql. You can install them by using these commands – 

sudo apt-get install php-fpm
sudo apt-get install php-mysql

After installing both php-fpm and php-mysql you will need to configure Nginx for installing WordPress in it.

Configure NGINX

To configure Nginx for php you will need to first edit the default config file of your Nginx server, to do that we will be using nano editor, you can also use vim editor for this, enter this command in terminal –

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

The file path for the server block can be different sometimes.

That command will open the default config file in the nano editor, you will now need to change a few things inside this file to set up the Nginx server. The first thing will be to add index.php.

Inside this file scroll down and look for the “Add index.php to the list” option, here between the index and index.html enter index.php. Just below that in “server_name” enter your domain name or IP address. 

add index.php and server ip to virtual host configuration file

After that remove the “try_files $uri $uri/ =404;” line below it and replace it with the line given below.

try_files $uri $uri/ /index.php?$args;

Once done, just below it you will need to uncomment 3 lines of code, you can uncomment them by removing the “#”, the lines that you will need to uncomment are – 

  1. location ~ \.php$ {
  2. include snippets/fastcgi-php.conf;
  3. fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;

After that, also uncomment the closing bracket below the “fastcgi_pass 127.0.0.1:9000;” line. Once done, recheck everything and save the file by clicking “CTRL+X > Y > Enter”.

uncomment php code

After saving the file, check if there are any errors in the file by using the command –

sudo nginx -t

This will display an output in the terminal like, “Syntax is OK” and “test is successful”, this means that there are no errors after editing the file and you can now proceed to the next step.

First, reload your Nginx config using the command – 

sudo service nginx reload

You can check if php on your Nginx server is working or not by creating a new .php file at location “/var/www/html/” by using the command – 

sudo nano /var/www/html/test.php

Install phpMyAdmin

Now to be able to set up and manage MySQL databases on your WordPress website you will need to install and set up phpMyAdmin.

To begin first update your apt packages using the command “sudo apt update”, after that download PHPMyAdmin using the command – 

sudo apt-get install phpmyadmin

During the download, it will ask you to enter “y” enter y and press enter to continue, when asked to choose a web server you will need to press TAB Key and press enter to continue.

php installation - server select

After that select yes and press enter in “Configure database for phpmyadmin”, The setup will now ask you for a password for PHPMyAdmin, enter the same password that you used during MySQL installation, select OK and press Enter, you might get asked to confirm the password.

Once phpMyAdmin is installed you will need to link your PHPMyAdmin to your root directory, to do that enter the command given below.

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

Once done go to your web browser and go to domain.com/phpmyadmin or ip_addresss/phpmyadmin to check if PHPMyAdmin is working perfectly or not. If it is then now you will need to create a superuser to log in to your admin dashboard.

First, enter the command given below to enter MySQL.

sudo mysql -u root -p

After this you will now need to create a username and password for phpmyadmin, the format for the command will be “CREATE USER ‘username’@’localhost’ IDENTIFIED BY ‘password’;”, where change the username to anything you like and change the password to a secure password.

CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';

After that, grant all the root privileges to the user using the command below, change the username to the one you added using the above command –

GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost';

Once done, exit using the command “exit”.

[ OPTIONAL BUT RECOMMENDED STEP ]

You should secure your phpMyAdmin by following this tutorial here – https://bit.ly/3FG2lso

Create a MySQL Database

Now after installing phpmyadmin, log in to the dashboard by going to your_domain.com/phpmyadmin, log in using the username and password that you provided.

After logging in, on the top left click on “Databases”, here in “database name” enter a database name like “wordpress_database” and click on create to create the database. After that proceed to the installation of WordPress.

create new mysql database

Install WordPress

Now we can start the installation of WordPress, first, go to the official website of WordPress at – https://wordpress.org and download the latest version of the WordPress zip file.

Once done extract the downloaded zip file and go inside it, here look for “wp-config-sample.php”, right click and rename it to just “wp-config.php”, after renaming it, edit the PHP file using a note editor by right clicking and open with > note pad. 

rename wpconfig file

Inside the wp-config file look for “MySQL database username” and change the “database_name_here” to your database name. Below that change the “username_here” to your MySQL username, below that change password to your database password.

edit the wp-congif file

Once done, save the file. After that select all the files using your mouse and archive them to a zip file named wordpress.zip.

archive all the wordpress file to a zip file

Now, open Filezilla and log in using the Ip address of your server, username will be root, enter the password of your server, and select port 22. For AWS you will need to use the key pair to log in to Filezilla, the steps can be seen here – https://youtu.be/-80-39VNzOA. 

After logging in, go to the file path “/var/www/html”, and inside the html, directory upload the edited wordpress.zip that you created. Wait for the upload to finish, after the wordpress.zip finishes uploading open putty and log in to your server.

upload wordpress zip to server

First, you will need to install unzip, to do that update your apt packages and install unzip – 

sudo apt-get install zip unzip 

After installing unzip, you can now unzip the WordPress zip file, to do that change your directory to “/var/www/html/” using the command – 

cd /var/www/html/

After changing the directory use the unzip command to unzip the WordPress zip file –

unzip wordpress.zip

Once the zip file finishes unzipping without any error, remove the zip file from the directory –

rm wordpress.zip

That’s it, WordPress is now installed successfully on your Nginx server.

Setup WordPress

To set up WordPress just enter your Ip address in a browser, if you have a domain connected then enter your domain name.

You will be redirected to the setup page of WordPress where you will need to first enter your site title, then enter a username for logging in to the server and a secure password, after that enter the admin email and Click “Install WordPress”.

Once WordPress install finishes you can log in to your website by going to “domain_or_IP/wp-login.php”.

In your WordPress website, check if everything is working or not, things like are you able to install and activate themes and plugins or not, does changing permalinks break your website. Try out different things and keep refreshing your website for potential errors.

If there is none then congratulations, your WordPress website is now almost ready to use, why almost?? Because there are still two things that we have left to set up on our WordPress website, the first is an SSL Certificate and the second one is Varnish Cache for an optimized web server.

Install SSL Certificate

Okay so now you have two options to add an SSL Certificate to your WordPress website with an Nginx server, the first is directly installing a free SSL certificate in your web server, the second one is to use Cloudflare to add the SSL certificate.

Both methods are easy to do, if you don’t want to use Cloudflare then you can use the direct install method and if you are using Cloudflare then use Cloudflare to add the SSL.

Install SSL Nginx-Ubuntu

To install SSL directly in our server we will use certbot, to begin first update your apt packages using the command “sudo apt-get update”, after that install the certbot using the command – 

sudo apt-get install certbot python3-certbot-nginx

If you are getting any errors then try “sudo apt-get install certbot python-certbot-nginx” instead. 

After installing the certbot, run the command given below to get an SSL Certificate for your domain or IP. 

sudo certbot – nginx

At first, you will be asked for an email address, enter an email then press enter, after that accept the terms and conditions by pressing a, after that enter y to receive a newsletter from let’s encrypt. After that, you will need to select the domain name or IP address that you want to get the SSL for.

After that, you will be asked whether you want to redirect all the traffic from HTTP to HTTPS or not, enter 2 and press enter. Now your SSL certificate will be assigned to your domain or IP address and HTTP traffic will now get redirected to HTTPS.

NOTE: The SSL certificate that you will get will only be valid for 90days, which means you will need to renew it after 90days using putty again.

Install SSL using Cloudflare

If you don’t want to install an SSL directly on your server then you can use Cloudflare’s SSL to serve your website over HTTPS. To do that make a Cloudflare account and connect your domain name, after that log in to the dashboard and go to the SSL/TLS tab.

Here Scroll down and select Flexible, after that go to Edge certificates and scroll down and enable the “Always use HTTPS” option.

select flexible ssl in cloudflare

That’s it, all the frontend users will now be served using a secure SSL certificate.

Install Varnish Cache

Now the final thing that we will need to install on our Nginx server is Varnish Cache. Varnish Cache is an HTTP accelerator used for caching HTTP requests to make a web server response time faster.

To Install Varnish on your Nginx server, first update its apt package using the command “sudo apt-get update”, after that use this command to install varnish cache.

sudo apt-get install varnish -y

After installing varnish check if it is installed or not by entering the command “varnish -v” in the terminal, this will show you the version of varnish installed.

After that, you will need to change the port to which Varnish listens to. Enter the command given below which will again open the default configuration file of Nginx.

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

Inside the file scroll down and look for “Virtual host Configuration”, change both the listen 80 to 8088. Once done save the file and restart the Nginx server.

change port 80 to 8088
systemctl restart nginx

Now you will need to edit the varnish.service file, to do that we will use nano editor, use the command –

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

Inside the file change the default port 6081 to 80, save it, and after that reload daemon.

systemctl daemon-reload

Now you will need to edit the default.vcl file, use the command – 

sudo nano /etc/varnish/default.vcl

Inside the vcl file in “backend default” change the port from “8080” to “8088”. Save and Exit.

After that restart varnish – 

systemctl restart varnish

Now check if varnish is working correctly or not by entering this command –

curl -I your-server-ip

In the terminal, you will get the output which will show if varnish is working or not.

That’s it, you have now successfully installed a WordPress website on an NGINX Server with Varnish Cache in Ubuntu 20.04. If you are facing any problems then comment below, thank you for reading. 

Leave a Comment

Your email address will not be published. Required fields are marked *