Accelerate Your Website By Using Varnish With Apache
As a website’s traffic increases, you can often find that the site is repeatedly serving the same files over and over again from disk. This can cause a problem whereby the server can reach the performance limits of its disks repeatedly loading and serving the same files to visitors. This problem is exacerbated by media heavy sites and others where a lot of static content is returned to the browser. This is where Varnish comes in. Acting as a layer between the web server and the browser, its job is to cache regularly requested items in memory to allow it to serve them faster while also reducing the load on your disks.
In this tutorial we’ll be using Varnish with the Apache web server. These instructions are valid for systems running Debian Linux, or a related distribution such as Ubuntu. We’ll assume you already have your server configured running Apache with some virtualhosts configured.
How to Set Up Varnish with Apache
To get started we can install Varnish from the default repositories. While this won’t be the latest version of Varnish, you will still get security updates for the lifetime of your Linux distribution:
sudo apt-get update
sudo apt-get install varnish
Once installed, the configuration for Varnish is handled by the configuration file /etc/default/varnish, so we’ll start with editing that to match our configuration. In this example I’ll be using nano to edit the file, though you can use your preferred text editor.
sudo nano /etc/default/varnish
In this file there are 4 alternate configuration sections listed. Find the line starting with “## Alternative 2”. This is the default selected configuration in the configuration, but we are going to edit the first line so that it listens on port 80 which is the default webserver port. When edited, your config should look as below:
DAEMON_OPTS=”-a :80 \
-T localhost:6082 \
-f /etc/varnish/default.vcl \
-S /etc/varnish/secret \
-s malloc,256m”
Save and exit this file. As per the configuration, Varnish will look for extra information on what to do from the /etc/varnish/default.vcl file, so we’ll look at that next to see where to get the website files from:
sudo nano /etc/varnish/default.vcl
We aren’t going to change the configuration in this file as the default settings are fine for our use, which are given in the “backend default” section. This is set to look at 127.0.0.1 (a special IP also known as localhost which is used to reference the same computer as the software is running on) port 8080 for the web server that will provide Varnish with the files it should be serving.
You can exit this file without making any changes, and we’ll use that information to reconfigure Apache to listen on that port for requests from Varnish.
Next, we’ll edit the /etc/apache2/ports.conf file to tell it to listen on port 8080.
sudo nano /etc/apache2/ports.conf
Change the listen line to:
Listen 8080
Then save and exit the file.
Now you need to go through all your vitualhost configurations to tell Apache which port to listen on. For example, if you are using the default configuration we would do:
sudo nano /etc/apache2/sites-available/000-default.conf
Then change the “<VirtualHost *:80>” line to:
<VirtualHost 127.0.0.1:8080>
Next, save and exit the file. This change will mean that Apache will only serve the website files to connections coming from Varnish. If you are serving multiple virtualhosts from your server then you’ll need to make this change to all of them.
With this done, the last step on modern systems (Ubuntu 16.04+, Debian Jessie+) is to update systemd to enable it to allow Varnish to listen on port 80. First we’ll copy over the default configuration file, then edit it:
sudo cp /lib/systemd/system/varnish.service /etc/systemd/system/
sudo nano /etc/systemd/system/varnish.service
In this file you are looking for a line that begins with “ExecStart”. In this line there are a number of familiar flags to the default configuration file. We need to change the section that says “-a :6081” to “-a :80” so it should end up looking like this:
ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m
Now we just need to reload the systemd daemon caches and restart the services:
systemctl daemon-reload
systemctl restart apache2
systemctl restart varnish
With that done, your website should now be served through Varnish. To check that it works, simply navigate to a website being served from your server.