Report this

What is the reason for this report?

How To Install and Configure Varnish with Apache on Ubuntu 12.04

Published on July 20, 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.

Setup

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 = "127.0.0.1";
    .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:

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
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 author

Etel Sverdlov
Etel Sverdlov
Author
See author profile

Former Director of Community at DigitalOcean. Expert in cloud topics including LAMP Stack, CentOS, Ubuntu, MySQL, SSL certificates, and more.

Still looking for an answer?

Was this helpful?
Leave a comment...

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!

  • Using phpMyAdmin without Varnish works without problems on nginx

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

Hi

in my configuration, i have 2 differents servers, apache in ip 14.15.16.17 and varnich ser with ip 12.13.14.15

what configuration should i have to run varnish with apache

default.vcl

backend default { .host = “14.15.16.17”; .port = “80”; #.thread_pool_max=1000; #.thread_pools=1; #.thread_pool_min=100; #.thread_pool_add_delay=1; .connect_timeout = 3600s; .first_byte_timeout = 3600s; .between_bytes_timeout = 3600s; }

Works like a charm!

port 8080 conflicts with tomcat

Excellent tutorial, I got it working at first try, now my local web development server is complete: VirtualBox, Zend Server Free version, MariaDB (an enhanced, drop-in replacement for MySQL…).

Thanks a lot!

Any news regarding the section about configuring SSL?

Excellent! thank you! only a steps and go!

I’m having a bit of a job installing varnish. I’m getting this warning first off.- [Fri Apr 26 09:47:50 2013] [warn] default VirtualHost overlap on port 80, the first has precedence

Any help would be most appreciated.

Thanks for the community contribution - sweet stuff.

Uhm. I had WordPress for Ubuntu droplet working. Followed these directions and now the site shows “It works!” rather than the WordPress site. Any suggestions on where I went wrong?

@layne.heiny: What’s the output of this command? <pre>ls /etc/apache2/sites-enabled</pre>

Hi,

The paths to the documents were incorrect. Once these were corrected then the site loads :)

Swift Question - Does this work on all sub-domains as well (example, a WPMultisite setup).

I’m using Wappalyzer, and it does not detect Varnish. However, when I type “varnishstat” into command line, I see numbers changing…

p.s. – Is there another way to exit out of the varnishstat screen without using alt+f4?

@Matt: No. I don’t recommend setting up Varnish on a wordpress multisite setup as the config will be very complex due to how wordpress configures the directories.

I believe you can exit out of the varnishstat screen by pressing ‘q’.

Anything I need to change in UFW / iptables because Varnish is using 8080?

I installed varnish on centos and once i do the configuratio as it is here, the domain name does not resolve since i got this error: Could not reliably determine the server’s fully qualified domain name, using #IP for ServerName xxxx

i have changed the hostname to the main domain without success and i also added in the httpd conf servername domain.com ip

nothinh works

@KiwoT: Varnish is running on port 80, you don’t need to edit any firewall rules.

@omontero: You can ignore that warning.

@Matt: No. I don’t recommend setting up Varnish on a wordpress multisite setup as the config will be very complex due to how wordpress configures the directories.”

What about Drupal? I have a virtual host created for a subdomain. Will varnish still function OK for that subdomain? There’s only one site on that subdomain/virtual host.

Hi, Somebody has installed on my Server. But I am not sure is it working or not. Because I don’t have any idea about Linux.

