July 7, 2017
14 min read time

Varnish Web Developer Wiki Highlights: Installing Varnish on Ubuntu


It doesn't require much introduction, as the idea is completely straightforward. As a part of the Varnish Wiki project, designed to help you do whatever you need to do with Varnish and share your knowledge and experience of putting Varnish into practice, we've put together a series of resources that are open to everyone.

This particular reference has been prepared for web developers to get you started with your Varnish installation and configuration on Ubuntu/UNIX. Ready? Let's get started.

Installing and configuring Varnish

The following text discusses how to configure your web server to use Varnish. Note that the installation is different for systemv and systemd. The following guide is for systemd as many Linux distributions are now adapting to the systemd init system.

Step 1 : Installing Varnish on Ubuntu/UNIX:

It is recommended that you install the Varnish package from its repository.

  • Start by grabbing the repository
  • Add the repository to the source list and save
sudo curl http://repo.varnish~cache.org/debian/GPG~key.txt | sudo apt~key add ~

sudo nano /etc/apt/sources.list

deb http://repo.varnish~cache.org/ubuntu/ trusty varnish~4.1
  • Run update and install
sudo apt~get update
sudo apt~get install varnish

Step 2: Configure Varnish

Varnish comes with two configuration files:

One with the starter parameter:


This file contains all the starter parameters.

The other is the default VCL file:

For systemd, the VCL file is directed in a different manner. It will be located in:


which will point to:


This default.vcl contains the default policies that the user includes. It also tells Varnish where to find the web content. However, there is a builtin.vcl that is always appended to the VCL you define/specify in this default.vcl.

  1. Modify Varnish config file
  • Open /etc/default/varnish in a text editor
  • You will see a code like the one below

DAEMON _OPTS="-a :80\
  -T localhost:6082
  -f /etc/varnish/default.vcl
  -s malloc,256m"
  -S /etc/varnish/secret


~T : refers to which port manages this.

~f : refers to the other configuration file containing all the default policies. If you plan to change the name of the default policy file, be sure to come here and change the default.vcl to the correct name.

~S : refers to the file containing private information, such as passwords, etc. also known as the shared-secret file.

~s : refers to the space Varnish Cache is allocated. 256m” is decided based on the current server’s RAM of 1GB.

  • Set the Varnish listen port to 80
  • Replace the -f line with -b as shown below
DAEMON_OPTS="-a :80 \
  -T localhost:6082 \
  -b \
  -S /etc/varnish/secret \
  -s malloc,256m"

These are all the configuration changes required in this file.

  1. Copy the default file named varnish.service

cp /lib/systemd/system/varnish.service /etc/systemd/system/

Edit /etc/systemd/system/varnish.service

Locate the line containing port 80 and change it to 8080

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

Note that this file points to the /etc/varnish/default.vcl file.

  1. Now modify the default.vcl file

This file contains configuration that points to the content. This is by default set to serve at 8080 and points to host as localhost as shown below.

To minimally configure Varnish:

  • Back up default.vcl
cp /etc/varnish/default.vcl /etc/varnish/default.vcl.bak
  • Open /etc/varnish/default.vcl in a text editor.
  • Locate the following piece of code:
backend default {
       .host = “”;
       .port = “80”;

The value of .host is localhost by default. It should be replaced with the fully qualified host name or IP address (typically a web server) and listen port of the Varnish backend or origin server; that is, the server providing the content Varnish will accelerate.

The value of .port should be replaced with the web server’s listening port, for example 8080 as shown below.

backend default {
       .host = “<your_webserver>”;
       .port = “8080”;

It is recommended that if changes are made to these files, they should be copied and renamed, because when Varnish updates, it will replace any changes made with the new default.vcl and Varnish files.

Varnish is now serving the client at port 80 and listening to the backend at port 8080.

This is when you can add ready-made VCL templates or recommended plugins for your web application (WordPress, Drupal, Magento2)

But before you add any new code you must understand VCL.

Step 3: Configure Apache2 to work with Varnish

Configure your web server to listen on a port other than the default port 80 because Varnish responds directly to incoming HTTP requests from the client on this port.

Varnish will communicate on a different port with your backend web servers. In the example above, it is port 8080.

In the sections that follow, we use port 8080 as an example, as shown above. If you have more than one backend, you can put them on another port, such as port 8081, 8082, etc.

To change the Apache listen port:

~ Open /etc/apache2/ports.conf in a text editor

~ Locate the listen directive

~ Change the value of the listen port to 8080 (you can use any available listen port)

~ Save your changes to ports.conf and exit the text editor

~ Also edit /etc/apache2/sites-available/000-default.conf

~ Change the VirtualHost port to 8080:


Setting up multiple backends (skip this section if you have one backend)

If you have more than one backend server, you need to add all these backends into the default.vcl file as backends and also define how and when each of them should be accessed.

Here is a simple example:

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

        #these are needed if you also want to do load balancing for your servers
        .max_connections = 250;
        .connect_timeout = 500s;
        # there are more parameters you can add here, check out the Reference.

backend server2 {
        .host = server2;
        .port = "8081";

sub vcl_rec {
  if (req.url ~ "^/server1") {
          set req.backend_hint = server1;
      } else {
          set req.backend_hint = server2;

Step 4: Restart

It is always required that you restart all services once changes are made in configuration files.

service varnish restart
service apache2 restart

Step 5: Testing


http -p Hh localhost

You can also use varnishtest to test your backend as shown below.

varnishtest "Apache as Backend"

varnish v1 -arg "-b" -start

client c1 {

  expect resp.http.server ~ "Apache"
  expect resp.http.via ~ "varnish"
} -run

Replace your backend ip-address and port number.

Step 6: Troubleshooting

If Varnish fails to start, try running it from the command line as follows:

varnishd ~d ~f /etc/varnish/default.vcl

This should display any error messages.

Step 7: The management interface

Varnish has a command line interface (CLI) to control any Varnish instance. It can be used to:

  • Reload VCL without restarting
  • Start/stop cache process
  • Change configuration parameters without restarting
  • View up-to-date documentation for parameters, etc.
  • Implement a list of management commands in the varnishadm (varnishadm establishes a connection to the Varnish deamon varnishd)
Are you ready for more fun with Varnish? Take a look at and contribute to the Varnish Wiki.
Check out the Varnish Wiki