How To Install and Configure Varnish with Apache on Ubuntu 12.04

Published on July 19, 2012
How To Install and Configure Varnish with Apache on Ubuntu 12.04

Status: Deprecated

This article covers a version of Ubuntu that is no longer supported. If you are currently operate a server running Ubuntu 12.04, we highly recommend upgrading or migrating to a supported version of Ubuntu:

Reason: Ubuntu 12.04 reached end of life (EOL) on April 28, 2017 and no longer receives security patches or updates. This guide is no longer maintained.

See Instead:
This guide might still be useful as a reference, but may not work on other Ubuntu releases. If available, we strongly recommend using a guide written for the version of Ubuntu you are using. You can use the search functionality at the top of the page to find a more recent version.

About Varnish

Varnish is an HTTP accelerator and a useful tool for speeding up a server, especially during a times when there is high traffic to a site. It works by redirecting visitors to static pages whenever possible and only drawing on the virtual private server itself if there is a need for an active process.


To perform the steps in this tutorial, you will need to both have a user with sudo privileges and apache installed on your virtual private server.

To create a user with sudo privileges, go through the third and fourth steps of the initial ubuntu server setup tutorial

Apache can be installed on your VPS with a single command from the apt-get repository.

sudo apt-get install apache2

Step One—Install Varnish

The varnish site recommends installing the varnish package through their repository.

You can start that process by grabbing the repository:

sudo curl http://repo.varnish-cache.org/debian/GPG-key.txt | sudo apt-key add -

The next step is to add the repository to the list of apt sources. Go ahead and open up that file.

sudo nano /etc/apt/sources.list

Once inside the file, add the varnish repository to the list of sources.

deb http://repo.varnish-cache.org/ubuntu/ lucid varnish-3.0

Save and exit.

Finally, update apt-get and install varnish.

sudo apt-get update
sudo apt-get install varnish

Step Two—Configure Varnish

Once you have both apache and varnish installed, you can start to configure them to ease the load on your server from future visitors.

Varnish will serve the content on port 80, while fetching it from apache which will run on port 8080.

Let’s go ahead and start setting that up by opening the /etc/default/varnish file:

sudo nano /etc/default/varnish

Uncomment all of the lines under “DAEMON_OPTS”—under Alternative 2, and make the configuration match the following code:

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

Once you save and exit out of that file, open up the default.vcl file:

sudo nano /etc/varnish/default.vcl

This file tells varnish where to look for the webserver content. Although Apache listens on port 80 by default, we will change the settings for it later. Within this file, we will tell varnish to look for the content on port 8080.

The configuration should like this:

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

Step Three—Configure Apache

So far we have told varnish that apache ports will be running on 8080. However the default settings for apache are still on port 80. We will correct the discrepancy now. Open up the apache ports file:

sudo nano /etc/apache2/ports.conf

Change the port number for both the NameVirtualHost and the Listen line to port 8080, and the virtual host should only be accessible from the localhost. The configuration should look like this:


Change these settings 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, and updated line looks like this:


Save and exit the file and proceed to restart Apache and Varnish to make the changes effective.

sudo service apache2 restart
sudo service varnish restart

Accessing your domain should instantly take you to the varnish cached version, and you can see the details of varnish’s workings with this command:

By Etel Sverdlov

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Great article guys, with this approach however, when you submit secure forms, or perform a redirection, Apache redirects you to http://yoursite:8080/some/route

And your visitors will get a server not found error.

We ended up using the following approach Varnish bind to the node’s ip and Apache bind to Both on port 80. That solved the :8080 redirection issue.

Here is how we solved the Apache :8080 redirection issue, that takes place if you use port 80 for Varnish and 8080 for Apache. On an Ubuntu 12.04 server

edit Apache’s ports config

sudo nano /etc/apache2/ports.conf

change port 8080 to 80, should look like this .


edit varnish config

sudo nano /etc/default/varnish

modify alternative 2, add your node’s ip (the one running Varnish and Apache), make sure you use port 80, should look like this.

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

edit varnish default.vcl

sudo nano /etc/varnish/default.vcl 

make sure you use port 80, should look like this

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

finally, edit your virtual host

sudo nano /etc/apache2/sites-available/default

make sure you use port 80


restart both services

sudo service varnish restart && sudo service apache2 restart

Hope this helps somebody.

the last command should be : sudo varnishstat

I’m using ubuntu 16 , and having link problems:

http://example.com/test is redirecting to http://example.com:8080/test and returning error :|

Just a heads up that the newer version of Apache and Linux seem to store the default virtual host file at /etc/apache2/sites-available/000-default.conf instead of /etc/apache2/sites-available/default.

Question about the configuration:

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

.host = MY_SERVER_IP instead rite ?

What If I want to use it on Specific vhost only?

I can install Varnish after complete all installation of my VPS? Apache, mysql, php, wordpress is already workings… I’m having a big problem that my db connection crashes, at my last host I never see this problem.

I complete all installation yesterday, but the db connection problem is intermittent… This can help me?


just tried this guide and it wouldn’t work at first. Visiting the Varnish site shows the install command as:

sudo curl https://repo.varnish-cache.org/GPG-key.txt | apt-key add -

so, the addition of the secure https request might be a change at their end. It wouldn’t work for me until I did the secure request. Secondly, using sudo as the admin user didn’t allow me to do the https request. I got the following error:

“ERROR: This command can only be used by root”

so I had to do “sudo su” to become root before I could run the command. Hope that helps. If I’m correct, you might need to update the article,



Accept-Ranges bytes Age 0 Cache-Control no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Connection keep-alive Content-Encoding gzip Content-Length 30206 Content-Type text/html; charset=UTF-8 Date Tue, 08 Dec 2015 06:49:48 GMT Expires Thu, 19 Nov 1981 08:52:00 GMT Pragma no-cache Server Apache/2.4.7 (Ubuntu) Set-Cookie frontend=74u6554745mg4p1eeq2jiqkqn7; expires=Wed, 09-Dec-2015 06:49:45 GMT; Max-Age=86400; path=/; domain=kk.local.com; httponly Vary Accept-Encoding Via 1.1 varnish X-Frame-Options SAMEORIGIN X-Powered-By PHP/5.6.15-1+deb.sury.org~trusty+1 X-Varnish 1552280728

varnish hits not showing

This is very helpful to boost the wordpress on VPS but many step is not good with lastest version.

By default after install VARNISH it work well and is not on port 80. (no need to do STEP THREE) and another.

BY THE WAY VARNISH IS AMAZING !!! In my case it take around 10 sec to open website and now it’s CMS normal speed around 1-2 sec :) I’M REALLY HAPPY NOW :D !


Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel