Install & Configure Varnish on Ubuntu Lucid Linux

I decided to install Varnish on an Ubuntu 10.04 Lucid Linux machine.

  • 1. Install Varnish
  • 2. Edit the /etc/varnish/default.vcl file
  • 3. Edit the /etc/default/varnish file
  • 4. Edit apache settings
  • 5. Restart Apache and Varnish
  • 6. Useful commands and information

1. Install Varnish

sudo apt-get install varnish

2. Edit the /etc/varnish/default.vcl file

This is where we tell varnish where to find content. I want varnish to find content on the local machine on port 8080, or basically where to find the webserver. By default apache listens on port 80 so we will need to change the apache settings later on. Edit the /etc/varnish/default.vcl file and change the contents as shown.

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

3. Edit the /etc/default/varnish file

This file contains varnish’s startup settings. The default set up is mostly fine. The main thing here is to tell varnish which port to listen on and how much RAM it can use. I want varnish to listen on port 80 and use 128M of RAM.


DAEMON_OPTS="-a :80 \
-T localhost:6082 \
-f /etc/varnish/default.vcl \
-S /etc/varnish/secret \
-s file,/var/lib/varnish/$INSTANCE/varnish_storage.bin,128M"

4. Edit apache settings

You must edit /etc/apache/ports.conf file and ensure apache will now listen on port 8080. If you have a vhosts setup you must also change which port the vhosts listen on.

So /etc/apache/ports.conf should look like this

NameVirtualHost *:8080
Listen 8080
.....

Change /etc/apache2/sites-available/default to also listen on port 8080, and do the same for any vhosts in this directory.

NameVirtualHost *:8080
Listen 8080
.....

Example vhosts


ServerName mormanski.net
.....

5. Restart Apache and Varnish

Restart apache.
sudo /etc/init.d/apache2 restart
And start Varnish too.
sudo /etc/init.d/varnish start

6. Useful commands and information

This netstat command will tell you which ports are open
netstat -anp --tcp --udp | grep LISTEN

You can use varnishlog to see what’s happening in real time
varnishlog

The following resources also proved helpful.

5 Responses to “Install & Configure Varnish on Ubuntu Lucid Linux”

  1. Now running Varnish Cache/proxy | The Black Book Says:

    […] some of the following resources: http://www.slideshare.net/schoefmax/caching-with-varnish-1642989 http://www.mormanski.net/2010/08/14/install-configure-varnish-on-ubuntu-lucid-lynx Uncategorizedcache, performance, ubuntu, varnish ← rc.local on ubuntu Leave a […]

  2. Gulab Pasha Says:

    Hi,

    I’m new to varnish, What if I use the below settings.

    vi /etc/varinish/default.vcl
    backend default {
    .host = “127.0.0.1”;
    .port = “80”;
    }

    &

    vi /etc/default/varnish
    DAEMON_OPTS=”-a :80 \
    -T localhost:6082 \
    -f /etc/varnish/default.vcl \
    -S /etc/varnish/secret \
    -s file,/var/lib/varnish/$INSTANCE/varnish_storage.bin,1G”

    &

    /etc/apache/ports.conf
    NameVirtualHost *:80
    Listen 80

    & all my virtual hosts listen to port 80:

    /etc/apache/sites-avialable/mysite.com

    i just looking to setup varnish to listen my port 80 and help clearing all caches.

    Please help

    Thanks,

  3. Mike Gifford Says:

    And this here simply indicates that port 8080 is open with IPv6 and there doesn’t seem to be an IPv4 port open, right?

    $ netstat -anp –tcp –udp | grep LISTEN | grep 8080(No info could be read for “-p”: geteuid()=555 but you should be root.)
    tcp6 0 0 :::8080 :::* LISTEN

    I’m wanting to set up varnish for some sites and not others on the same server so want to Listen on both 80 & 8080. I haven’t seen a clear example for this however.

  4. Eric Says:

    These are good, to the point, instructions. Thanks for posting them.

    Why not encourage visitors to use Varnish’s repository instead of the universe? Instead of using:

    sudo apt-get install varnish

    use

    1. curl http://repo.varnish-cache.org/debian/GPG-key.txt | apt-key add –
    2. echo “deb http://repo.varnish-cache.org/debian/ $(lsb_release -s -c) varnish-2.1″ >> /etc/apt/sources.list
    3. apt-get update
    4. apt-get install varnish

    (See http://www.varnish-cache.org/installation/ubuntu)

    When I last used the universe repository, the version was 2.1.0. The universe repository does seem to have been updated, though.

    Adding Listen 8080 to /etc/apache2/sites-available/default and other vhosts prevented Apache from restarting. (instead of ) seems to be all it needs.

    Also, this line:

    3. Edit the /etc.default/varnish file

    perhaps should read:

    3. Edit /etc/default/varnish

    We’re so programmed into thinking that a file has to have an extension, along with the typo, that this instruction can be a little confusing.

    thanks!

  5. Administrator Says:

    Thanks Eric,

    I fixed the typo!

Leave a Reply