Tips / Nginx

How to install Varnish on Ubuntu


Today I will explain How to install Varnish on Ubuntu Linux. In just two simple steps we can have our varnish-cache server working on Ubuntu. Before start you need to move to and get the lastest stable version of Varnish, it is recommended to use the official Ubuntu repository provided in varnish-cache website.

Install Varnish on Ubuntu

Install the GPG key for the repo:

sudo curl | sudo apt-key add -

Next step is adding the repo to our apt sources lists

sudo nano -w /etc/apt/sources.list

Once you are in there, add this line to the file:

deb lucid varnish-3.0

Save your file and exit.

Now let’s update the systema install varnish:

sudo apt-get update
sudo apt-get install varnish

Varnish Configuration

Now, I will assume that you have Nginx (or Apache in the worst scenario) installed and running on port 80, right? In order to work with Varnish you will have to change your 80 listen port for another one like 8080 for example. You can do this from Nginx or Apache configuration.

Why do you need to change the port of your web server?

Because now Varnish is the one that is going to recieve all the requests on 80 port, and then it will act a as a front end proxy to pass all the requests to the webserver.

Edit varnish configuration file

sudo nano -w /etc/default/varnish

Remove all the lines starting at the line that says “DAEMON_OPTS” under Alternative 2.
At the end your configuration should look like this:

-T localhost:6082
-f /etc/varnish/default.vcl
-S /etc/varnish/secret
-s malloc,256m"

Now let’s edit the Varnish VCL file /etc/varnish/default.vcl

sudo nano -w /etc/varnish/default.vcl

At this file you will configure and tell Varnish where the webserver is located, example:

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

Once you have this configured at Varnish, the only thing left is to restart the services in order to apply changes:

/etc/init.d/varnish restart
/etc/init.d/nginx restart


/etc/init.d/apache2 restart

Test Varnish

It can be easily done from the shell using curl:

[me@localhost ~]$ curl -I
HTTP/1.1 302
Server: Varnish
Accept-Ranges: bytes
Date: Tue, 08 Apr 2014 13:32:12 GMT
X-Varnish: 1972635367
Age: 0
Via: 1.1 varnish
Connection: close

As you see, you should notice ‘Server: Varnish’ at the header.

Need more information? Check out the Varnish official website.

Popular search terms:

  • /etc/varnish/secret nginx configuration
  • Varnis lucid example

Esteban Borges

Linux Geek, Webperf Addict, Nginx Fan. CTO @Infranetworking