Share your love
Install Varnish Cache with Apache on Ubuntu 22.04

This guide on the Orcacore website 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 memory to serve it out when the page is revisited. 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.
Table of Contents
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 on 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.
1. Configure Apache For Varnish Cache
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.
2. 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 -y
Here you have Varnish Cache installed on your server. Let’s see how to configure it.
3. 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 on 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:

Testing Varnish Cache with Curl
Now you can use the curl command to test the Varnish Cache on Ubuntu 22.04:
curl -I http://localhost/

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. Installing Varnish Cache with Apache on Ubuntu 22.04 involves installing Varnish, configuring it to run on port 80, and adjusting Apache to listen on a different port (e.g., 8080). After updating the Varnish configuration, restart both services to enable caching and improve website performance.
Hope you enjoy it. Please subscribe to us on Facebook, Instagram, and YouTube.
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