Install & Configure Varnish Cache with Apache on Debian 9

Try it in our public cloud & Get $5 Credit
CLAIM NOW

Install & Configure Varnish Cache with Apache on Debian 9

Varnish Cache is an often used but not commonly known accelerator designed specifically for use with Hypertext Transfer Protocol, known by the abbreviation HTTP. As an HTTP accelerator, Varnish Cache works by taking information or data, storing it in a virtual memory cache, and then allowing the operating system to decide if information needs to be moved to more traditional storage. This alleviates the condition where the operating system continues to try and store tons of data on the disk itself. In practical use, Varnish Cache helps load web pages quicker when requested by a user.

configure varnish cache

Varnish Cache is open-source, meaning the software belongs to a family of free software licenses, allowing use and distribution with minimal restriction.

Getting Started

To install and configure Varnish Cache, a node with an updated Linux Debian 9 installation is required. Debian 9, sometimes called Debian Stretch, is a specific Linux-based operating system we’re using for this installation. Additionally, the node you choose may be cloud-based, or it can be hosted on a dedicated server.

Install and Configure Varnish Cache with Apache on Debian 9

Now that you’re chosen your node and have confirmed your node has Linux Debian 9 installed, we can setup Varnish Cache and Apache.

Installing Apache

Before we can install and configure Varnish Cache, we need to install Apache on your node. The first step in that process is updating the Apache package and the operating system:
apt update
apt upgrade

After the package is downloaded and the operating system is updated, install Apache 2.4:
apt -y install apache2

When the installation completes, it’s time to start and enable the Apache service:
systemctl enable apache2 && systemctl start apache2

If you have UFW enabled, you can utilize the commands below. If you do not have UFW enabled, skip the commands below:
ufw allow 80/tcp
ufw allow 443/tcp
ufw reload

Installing and Configuring Varnish Cache

With the Apache installation complete, it’s time to move onto Varnish Cache.

The first step is to download the GPG key and then add it:
curl -L https://packagecloud.io/varnishcache/varnish5/gpgkey | sudo apt-key add -

Next, the package repository requirements need to be installed:
apt install -y debian-archive-keyring apt-transport-https

Once complete, the repository needs to be added to the source list:
echo "deb https://packagecloud.io/varnishcache/varnish5/debian/ stretch main" > /etc/apt/sources.list.d/varnishcache5.list

The package manager will also need to be updated so that the new repository can be added:
apt update

Now that the repository has been installed and the necessary components are updated, Varnish Cache may be installed:
apt install -y varnish

With any installation, once complete, it’s important to check that the installed version matches what you anticipated. Check that Varnish Cache is installed and the proper version was installed:
varnishd -V

After confirming your installation, we can configure Varnish Cache to listen to port 80 by editing the following file, replacing port 6081 by port 80:
nano /lib/systemd/system/varnish.service

From:
ExecStart=/usr/sbin/varnishd -a :6081 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m

To:
ExecStart=/usr/sbin/varnishd -a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m

Once the file has been edited, we need to reload the system daemon to update the file:
systemctl daemon-reload

When the reload is complete, the default configuration needs to be modified, telling the system where to redirect the received Varnish Cache requests:
nano /etc/varnish/default.vcl

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

Apache 2.4 will also need a slight modification, enabling it to listen on port 8080 instead of port 80:
nano /etc/apache2/ports.conf
Listen 8080

nano /etc/apache2/sites-enabled/000-default.conf
<VirtualHost *:8080>

Once the modification has been made, restart Apache 2.4 and Varnish Cache. When the restart completes, enable Varnish Cache:
systemctl restart apache2
systemctl restart varnish
systemctl enable varnish

Now it’s time to test Varnish Cache and Apache 2.4, ensuring both are functioning as expected:
curl -I http://127.0.0.1
HTTP/1.1 200 OK
Server: Apache/2.4.25 (Debian)
Vary: Accept-Encoding
Content-Type: text/html
X-Varnish: 2
Age: 0
Via: 1.1 varnish (Varnish/5.1)
ETag: W/"29cd-55771150d08a7-gzip"
Accept-Ranges: bytes
Connection: keep-alive

If necessary, the Varnish Cache stats can be checked using the following command:
varnishstat

Conclusion

Congratulations, you’ve successfully installed Apache 2.4 and the installed and configured Varnish Cache on your Linux Debian 9 node. This installation allows you to streamline your web page distribution, allowing users to access information quicker and more efficiently than before. If you found this guide helpful during your setup, or if you know of anyone else attempting the same installation, please share this with them.