How to Install and Use Varnish Cache on Debian 11
Varnish Cache is an open-source, powerful, and one of the most popular HTTP accelerators used by over 3.4 million websites. It can be used as a reverse caching proxy specially designed for high-traffic dynamic websites. It is used to cache content in front of the web server. It works by caching content in memory. When a user requests a cached page, Varnish serves the cached copy instead of requesting the same page again and again from the backend server. Many VPS hosting providers use it makes as part of their technology stack to improve website performance.
This post will show you how to install the Varnish Cache with Apache on Debian 11.
Requirements
- A server running Debian 11.
- A root password is configured on your server.
Install and Configure Apache Web Server
First, you will need to install the Apache web server on your server. You can install it by running the following command:
apt install apache2 -y
After installing the Apache web server, you will need to edit the Apache configuration file and change the default port to something different.
nano /etc/apache2/ports.conf
Find the following line:
Listen 80
And, replaced it with the following line:
Listen 8080
Save and close the file when you are done.
Next, you will also need to edit the Apache default virtual host configuration file and change the default port:
nano /etc/apache2/sites-available/000-default.conf
Find the following line:
<VirtualHost *:80>
And, replaced it with the following line:
<VirtualHost *:8080>
Save and close the file then restart the Apache service to apply the changes:
systemctl restart apache2
You can now check the Apache listening port using the following command:
ss -antpl | grep apache2
You should see the following output:
LISTEN 0 511 *:8080 *:* users:(("apache2",pid=19315,fd=4),("apache2",pid=19314,fd=4),("apache2",pid=19313,fd=4),("apache2",pid=19312,fd=4),("apache2",pid=19311,fd=4),("apache2",pid=19310,fd=4))
Install Varnish Server Debian 11
By default, the latest version of the Varnish cache is not available in the Debian default repository. So you will need to add the Varnish repository to APT.
First, install the required dependencies using the following command:
apt install debian-archive-keyring curl gnupg apt-transport-https -y
Next, add the Varnish GPG key using the following command:
curl -fsSL https://packagecloud.io/varnishcache/varnish70/gpgkey | gpg --dearmor -o /etc/apt/trusted.gpg.d/varnish.gpg
Next, create a Varnish source file:
nano /etc/apt/sources.list.d/varnishcache_varnish70.list
Add the following line:
deb https://packagecloud.io/varnishcache/varnish70/debian/ bullseye main deb-src https://packagecloud.io/varnishcache/varnish70/debian/ bullseye main
Save and close the file then update the repository cache with the following command:
apt update -y
Next, install the Varnish Cache with the following command:
apt install varnish -y
Once the Varnish Cache is installed, start the Varnish server using the following command.
systemctl restart varnish
You can also verify the Varnish version with the following command.
varnishd -V
You will get the following output.
varnishd (varnish-7.0.3 revision 6a4c6a5c7e66a664b140278c209f0b18c544cab8) Copyright (c) 2006 Verdens Gang AS Copyright (c) 2006-2022 Varnish Software
Configure Varnish Server
Next, you will need to edit the default.vcl file and define your backend server:
nano /etc/varnish/default.vcl
Change the following lines as per your backend server:
backend default { .host = "127.0.0.1"; .port = "8080"; }
Save and close the file when you are finished.
Configure Varnish to Work with Apache
Next, you will need to create a custom service configuration file for Varnish. You can create it with the following command.
mkdir /etc/systemd/system/varnish.service.d
nano /etc/systemd/system/varnish.service.d/customport.conf
Add the following lines:
[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 then reload the systemd daemon with the following command:
systemctl daemon-reload
Next, restart the Varnish service with the following command:
systemctl restart varnish
You can now check the status of the Varnish Cache with the following command:
systemctl status varnish
You should get the following output:
? varnish.service - Varnish Cache, a high-performance HTTP accelerator Loaded: loaded (/lib/systemd/system/varnish.service; enabled; vendor preset: enabled) Drop-In: /etc/systemd/system/varnish.service.d ??customport.conf Active: active (running) since Fri 2022-12-30 05:31:51 UTC; 9s ago Process: 24637 ExecStart=/usr/sbin/varnishd -a :80 -a localhost:8443,PROXY -p feature=+http2 -f /etc/varnish/default.vcl -s malloc,256m (> Main PID: 24638 (varnishd) Tasks: 217 Memory: 132.4M CPU: 535ms CGroup: /system.slice/varnish.service ??24638 /usr/sbin/varnishd -a :80 -a localhost:8443,PROXY -p feature=+http2 -f /etc/varnish/default.vcl -s malloc,256m ??24652 /usr/sbin/varnishd -a :80 -a localhost:8443,PROXY -p feature=+http2 -f /etc/varnish/default.vcl -s malloc,256m Dec 30 05:31:50 debian11 systemd[1]: Starting Varnish Cache, a high-performance HTTP accelerator... Dec 30 05:31:51 debian11 varnishd[24638]: Version: varnish-7.0.3 revision 6a4c6a5c7e66a664b140278c209f0b18c544cab8 Dec 30 05:31:51 debian11 varnishd[24638]: Platform: Linux,5.10.0-20-amd64,x86_64,-junix,-smalloc,-sdefault,-hcritbit Dec 30 05:31:51 debian11 varnishd[24638]: Child (24652) Started Dec 30 05:31:51 debian11 varnishd[24638]: Child (24652) said Child starts Dec 30 05:31:51 debian11 systemd[1]: Started Varnish Cache, a high-performance HTTP accelerator.
At this point, Varnish is installed and listens on port 80. You can check it with the following command:
ss -antpl | grep :varnishd
You will get the following output:
LISTEN 0 1024 0.0.0.0:80 0.0.0.0:* users:(("cache-main",pid=24652,fd=3),("varnishd",pid=24638,fd=3)) LISTEN 0 1024 127.0.0.1:8443 0.0.0.0:* users:(("cache-main",pid=24652,fd=7),("varnishd",pid=24638,fd=7)) LISTEN 0 10 127.0.0.1:39231 0.0.0.0:* users:(("varnishd",pid=24638,fd=11)) LISTEN 0 10 [::1]:36395 [::]:* users:(("varnishd",pid=24638,fd=10)) LISTEN 0 1024 [::]:80 [::]:* users:(("cache-main",pid=24652,fd=5),("varnishd",pid=24638,fd=5)) LISTEN 0 1024 [::1]:8443 [::]:* users:(("cache-main",pid=24652,fd=6),("varnishd",pid=24638,fd=6))
Verify Varnish Cache
At this point, the Varnish cache is installed and running. You can now verify the Varnish cache using the CURL command:
curl -I http://localhost/
You will get the varnish cache in the following output:
HTTP/1.1 200 OK Date: Fri, 30 Dec 2022 05:33:00 GMT Server: Apache/2.4.54 (Debian) Last-Modified: Fri, 30 Dec 2022 04:42:33 GMT Vary: Accept-Encoding Content-Type: text/html X-Varnish: 2 Age: 0 Via: 1.1 varnish (Varnish/7.0) ETag: W/"29cd-5f1043adffc4c-gzip" Accept-Ranges: bytes Connection: keep-alive
Conclusion
Congratulations! you have successfully installed Varnish Cache with Apache on Debian 11. You can now use the Varnish cache as a front-end server to speed up the website load time. Feel free to ask me if you have any questions.