Install Varnish Cache 5.1 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).
In this article, we will explain how to install and configure Varnish Cache 5 as a front-end to Apache HTTP server on a Debian and Ubuntu systems.
- A Ubuntu system installed with LAMP Stack
- A Debian system installed with LAMP Stack
- A Debian/Ubuntu system with static IP address
Step 1: Install Varnish Cache on Debian and Ubuntu
1. Unluckily, there are no pre-compiled packages for latest version of Varnish Cache 5 (i.e 5.1.2 at the time of writing), so you need to build it from its source files as shown below.
First install the dependencies for compiling it from source using the apt command like this.
$ sudo apt install python-docutils libedit-dev libpcre3-dev pkg-config automake libtool autoconf libncurses5-dev libncurses5
2. Then download Varnish and compile it from source as follows.
$ wget https://repo.varnish-cache.org/source/varnish-5.1.2.tar.gz $ tar -zxvf varnish-5.1.2.tar.gz $ cd varnish-5.1.2 $ sh autogen.sh $ make $ sudo make install $ sudo ldconfig
3. After compiling Varnish Cache from source, the main executable will be installed as /usr/local/sbin/varnishd. To confirm that the Varnish installation was successful, run the following command to see the version.
$ /usr/local/sbin/varnishd -V
Step 2: Configure Apache to Work With Varnish Cache
4. Now you need to configure Apache to work with Varnish Cache. By default Apache listens on port 80, you need change the default Apache port to 8080 to enable it run behind Varnish caching.
So open the Apache ports configuration file /etc/apache2/ports.conf and find the line listen 80, then change it to listen 8080.
Alternatively, just run the sed command to change port 80 to 8080 as follows.
$ sudo sed -i "s/Listen 80/Listen 8080/" /etc/apache2/ports.conf
5. Once you have changed port, restart Apache and Varnish daemon manually by typing following command instead of calling systemctl start varnish, since certain configurations are not in place when its installed from source.
$ sudo systemctl restart apache $ sudo /usr/local/sbin/varnishd -a :80 -b localhost:8080
Step 3: Test Varnish Cache on Apache
6. Finally, test if Varnish cache is enabled and working with the Apache HTTP server using the cURL command below to view the HTTP header.
$ curl -I http://localhost
That’s It! For more information about Varnish Cache, visit – https://github.com/varnishcache/varnish-cache
In this tutorial, we have explained how to setup Varnish Cache 5.1 for Apache HTTP server on Debian and Ubuntu systems. You can share any thoughts or queries with us via the feedback from below.