Varnish Cache is a high-performance HTTP accelerator designed for high-traffic dynamic websites. It acts as a middleman between your client and server, handling all inbound requests before they land on your web server’s backend. Varnish caches a copy of each web page served by the web server. 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. It makes the Full Page Cache by Varnish so ideal for stores with high-traffic stores and thousands of products.

This post will show you how to install the Varnish Cache and configure it with Apache on Ubuntu 22.04.

Requirements

  • A server running Ubuntu 22.04.
  • A root password is configured on your server.

Install and Configure Apache Web Server

Before starting, the Apache web server must be installed on your server. If not installed, you can install it with the following command:

apt install apache2 -y

Next, 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 then edit the Apache default virtual host configuration file:

nano /etc/apache2/sites-available/000-default.conf

Find the following line:


And replace it with the following line:


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 8080

You should see the following output:

LISTEN 0      511                *:8080            *:*    users:(("apache2",pid=2553,fd=4),("apache2",pid=2552,fd=4),("apache2",pid=2551,fd=4))

Install Varnish Cache

By default, the latest version of the Varnish cache is not available in the Ubuntu 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 | sudo 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/ubuntu/ focal main
deb-src https://packagecloud.io/varnishcache/varnish70/ubuntu/ focal main

Save and close the file then create another configuration file:

nano /etc/apt/preferences.d/varnish

Add the following lines:

Package: varnish
Pin: origin packagecloud.io
Pin-Priority: 900

Save and close the file then update the repository cache with the following command:

apt update

Next, install the Varnish Cache with the following command:

apt install varnish -y

Once the Varnish Cache is installed, 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 must create a custom service configuration file for Varnish.

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 Tue 2022-10-18 13:07:44 UTC; 14s ago
    Process: 4968 ExecStart=/usr/sbin/varnishd -a :80 -a localhost:8443,PROXY -p feature= http2 -f /etc/varnish/default.vcl -s malloc,256m (c>
   Main PID: 4969 (varnishd)
      Tasks: 217
     Memory: 90.6M
        CPU: 595ms
     CGroup: /system.slice/varnish.service
             ??4969 /usr/sbin/varnishd -a :80 -a localhost:8443,PROXY -p feature= http2 -f /etc/varnish/default.vcl -s malloc,256m
             ??4983 /usr/sbin/varnishd -a :80 -a localhost:8443,PROXY -p feature= http2 -f /etc/varnish/default.vcl -s malloc,256m

Oct 18 13:07:43 ubuntu2204 systemd[1]: Starting Varnish Cache, a high-performance HTTP accelerator...
Oct 18 13:07:44 ubuntu2204 varnishd[4969]: Version: varnish-7.0.3 revision 6a4c6a5c7e66a664b140278c209f0b18c544cab8
Oct 18 13:07:44 ubuntu2204 varnishd[4969]: Platform: Linux,5.15.0-33-generic,x86_64,-junix,-smalloc,-sdefault,-hcritbit
Oct 18 13:07:44 ubuntu2204 varnishd[4969]: Child (4983) Started
Oct 18 13:07:44 ubuntu2204 varnishd[4969]: Child (4983) said Child starts
Oct 18 13:07:44 ubuntu2204 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 :80

You will get the following output:

LISTEN 0      1024         0.0.0.0:80         0.0.0.0:*    users:(("cache-main",pid=4983,fd=3),("varnishd",pid=4969,fd=3))
LISTEN 0      1024            [::]:80            [::]:*    users:(("cache-main",pid=4983,fd=5),("varnishd",pid=4969,fd=5))
LISTEN 0      511                *:8080             *:*    users:(("apache2",pid=4749,fd=4),("apache2",pid=4748,fd=4),("apache2",pid=4745,fd=4))

Verify Varnish Cache

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: Tue, 18 Oct 2022 13:08:27 GMT
Server: Apache/2.4.52 (Ubuntu)
Last-Modified: Tue, 18 Oct 2022 13:03:09 GMT
Vary: Accept-Encoding
Content-Type: text/html
X-Varnish: 2
Age: 0
Via: 1.1 varnish (Varnish/7.0)
ETag: W/"29af-5eb4eb6b9e071-gzip"
Accept-Ranges: bytes
Content-Length: 10671
Connection: keep-alive

Conclusion

Congratulations! you have successfully installed Varnish Cache with Apache on Ubuntu 22.04. You can now use the Varnish cache in your server to speed up the website load time. Feel free to ask me if you have any questions.