Work With Jeremy David Evans

2015 / 19 December

How to use Varnish Cache for Ubuntu

varnish caching server

Varnish Cache is Amazing

I will leave it to you to read some of the benchmarking studies, it is likely you already have if you’re here. I am going to cut straight to the point and share my shell-secrets with you, so you can start speeding up your sites right away.

Pre-req’s

I am assuming:

  • You are using Apache2
  • You are using the current LTS Ubuntu (Trusty v14.4)
  • You have root access
  • You’re using nano as a text editor (doesn’t matter, really: replace as it as you see fit, you vim-freak)
  • You love fast interwebz

Step One:

Download Varnish

As root, you must add the varnish repository to your sources.list. Copy and paste step-by-step.

  1. su
  2. apt-get install apt-transport-https
  3. curl https://repo.varnish-cache.org/GPG-key.txt | apt-key add -
  4. echo "deb https://repo.varnish-cache.org/ubuntu/ trusty varnish-4.1" >> /etc/apt/sources.list.d/varnish-cache.list
  5. apt-get update
  6. apt-get install varnish

Step Two:

Configure Varnish

This is for the most common set-up. Varnish will be public facing, using the port that all browsers use to request web pages (:80). We will be setting the back-end server (Apache2) to port 8080, where Varnish will fetch our pages if they’re not cached.

sudo nano /etc/default/varnish
#edit the default configuration to listen on port 80 as follows
 DAEMON_OPTS="-a :80 \
             -T localhost:6082 \
             -f /etc/varnish/default.vcl \
             -S /etc/varnish/secret \
             -s malloc,256m"

In theory, the default file will look just like this except for the first line (the “-a” line). Be sure to change this first line to :80.

Now, there is another config file that you may have noticed is loaded in the aforementioned. You can peek around at /etc/varnish/default.vcl but I am not going to make it a step since we are going to be setting our server to the default configuration described therein. In other words, that file expects the backend server to be on localhost (127.0.0.1) at 8080.

So, let’s do that!

Step Three:

Configure Apache2

sudo nano /etc/apache2/ports.conf
#change the "Listen" line to the following:
Listen 127.0.0.1:8080

This is great: Apache will listen to localhost on port 8080 by default now! But I bet you have a virtualhost or two already running… So now we need to do the same for each virtualhost configuration file.

#let's snoop around our virtualhost directory a bit...
ls /etc/apache2/sites-available/

#you should see all of your site directories here. You will need to edit them all like I will suggest you edit the default:
sudo nano /etc/apache2/sites-available/000-default.conf
<VirtualHost 127.0.0.1:8080>
...blah, blah, w/e
</VirtualHost>

There! Now your virtualhosts are in tune with your server daemon and all is well. We’re gonna have to restart apache and varnish and see how everyone feels about the new set-up.

Step Four:

Testing… Testing…

sudo service apache2 restart
sudo service varnish restart

So? How’d it go? Hopefully, both gave the sweetest words from your servers’ lips: nothing at all. Maybe a reassuring “[OK]” from apache2.

If not, it is foremost likely to be a typo. Put on your programmer goggles and look carefully through the virtual hosts and varnish config files. They generally output a line number for a syntax error, so this is a quick fix.

Obviously, there are many server configuration deviating from the simple defaults used here (SSL comes to mind). If that is the case, then Google is your ally here.

Go ahead and visit each URL. You should get the ol’ page you know and love, at about double the speed. Awesome, right?

If not awesome…

There is a known bug using Varnish 4.~ on Ubuntu, with the terrifying error of simply a blank page. It is odd: I don’t always encounter this bug, even on identical servers with the same installation methods… nonetheless, it does happen. The more telltale problem is that Varnish simply will not listen on port 80. If you’re getting a blank page, run a quick netstat to see if this is your problem:

sudo netstat -lpnt 
# or see all the processes related to Varnish
ps aux | grep varnish

Is “varnishd” (Varnish’s daemon) listening on port 80? If not, no worries: the solution is simple. We just need to configure an additional process that is overriding our default configuration. This is usually due to the fact that varnish is counting on an “init.d” file to read the configuration, though your system may be using “systemd” to read an alternate init file. (Don’t sweat it if that doesn’t make sense; purely for geeks, that bit.) All we need to do is mark up alternate config file to listen at port 80.

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

# change "ExecStart=/usr/sbin/varnishd -a" from ":6081" to :80" . 
# Now we'll restart Vanrish using our fancy new systemd init file:

sudo service varnish restart
# or use this command, which actually reloads the /etc/default/varnish config file! 
# (Don't worry, we needed to update the systemd init script if you were having this problem since server reboots will cause it to return without the aforementioned step.)
systemctl reload varnish.service

Keep the Web Shiny

Remember, in the end, it is all [OK]…. Putting in this trivial amount of effort now is going to save all of your website viewers seconds of their lives that they would otherwise be “buffering” through. Think about it: with a few thousand visitors and a few seconds of pageload saved, you could quickly save the cumulative equivalent of a human life! Food for thought, developer, food for thought….

No comments so far.

LEAVE YOUR COMMENT