LinuxTutorialsUbuntu

How To Install Varnish on Ubuntu 20.04 LTS

Install Varnish on Ubuntu 20.04

In this tutorial, we will show you how to install Varnish on Ubuntu 20.04 LTS. For those of you who didn’t know, Varnish Cache is a web application accelerator that can be used as a proxy to your Apache webserver. The open-source software sits in front of your webserver to serve web traffic very fast. If you are running multiple servers, Varnish Cache can also be used as a load balancer. It makes your website really fast and accelerates your website performance up to 300 – 1000x (means 80% or more).

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you through the step-by-step installation of the Varnish HTTP accelerator on Ubuntu 20.04 (Focal Fossa). You can follow the same instructions for Ubuntu 18.04, 16.04, and any other Debian-based distribution like Linux Mint.

Prerequisites

  • A server running one of the following operating systems: Ubuntu 20.04, 18.04, 16.04, and any other Debian-based distribution like Linux Mint.
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • A non-root sudo user or access to the root user. We recommend acting as a non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.

Install Varnish on Ubuntu 20.04 LTS Focal Fossa

Step 1. First, make sure that all your system packages are up-to-date by running the following apt commands in the terminal.

sudo apt update
sudo apt upgrade

Step 2. Installing the Apache webserver.

Install Apache web server using the command below:

sudo apt install apache2

Once the installation of the Apache webserver is complete, start the webserver and check its status using the commands below:

sudo systemctl start apache2
sudo systemctl status apache2

Now we configure Apache to listen to port 8080, open the configuration file as shown with:

sudo nano /etc/apache2/ports.conf

And look for the Listen line and set another port. In this case, I will choose 8080:

Listen 8080

Next, edit the default Apache Virtual Host to listen to port 8080 to:

sudo nano /etc/apache2/sites-available/000-default.conf

Replace the VirtualHost line with this one:

<VirtualHost *:8080>

Save and exit the configuration file. For the changes to come into effect, restart the Apache webserver:

sudo apachectl configtest
sudo systemctl restart apache2

If your server is running behind a Firewall then make sure to open the Apache ports:

sudo ufw allow http
sudo ufw allow https

Step 3. Installing Varnish on Ubuntu 20.04.

Varnish should be available for installation in the standard Ubuntu repositories. Run the following command to install it:

sudo apt install varnish

Next, start the service and enable it to run at system startup:

sudo systemctl start varnish
sudo systemctl enable varnish

Step 4. Configure Varnish Cache.

We can do this by editing the Varnish configuration file /etc/default/varnish’:

cd /etc/varnish/
sudo cp default.vcl default.vcl.bak
sudo nano default.vcl

And inside the backend default section, make sure it has the following configuration:

backend default {
.host = "127.0.0.1";
.port = "8080";
}

And it is also necessary to make changes in another configuration file:

cd /etc/default/
sudo nano varnish

In the uncommented section of DAEMON_OPTS change the listening port and leave it as follows:

DAEMON_OPTS="-a :80 \
-T localhost:6082 \
-f /etc/varnish/default.vcl \
-S /etc/varnish/secret \
-s malloc,256m"

Now it is time to make one last change and it is in the file that manages the Varnish service where you have to modify the listening port:

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

Then, locate the ExecStart line and replace it with this one:

ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m

Restart the Apache and Varnish service for the changes to take effect:

sudo systemctl daemon-reload
sudo systemctl restart varnish
sudo systemctl restart apache2

Step 5. Testing Varnish.

The test consists of making an HTTP request via curl and verifying that it is handled by Varnish:

curl -I 192.168.77.21

Output:

HTTP/1.1 200 OK
Date: Tue, 06 Apr 2021 08:16:21 GMT
Server: Apache/2.4.41 (Ubuntu)
Last-Modified: Tue, 05 Apr 2021 20:12:22 GMT
Vary: Accept-Encoding
Content-Type: text/html
X-Varnish: 2
Age: 0
Via: 1.1 varnish (Varnish/6.2)
ETag: W/"2aa6-5meil4naa0e-gzip"
Accept-Ranges: bytes
Content-Length: 10918
Connection: keep-alive

Congratulations! You have successfully installed Varnish. Thanks for using this tutorial for installing the Varnish cache on your Ubuntu 20.04 LTS Focal Fossa system. For additional help or useful information, we recommend you check the official Varnish website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is a seasoned Linux system administrator with a wealth of experience in the field. Known for his contributions to idroot.us, r00t has authored numerous tutorials and guides, helping users navigate the complexities of Linux systems. His expertise spans across various Linux distributions, including Ubuntu, CentOS, and Debian. r00t's work is characterized by his ability to simplify complex concepts, making Linux more accessible to users of all skill levels. His dedication to the Linux community and his commitment to sharing knowledge makes him a respected figure in the field.
Back to top button