Install Varnish Cache with Apache on Ubuntu 22.04

This guide intends to teach you How To Install Varnish Cache with Apache on Ubuntu 22.04.

Varnish is a reverse proxy that makes a copy of the page in the memory to serve it out when the page will be re-visited. The copy will be made based on request headers like URL and Request Method. This is known as Full Page Cache.

Varnish Cache ensures pages of webshops do not have to be loaded over again. The performance and page load will benefit a lot from Varnish Cache. And that is exactly what you want: a lightning-fast webshop for all your visitors.

Steps To Install Varnish Cache with Apache on Ubuntu 22.04

To complete this guide, you must 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 guide the Initial Server Setup with Ubuntu 22.04.

Also, you need to have Apache installed on your server. To do this, you can follow the Apache installation part of this guide on How To Install LAMP Stack on Ubuntu 22.04.

Configure Apache on Ubuntu 22.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      4059/apache2

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

Set up Varnish on Ubuntu 22.04

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

First, add the required dependencies with the following command:

sudo apt install debian-archive-keyring curl gnupg apt-transport-https -y

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

curl -fsSL https://packagecloud.io/varnishcache/varnish70/gpgkey | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/varnish.gpg

Now add the Varnish Cache repository with the command below:

sudo tee /etc/apt/sources.list.d/varnishcache_varnish70.list > /dev/null <<-EOF
deb https://packagecloud.io/varnishcache/varnish70/ubuntu/ focal main
deb-src https://packagecloud.io/varnishcache/varnish70/ubuntu/ focal main
EOF

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 22.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 22.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      5978/varnishd
tcp6       0      0 :::8080                 :::*                    LISTEN      5791/apache2
tcp6       0      0 :::80                   :::*                    LISTEN      5978/varnishd

Testing Varnish Cache

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

curl -I http://localhost/
Output
HTTP/1.1 200 OK
Date: Mon, 03 Oct 2022 10:25:14 GMT
Server: Apache/2.4.52 (Ubuntu)
Last-Modified: Mon, 03 Oct 2022 10:11:51 GMT
Vary: Accept-Encoding
Content-Type: text/html
X-Varnish: 2
Age: 0
Via: 1.1 varnish (Varnish/7.0)
ETag: W/"29af-5ea1e927bad79-gzip"
Accept-Ranges: bytes
Content-Length: 10671
Connection: keep-alive

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

Conclusion

At this point, you have learned to Install Varnish Cache with Apache on Ubuntu 22.04.

Hope you enjoy it.

You may be like these guides:

Back up and Restore FileSystem With Timeshift on Ubuntu 22.04

How To Install RPM Packages on Ubuntu 22.04

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!