Install Varnish 4.1 on EL6/7 and Ubuntu Trusty
You’ve heard of this Varnish thing and want to give it a quick go?

Or maybe you do development and need to install several Varnish versions on different machines and make this routine faster?

During my ordinary day at work I install Varnish Cache several times on different machines and, although I’m pretty used to doing it, it can happen that I make simple mistakes as I try to install a Trusty package on EL7.

Here’s the ultimate simple guide for anyone who needs to install and get started with Varnish.

What do we want to achieve at the end of this tutorial?
[1]At the first request we will expect something like this:


[2]At the second request we will get something like this:


Now that I’ve spoiled the final result we can start with the tutorial!

Step one - Get yourself a backend
We need a backend that Varnish can use to request content from, any type of web server is okay. For this tutorial we will install Apache2.

If you are on EL6-EL7:  sudo yum install httpd

If you are on Ubuntu Trusty: sudo apt-get install apache2

Step two - Install Varnish Cache
Varnish Cache can be installed either from package or it can be built from source.
Packages are here:, while the source code lives on github:

We will install Varnish from packages because it is faster, and it is also the most common way to install it.

The procedure is the same for Ubuntu Trusty and Redhat, you'll have to type from command line:

  • curl -s | sudo bash

Choosing "" if you are on Ubuntu or "" if you are on RedHat.

Please note you need sudo permissions to complete the installation.

Step three - Configure Varnish
At this point we have both Varnish and a backend installed, now we need those two to be able to “talk” to each other and Varnish to listen to what the clients are requesting.

Varnish will serve content on port 80 while Apache will listen on port 8080.

There are two files we want to modify for the Varnish side of the things: one is the usual configuration file, containing daemon options, while the other one is the VCL file.

Configuration file:
If you are on EL6 it is under /etc/sysconfig/varnish.
If you are on EL7 it is under /etc/varnish/varnish.params.
If you are on Ubuntu Trusty it is under /etc/default/varnish.

On EL6-EL7:
Open the configuration file and change this line: VARNISH_LISTEN_PORT=6081 to VARNISH_LISTEN_PORT=80.

On Ubuntu Trusty:
Open the configuration file using your preferred text editor and use the alternative 2 (the 
configuration file provides 4 different alternatives you can use and modify based on your needs)

The configuration has to match with this one:
DAEMON_OPTS="-a :80 \
            -T localhost:6082 \
            -f /etc/varnish/default.vcl \
            -S /etc/varnish/secret \
            -s malloc,256m"

Let me explain:

  • varnish now listens on port 80 (the default value for `-a` is 6081)
  • varnishadm, which is the administration console, can be reached on localhost at port 6082
  • /etc/varnish/default.vcl is the VCL that is going to be used by Varnish once we start it
  • /etc/varnish/secret is where the secret file, used as key for the communication between manager process and child process, is stored
  • malloc, 256m indicates Varnish will use a malloc storage of 256Mb (you might want to increase it once your Varnish gets into a real production environment)

Now, let’s change the /etc/varnish/default.vcl file.

This file defines the cache policy we want Varnish to apply and also which backend(s) Varnish should talk to.

For this tutorial Varnish will communicate to a single backend(Apache2) which ip is `localhost` and listens on port 8080.

Your backend declaration in your VCL file should be like this:

vcl 4.0;

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

Note that those 5 lines are enough to allow Varnish to start and make it cache as it should because even if you don’t define your cache policy the builtin.vcl is always appended to the VCL you specify.

Step four - Configure Apache2
By default Apache listens on port 80, but we want to change it to 8080 to make it cooperate with Varnish.

Let’s open /etc/apache2/ports.conf(/etc/httpd/conf/httpd.conf for EL6/EL7) and change the port number for both the NameVirtualHost and the Listen line.

The configuration should look like this:

For Ubuntu Trusty let’s change the port value in the default virtual host file as well:
sudo nano /etc/apache2/sites-available/default

The Virtual Host should also be set to port 8080:

Step five - Testing
It is all set, both backend and Varnish are well configured, now we need to restart Apache and Varnish to make the changes take effect:

On EL6/EL7:
sudo service httpd restart
sudo service varnish restart

On Ubuntu Trusty:
sudo service apache2 restart
sudo service varnish restart

At this point we expect Varnish to handle every incoming request. To test that everything is working as expected we (as clients) can issue a request that has to go through Varnish.

From your terminal it is enough to:
curl -I http://localhost:80

The first request will be a MISS[1] and Varnish will need to go to the backend to retrieve the requested content for the first time, but if we do another curl request, it will now be a HIT because the requested resource is already present in cache[2].

You can continue to fire requests against Varnish and use varnishlog to understand better how they are handled by Varnish.

Once you feel more comfortable you can change your VCL file and play around with other Varnish tools such as varnishstat, varnishhist, varnishtop and varnishtest.

Table for fast Varnish/Apache configuration:





Ubuntu Trusty


Listens on port 80





Uses backend localhost:8080





Listens on port 8080