How to Install Varnish on Ubuntu 22.04

How to Install Varnish on Ubuntu 22.04

In this tutorial, we are going to show you how to install Varnish on Ubuntu 22.04.

Varnish cache is a web application accelerator that is used as caching HTTP reverse proxy. The varnish case has many advantages against other caching software and can speed up delivery up to 1000 times. Varnish offers many features such as private CDN, Gzip compression and decompression, HTTP streaming pass & fetch, etc.

In this blog post for the installation, we are going to configure it with the Apache web server. This process is very easy and may take up to 20 minutes. Let’s get started!


  • Fresh install of Ubuntu 22.04
  • User privileges: root or non-root user with sudo privileges

Step 1. Update the System

If you have a fresh installation of Ubuntu 22.04, update the system packages to the latest versions available.

sudo apt update -y && sudo apt upgrade -y

Step 2. Install Varnish

First, we are going to add the Varnish repository because it does not exist in the default Varnish repository.

tee /etc/apt/sources.list.d/varnishcache_varnish70.list > /dev/null <<-EOF
deb focal main
deb-src focal main

Once added, update the system


sudo apt-update -y

Install the varnish cache with the commands below:

sudo apt install varnish -y

Once installed, start and enable the service.

sudo systemctl start varnish && sudo systemctl start varnish

To check if the service is up and running, type the following command:

sudo systemctl status varnish

You should receive the following output:

root@host:~# systemctl status varnish
● varnish.service - Varnish Cache, a high-performance HTTP accelerator
     Loaded: loaded (/lib/systemd/system/varnish.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2022-09-21 16:13:26 CDT; 1 week 5 days ago
   Main PID: 2449 (varnishd)
      Tasks: 217 (limit: 4575)
     Memory: 92.4M
        CPU: 11min 37.355s
     CGroup: /system.slice/varnish.service
             ├─2449 /usr/sbin/varnishd -j unix,user=vcache -F -a :6081 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m
             └─2471 /usr/sbin/varnishd -j unix,user=vcache -F -a :6081 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m

By default, the varnish service is running on port 6081. You can check this with the command below:

netstat -tunlp | grep 6081

You should receive the following output:

root@host:~# netstat -tunlp | grep 6081
tcp        0      0  *               LISTEN      2449/varnishd
tcp6       0      0 :::6081                 :::*                    LISTEN      2449/varnishd

This is another proof that the varnish cache is installed successfully and the service is up and running.

Step 3. Install Apache2

First, we need to install the Apache Web server to configure Varnish later. To install the Apache Web server execute the following command:

sudo apt install apache2 -y

To start and enable the Apache service execute the following command:

sudo systemctl start apache2 && sudo systemctl enable apache2

To check the status of the service:

sudo systemctl status apache2

You should receive the following output:

root@host:~# sudo systemctl status apache2
● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2022-09-21 15:52:40 CDT; 1 week 5 days ago
   Main PID: 767 (apache2)
      Tasks: 55 (limit: 4575)
     Memory: 13.9M
        CPU: 1min 15.604s
     CGroup: /system.slice/apache2.service
             ├─   767 /usr/sbin/apache2 -k start
             ├─123603 /usr/sbin/apache2 -k start
             └─123604 /usr/sbin/apache2 -k start

By default, the Apache service is running on port 80. We need to change that for we can configure the Varnish cache to work with the Apache web server late.

To change the Apache port from 80 to 8080, open the file /etc/apache2/ports.conf with your favorite text editor and change the Listen 80 to Listen 8080 to look like this:

Listen 8080

Save the file, close it and restart the Apache service.

sudo systemctl restart apache2

To check if everything is OK, execute the following command:

netstat -tunlp | grep apache2
root@host:# netstat -tunlp | grep apache2
tcp6       0      0 :::8080                 :::*                    LISTEN      127811/apache2

Step 4. Configure the Varnish Cache

Now, when Varnish is installed and Apache is listening on port 8080, we can proceed with configuring the Varnish. First of all, we are going to change the default listening Varnish port from 6081 to 80.

Open the /lib/systemd/system/varnish.service file and change the port to 80, along with the cache size to 1GB to look like this:

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer

Now just $43 .99

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,1g

Save the file, close it and reload the daemon and restart the Varnish service.

sudo systemctl daemon-reload && sudo systemctl restart varnish service

Next is to check the Varnish cache backend. Open the file /etc/varnish/default.vcl and check if the changes are affected in this file. The lines should look like this:

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

Restart the Apache and Varnish services again:

sudo systemctl restart apache2 varnish

The final command is to check with curl command if Varnish is working properly:

curl -I YourIPAddressHere
root@host:/etc/apache2# curl -I YourIPAddressHere
HTTP/1.1 200 OK
Date: Tue, 04 Oct 2022 14:05:59 GMT
Server: Apache/2.4.52 (Ubuntu)
Last-Modified: Wed, 21 Sep 2022 20:51:19 GMT
Vary: Accept-Encoding
Content-Type: text/html
X-Varnish: 2
Age: 0
Via: 1.1 varnish (Varnish/6.6)
ETag: W/"119c-5e9361b575472-gzip"
Accept-Ranges: bytes
Connection: keep-alive

Congratulations! You successfully installed and configured the Varnish cache on Ubuntu 22.04

If you have any difficulties completing this setup on your own, just sign up for one of our NVMe VPS plans, and our admins will help you. You can contact us anytime you want. We are available 24/7 via live chat and support tickets.

If you liked this post about installing Varnish cache on Ubuntu 22.04, please share it with your friends on social networks or simply leave a reply below.

Leave a Comment