Install and Configure Varnish Cache with Apache on Debian 11

In this tutorial, we want to teach you How To Install and Configure Varnish Cache with Apache on Debian 11.

Varnish Cache is an open-source web application accelerator (also referred to as an HTTP accelerator or caching HTTP reverse proxy). Varnish stores (or caches) files or fragments of files in memory; this enables Varnish to reduce the response time and network bandwidth consumption on future, equivalent requests. Unlike web servers like Apache and Nginx, Varnish was designed for use exclusively with the HTTP protocol.

Install and Configure Varnish Cache with Apache on Debian 11

To install Varnish with Apache, you need some requirements.

Requirements for installing and configuring Varnish

First, you must log in to your server as a non-root user with sudo privileges. To do this, you can follow our guide the Initial Server Setup with Debian 11.

Then, you need to have an Apache web server installed on your server. To do this, you can check our guide How To Install Apache Web Server on Debian 11.

Now follow the steps below to complete this guide.

Install Varnish Cache on Debian 11

First, you need to install debian-archive-keyring to verify the authentic Debian repositories with the command below:

sudo apt install debian-archive-keyring

Then, install the required packages with the following command:

sudo apt install curl gnupg apt-transport-https

Next, install the GPG key that is used to sign the metadata in the repository:

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

At this point, you need to create a file varnishcache_varnish.list in /etc/apt/sources.list.d directory with the following command:

sudo tee /etc/apt/sources.list.d/varnishcache_varnish.list<<EOL 
deb https://packagecloud.io/varnishcache/varnish70/debian/ bullseye main 
deb-src https://packagecloud.io/varnishcache/varnish70/debian/ bullseye main 
EOL

Update your local package index:

sudo apt update

Now you can install Varnish on Debian 11 by running the below command:

sudo apt install varnish

Verify your Varnish Cache installation by checking its version:

sudo varnishd -V
Output
varnishd (varnish-7.0.2 revision 9b5f68e19ca0ab60010641e305fd12822f18d42c)
Copyright (c) 2006 Verdens Gang AS
Copyright (c) 2006-2021 Varnish Software

Varnish Cache 7 failed to start at some point, to solve the error edit /lib/systemd/system/varnish.service and replace localhost with 127.0.0.1.

...
ExecStart=/usr/sbin/varnishd \ 
          -a :6081 \ 
          -a 127.0.0.1:8443,PROXY \ 
          -p feature=+http2 \ 
          -f /etc/varnish/default.vcl \ 
          -s malloc,256m 
ExecReload=/usr/sbin/varnishreload
...

When you are done, save and close the file.

Reload the Daemon:

sudo systemctl daemon-reload

At this point, start and enable the Varnish cache with the commands below:

# sudo systemctl start varnish 
# sudo systemctl enable varnish

Verify that your Varnish Cache service is active and running on your Debian 11:

sudo systemctl status varnish
Output
● varnish.service - Varnish Cache, a high-performance HTTP accelerator
Loaded: loaded (/lib/systemd/system/varnish.service; enabled; vendor prese>
Active: active (running) since Mon 2022-06-20 03:25:51 EDT; 7min ago
Main PID: 4221 (varnishd)
Tasks: 217
Memory: 122.7M
CPU: 882ms
CGroup: /system.slice/varnish.service
├─4221 /usr/sbin/varnishd -a :6081 -a localhost:8443,PROXY -p feat>
└─4235 /usr/sbin/varnishd -a :6081 -a localhost:8443,PROXY -p feat>

Configure Apache to work with Varnish Cache

Here you need to edit the /etc/apache2/ports.conf file. Open the file with your favorite text editor, here we use vi:

sudo vi /etc/apache2/ports.conf

Find the Listen directive and change it to 8080:

...
Listen 8080
...

Restart Apache to apply the changes:

sudo systemctl restart apache2

Configure Varnish Cache Server

At this point, you need to edit the Varnish configuration file. Open the file with your favorite text editor, here we use vi:

sudo vi /etc/systemd/system/multi-user.target.wants/varnish.service

Set the Listen port to 80:

ExecStart=/usr/sbin/varnishd \

-a :80 \
...

When you are done, save and close the file.

The varnish systemd service should then be restarted.

# sudo systemctl daemon-reload 
# sudo systemctl restart varnish

Confirm your Varnish port:

sudo systemctl status varnish
Output
● varnish.service - Varnish Cache, a high-performance HTTP accelerator
Loaded: loaded (/lib/systemd/system/varnish.service; enabled; vendor prese>
Active: active (running) since Mon 2022-06-20 03:42:45 EDT; 8s ago
Process: 8574 ExecStart=/usr/sbin/varnishd -a :80 -a 127.0.0.1:8443,PROXY ->
Main PID: 8575 (varnishd)
Tasks: 217
Memory: 130.4M
CPU: 727ms
CGroup: /system.slice/varnish.service
├─8575 /usr/sbin/varnishd -a :80 -a 127.0.0.1:8443,PROXY -p featur>
└─8590 /usr/sbin/varnishd -a :80 -a 127.0.0.1:8443,PROXY -p featur>

Finally, we use the curl command to see if the Varnish cache is activated and working with Apache:

curl -I http://localhost
Output
HTTP/1.1 200 OK
Date: Mon, 20 Jun 2022 07:50:13 GMT
Server: Apache/2.4.53 (Debian)
Last-Modified: Mon, 20 Jun 2022 07:18:05 GMT
Vary: Accept-Encoding
Content-Type: text/html
X-Varnish: 2
Age: 0
Via: 1.1 varnish (Varnish/7.0)
ETag: W/"29cd-5e1dbe7b5d589-gzip"
Accept-Ranges: bytes
Connection: keep-alive

If you rerun the command, the Varnish cached response will appear showing the page Age header:

curl -I http://localhost
Output
HTTP/1.1 200 OK
Date: Mon, 20 Jun 2022 07:50:13 GMT
Server: Apache/2.4.53 (Debian)
Last-Modified: Mon, 20 Jun 2022 07:18:05 GMT
Vary: Accept-Encoding
Content-Type: text/html
X-Varnish: 5 3
Age: 14
Via: 1.1 varnish (Varnish/7.0)
ETag: W/"29cd-5e1dbe7b5d589-gzip"
Accept-Ranges: bytes
Content-Length: 10701
Connection: keep-alive

Conclusion

At this point, you learn to Install and Configure Varnish Cache with Apache on Debian 11.

I hope you enjoy using it.

Also, you may be interested in these articles:

Varnish Cache with Nginx on Rocky Linux 9

Install Varnish Cache with Nginx on Ubuntu 22.04

How to Install Varnish Cache with Apache on AlmaLinux 9

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!