0+14:07:24 Hitrate ratio: 0 0 0 Hitrate avg: 0.0000 0.0000 0.0000

       2          .            .   n_sess_mem - N struct sess_mem
      10          .            .   n_wrk - N worker threads
      10         0.00         0.00 n_wrk_create - N worker threads created
       1          .            .   n_backend - N backends
   33895         2.00         0.67 shm_records - SHM records
   33895         2.00         0.67 shm_writes - SHM writes
       1         0.00         0.00 n_vcl - N vcl total
       1         0.00         0.00 n_vcl_avail - N vcl available
       1          .            .   n_ban - N total active bans
       1          .            .   n_ban_gone - N total gone bans
       1         0.00         0.00 n_ban_add - N new bans added
   50844         1.00         1.00 uptime - Client uptime
       1         0.00         0.00 LCK.sms.creat - Created locks
       2         0.00         0.00 LCK.sma.creat - Created locks
       1         0.00         0.00 LCK.hcb.creat - Created locks
     283         0.00         0.01 LCK.hcb.locks - Lock Operations
       1         0.00         0.00 LCK.vcl.creat - Created locks
       2         0.00         0.00 LCK.vcl.locks - Lock Operations
       1         0.00         0.00 LCK.stat.creat - Created locks
       2         0.00         0.00 LCK.stat.locks - Lock Operations
       1         0.00         0.00 LCK.sessmem.creat - Created locks
       1         0.00         0.00 LCK.wstat.creat - Created locks
  101873         2.00         2.00 LCK.wstat.locks - Lock Operations
       1         0.00         0.00 LCK.herder.creat - Created locks
       1         0.00         0.00 LCK.herder.locks - Lock Operations
       2         0.00         0.00 LCK.wq.creat - Created locks
  101580         2.00         2.00 LCK.wq.locks - Lock Operations
       1         0.00         0.00 LCK.exp.creat - Created locks
   50795         1.00         1.00 LCK.exp.locks - Lock Operations
       2         0.00         0.00 LCK.lru.creat - Created locks
       1         0.00         0.00 LCK.cli.creat - Created locks
   16953         1.00         0.33 LCK.cli.locks - Lock Operations
       1         0.00         0.00 LCK.ban.creat - Created locks
   50795         1.00         1.00 LCK.ban.locks - Lock Operations
       1         0.00         0.00 LCK.vbp.creat - Created locks
       1         0.00         0.00 LCK.vbe.creat - Created locks
       1         0.00         0.00 LCK.backend.creat - Created locks

268435456 . . SMA.s0.g_space - Bytes available 1 . . VBE.default(127.0.0.1,7080).vcls - VCL references

What should I do?

@mert_can2: That’s not an error, it’s just some debugging information varnish output. Are you experiencing any problems with varnish?

@benmcclanahan: Varnish should work with pretty much everything as long as you configure it correctly i.e. tell it which files/URIs it should cache and which ones it shouldn’t. Take a look at <a href=“https://drupal.org/project/varnish”>https://drupal.org/project/varnish</a>

@Kamal Nasser: Thank you very much for your answer. I think it is working very well. But I am seeing only this website on my server: km34416.keymachine.de and I don’t want this website. I want to put my an other website. Do you know how can I change it?

@mert_can2: I’m not sure what you’re referring to – can you please explain further?

I followed this guide for my server (hosted with digital ocean) and unfortunately I get a 404 whenever varnish is running. If I put apache back to 80, and it’s all fine. Can anyone shed any light on this? I’m pretty good with apache, but Varnish is completely new to me.

Thanks!

@Kev: Are you still experiencing this issue?

Hello I followed this guide for my server, but i have 403 error (Forbidden) in all sites… we can hel-me?

@eng.moreira: Does this still happen or have you managed to fix it?

I know I’m just leaving another “This is awesome, thank you!”… but thank you!

Great tutorial thanks. Just installed my first web server running on Ubuntu and added Varnish to it

I followed the tutorial exactly, but now my site says “It Works!”

I tried the command “ls /etc/apache2/sites-enabled” and get my correct domain name, however I have not changed DNS to this host, so still using the IP address. Any way to fix? thanks!

@robjr: Until you configure DNS, try editing your virtualhost to listen for the IP (ServerName 1.2.3.4) instead of the actual domain name and see if that works.

I followed this tutorial a couple of times and I keep getting the error

“varnishstat: error while loading shared libraries: libvarnishapi.so.1: cannot open shared object file: No such file or directory” and if I try to go the the web page I get “error 404: file not found”

Any ideas what I messed up?

@grozanc: Try running <pre>sudo ldconfig</pre>

I tried running “sudo ldconfig” didn’t help. Also, when I try to find the “libvarnishapi.so” file I can’t find it.

@grozanc: I’m receiving the same error while trying to setup varnish.

I also tried “sudo ldconfig” and it didn’t help.

@grozanc: I solved it by running “sudo apt-get install libvarnish-dev”

@jhaile - thanks for your solution - like @grozanc I was stumped by this error having followed the instructions above. I searched on Google and didn’t find any solutions, came back to this page and read your comment and it’s all working now (at least I don’t get the error any longer) :)

My domain wont open and I’m getting this error log <pre> File does not exist: /etc/apache2/htdocs </pre>

Creative CommonsThis work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.
Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.