How to Install and Configure Varnish on Ubuntu 20.04?

The administration of web services is an elementary task nowadays when managing a server. That is why there are always tools that help us with the task. One of them is the webserver acceleration, something that seems basic in our times. To help you with that is that this post exists so that you can install and configure Varnish on Ubuntu 20.04.

What is Varnish?

According to the Varnish website:

Varnish Cache is a web application accelerator also known as a caching HTTP reverse proxy. You install it in front of any server that speaks HTTP and configures it to cache the contents. Varnish Cache is really, really fast. It typically speeds up delivery with a factor of 300 – 1000x, depending on your architecture

One of the key features of Varnish Cache, in addition to its performance, is the flexibility of its configuration language, VCL. VCL enables you to write policies on how incoming requests should be handled. In such a policy you can decide what content you want to serve, from where you want to get the content, and how the request or response should be altered.

Another interesting aspect is that Varnish is open source released under the FreeBSD license. This project was born in 2005 so it has a long trajectory that has allowed it to gain experience.

So Varnish is a very useful tool for our web servers and especially when we face a lot of traffic.

Install and configure Varnish on Ubuntu 20.04

Varnish is very flexible and that is why it can be configured in both Nginx and Apache. Being the latter, the current choice for being very popular.

First Step: Install and Configure Apache

The first step is to install Apache on Ubuntu 20.04 and then make some configurations to set it up with Varnish.

So, in a command-line environment first, update the operating system.

sudo apt update
sudo apt upgrade

After that, install Apache from the official Ubuntu 20.04 repository.

sudo apt install 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

Now you need to change the port that Apache uses by default which is 80 for this you need to modify the ports.conf file.

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

Save the changes and close the editor.

Now you have to do the same in the default Virtualhost configuration file:

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

And replace the VirtualHost line with this one:

<VirtualHost *:8080>

Save the changes and close the file.

Remember that every time you add a new VirtualHost you have to change the port in its configuration file.

You can test the syntax of the configurations for errors:

sudo apachectl configtest
Syntax OK

Finally, to apply the changes, restart Apache.

sudo systemctl restart apache2

Now let’s go to Varnish

Second Step: Install Varnish on Ubuntu 20.04

Varnish is available in the official Ubuntu repositories so the installation is quite easy to do.

So, to install Varnish run the following command:

sudo apt install varnish

You can start the service and enable it to run at system startup

sudo systemctl start varnish
sudo systemctl enable varnish

After that, you have to do some configuration to make it work with Apache.

So, Varnish uses VCL for its configurations so we have to edit the default configuration file.

Before this, make a backup:

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 = "";
.port = "8080";

As you can notice, we are indicating that it will listen on port 8080 on localhost to Apache.

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"

Save the changes and close the file.

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

Save the changes and close the file.

To apply the changes, refresh the Systemd services

sudo systemctl daemon-reload

And restart Varnish:

sudo systemctl restart varnish

To check that everything is OK you have to request the website with the Curl command

curl -I [your-server]

Replace [your-server] with the IP address of your server or the domain. You should receive an output similar to this:

HTTP/1.1 200 OK
Date: Tue, 23 Mar 2021 20:16:57 GMT
Server: Apache/2.4.41 (Ubuntu)
Last-Modified: Tue, 23 Mar 2021 20:02:35 GMT
Vary: Accept-Encoding
Content-Type: text/html
X-Varnish: 2
Age: 0
Via: 1.1 varnish (Varnish/6.2)
ETag: W/"2aa6-5be39a9d0fa0e-gzip"
Accept-Ranges: bytes
Content-Length: 10918
Connection: keep-alive

This way Varnish will be installed and configured.


Varnish together with Apache is a good combination that will help our webserver to better process all the websites that we can deploy. And as we have been able to demonstrate the configuration is not a complicated matter.

Scroll to Top