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.
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
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
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 = "127.0.0.1"; .port = "8080"; }
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:
NameVirtualHost 127.0.0.1:8080 Listen 127.0.0.1:8080
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:
<VirtualHost 127.0.0.1:8080>
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:
varnishstat
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
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!
Before each step backup your config files.
Great article. Thank you.
Glad you liked it, thank you!
A section about handling SSL would be handy as well – listening on 443, etc.
That’s a great suggestion! I’ll add it to the article queue and get it posted =]
If I go to my site (http://domain.com/) and click on the top link for the forum, the browser takes some tme and then takes me to http://domain.com:8080/forum/ (Its broken)
Installing Varnish should not break the links on the site. I would very much recommend double checking your apache and varnish configuration to make sure it matches the details the article above.
Additionally, if you like, you can upload your configuration to pastebin or some other paste site and put up a link so that we can take a look at it.
Hey there! Thanks for your help. I installed varnish with ease thanks to you ;) What bothers me now is that I have the same problem as wisnu when I log into phpMyAdmin. It takes me to http://xx.x.x.x:81/index.php?token=xxxxx But when I remove the port, it works just fine~ I fear that this problem might occur on a homepage later on (as the server is going to host a few) I use the default Varnish logic (btw… do you have some improvements you can share?) inside the VCL.
Thanks in advance!
Hi Marco, if removing the port number corrects the problem that you are experiencing with phpMyAdmin, you may be able to simply include a rewrite rule in the .htaccess file that directs visitors to the site to another page that does work.
If you want to find a more permanent solution, you may need to find out what is listening on port 81, which is probably apache, and set up the correct redirect rule and use php absolute paths. You can find more info about it here:
https://johan.pp.se/2010/09/11/varnish-and-phpmyadmin
my server’s ip adress is, 192.81.210.107, should I use this one instead 127.0.0.1 or leave it like this ? I typed 192.81.210.107 instead 127.0.0.1 and it does not work right now