Install Varnish Cache 5.2 for Apache on Debian and Ubuntu

Varnish Cache (also called Varnish) is an open source, high-performance HTTP accelerator with a modern design. It stores the cache in memory ensuring that web server resources are not wasted in creating the same web page over and over again when requested by a client.

It can be configured to run in front of a web server to serve pages in a much faster way thus making websites load quickly. It supports load balancing with health checking of backends, URL rewriting, graceful handling of “dead” backends and offers partial support for ESI (Edge Side Includes).

Important: If you are using Debian, install debian-archive-keyring package for verifying the official Debian repositories.

$ sudo apt-get install debian-archive-keyring

2. After that, create a file named /etc/apt/sources.list.d/varnishcache_varnish5.list that contains the repository configuration below. Make sure to replace ubuntu and xenial with your Linux distribution and version.

You need to modify the service directive ExecStart, it defines the varnish daemon runtime options. Set the value of the -a flag, which defines the port varnish listens to, from 6081 to 80.

Configure Varnish on Systemd

8. To effect the above changes to the varnish service unit file, run the following systemctl command:

$ sudo systemctl daemon-reload

9. Then, configure Apache as a backend server for Varnish proxy, in the /etc/varnish/default.vcl configuration file.

# sudo vi /etc/varnish/default.vcl

Using the backend section, you can define the host IP and port for your content server. The following is the default backend configuration which uses the localhost (set this to point to your actual content server).

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

Configure Varnish Cache

10. Once you have performed all the above configuration, restart Apache and Varnish daemon by typing following commands.

Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

If you want Varnish to run in front of the website, for web caching then you need to make it listen on the default HTTP port even in production environment. However, this is critical, therefore you should decide what works best for you: in-depth consultations(possibly from experts) can help you make the best decision.