Install Varnish Cache with Apache on Ubuntu 20.04

In this article, we want to teach you how to Install Varnish Cache with Apache on Ubuntu 20.04.

Varnish Cache is a so-called reverse caching proxy.

It’s a piece of software that you put in front of your web server(s) to reduce the loading times of your website/application/API by caching the server’s output.

Install Varnish Cache with Apache on Ubuntu 20.04

To install Varnish Cache on Ubuntu 20.04, You need to log in to your server as a non-root user with sudo privileges and set up a basic firewall. To do this, you can follow our article about the Initial Server Setup with Ubuntu 20.04.

Also, you need to install Apache on your Ubuntu 20.04. For this, you can visit our article How To Install Apache on Ubuntu 20.04.

When you are done, follow the steps below to install Varnish Cache on Ubuntu 20.04.

Configure Apache on Ubuntu 20.04

First, update your local package index with the following command:

sudo apt update

The default port of Apache is 80. You need to change it to 8080.

To do this, you can use the following commands:

$ sudo sed -i -e 's/80/8080/g' /etc/apache2/ports.conf
$ sudo sed -i -e 's/80/8080/g' /etc/apache2/sites-available/*

Now restart Apache to apply the changes with the following command:

sudo systemctl restart apache2

Then, verify that Apache is listening on port 8080 with the following command:

sudo netstat -pnlt | grep 8080

In your output you should see:

Output
tcp6 0 0 :::8080 :::* LISTEN 3345/apache2

Now you can start to install Varnish Cache on Ubuntu 20.04.

Installing Varnish Cache

To install the LTS version of Varnish Cache, you need to add the official Varnish Cache repository.

First, add the required dependencies with the following command:

sudo apt install curl gnupg apt-transport-https

Then, add the GPG key for the package with the following command:

curl -L https://packagecloud.io/varnishcache/varnish60lts/gpgkey | sudo apt-key add -

Now add the Varnish Cache repository with the command below:

echo "deb https://packagecloud.io/varnishcache/varnish60lts/ubuntu/ focal main" | sudo tee -a /etc/apt/sources.list.d/varnish60lts.list

Then, specify a higher priority for this repository than the default package available in Ubuntu.

Create the file with your favorite text editor, here we use vi:

sudo vi /etc/apt/preferences.d/varnish

Add the following content to the file:

Package: varnish
Pin: origin packagecloud.io
Pin-Priority: 900

When you are done, save and close the file.

Update your local package index:

sudo apt update

Now you can install Varnish on Ubuntu 20.04 with the following command:

sudo apt install varnish

Here you have Varnish Cache installed on your server. Let’s see how to configure it.

Configure Varnish Cache on Ubuntu 20.04

At this point, you need to check the default address and port configuration. Open the Varnish configuration file with your favorite text editor, here we use vi:

sudo vi /etc/varnish/default.vcl

On the “backend default” section be sure that it looks like this:

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

When you are done, save and close the file.

Now you need to configure Varnish to listen at port 80 instead of the default of 6081.

First, create the directory for the custom configuration file with the following command:

sudo mkdir /etc/systemd/system/varnish.service.d

Then, create the file with your text editor:

sudo vi /etc/systemd/system/varnish.service.d/customport.conf

Add the following contents to the file:

[Service]
ExecStart=
ExecStart=/usr/sbin/varnishd -a :80 -a localhost:8443,PROXY -p feature=+http2 -f /etc/varnish/default.vcl -s malloc,256m

Save and close the file, when you are done.

To register the change reload the systemd with the following command:

sudo systemctl daemon-reload

Restart Varnish to apply the changes:

sudo systemctl restart varnish

Verify that Varnish is listening on port 80 with the following command:

sudo netstat -ltnp | grep ':80'

In your output you will see:

Output
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 9408/varnishd
tcp6 0 0 :::80 :::* LISTEN 9408/varnishd

Now you can use the curl command to test the Varnish Cache on Ubuntu 20.04:

curl -I http://localhost/
Output
HTTP/1.1 200 OK
Date: Wed, 24 Nov 2021 09:36:36 GMT
Server: Apache/2.4.41 (Ubuntu)
Last-Modified: Wed, 24 Nov 2021 08:44:34 GMT
Vary: Accept-Encoding
Content-Type: text/html
X-Varnish: 2
Age: 0
Via: 1.1 varnish (Varnish/6.0)
ETag: W/"2aa6-5d184de065e21-gzip"
Accept-Ranges: bytes
Content-Length: 10918
Connection: keep-alive

Be sure that the X-Varnish: 2 and Via: 1.1 varnish (Varnish/6.0) headers appear in the output.

Conclusion

At this point, you learn to install Varnish Cache with Apache on Ubuntu 20.04.

Hope you enjoy using it.

May this article about Install and Configure Varnish Cache For Apache on Centos 7 be useful for you.

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

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

Stay informed and not overwhelmed, subscribe now!