;

How to set up Varnish on Ubuntu 16

Try it in our public cloud & Get $5 Credit
CLAIM NOW

One of the problems with running a site with many users is that the constant requests for dynamic content can really bog down a web server. In order to solve this problem, many big companies turn to a tool called Varnish. It’s an HTTP accelerator that caches both static and dynamic content, thereby easing the load on your server and improving site performance for the end user.

Varnish is open source under the BSD license and can be easily installed on Ubuntu 16.

Getting started

To complete this guide, you will need the following:
• 1 Node (Cloud Server or Dedicated Server) with Ubuntu 16 installed.
• Apache installed as part of a LAMP stack
• All commands entered in root

Tutorial

Begin by installing Varnish on your system.

apt-get install varnish -y

Let’s change the port that Varnish uses by default. Open the Varnish configuration file.

nano /etc/default/varnish

Go to the Alternative 2 section and change

DAEMON_OPTS="-a :6081 \

to

DAEMON_OPTS="-a :80 \

You’ll need to change the Varnish VCL configuration as well.

nano /etc/varnish/default.vcl

Remember, if you change the port in this file, you’ll need to make sure to set the same port in the Apache configuration at the next step.

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

Change the port in Apache Virtualhost to match the one you set previously in the Varnish VCL configuration file.

nano /etc/apache2/sites-enabled/000-default.conf

<VirtualHost *:80>

to

<VirtualHost *:8080>

Restart Apache and Varnish so that they reflect the changes you’ve just made.
systemctl restart apache2.service
systemctl restart varnish.service

You might run into some issues with installing Varnish on Ubuntu 16.

If you get an error, check the process that’s running on your server.

ps aux | grep vcache
vcache 15569 0.0 0.7 125044 7816 ? Ss 08:20 0:00 /usr/sbin/varnishd -j unix,user=vcache -F -a :6081 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m
vcache 15581 0.0 9.3 272012 94900 ? Sl 08:20 0:00 /usr/sbin/varnishd -j unix,user=vcache -F -a :6081 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m

You may see that the port is still set to 6081 instead the one you set in /etc/default/varnish. What this means is that the systemd service bypassed the configuration file.

In order to solve the problem, edit the file /lib/systemd/system/varnish.service and change the port 6081 to 80. Also, you can change the malloc,256 for a different memory value at this time.

nano /lib/systemd/system/varnish.service

[...]
ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :6081 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m
[...]

to

[...]
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
[...]

Reload and restart Apache and Varnish once more.

systemctl daemon-reload
systemctl restart apache2.service
systemctl restart varnish.service

Check your Varnish stats to make sure everything’s working correctly.

varnishstat

Finally, ensure that your web server and Varnish are operating normally.

curl -I http://your_ip

HTTP/1.1 200 OK
Date: Wed, 22 Jun 2016 08:32:03 GMT
Server: Apache/2.4.18 (Ubuntu)
Last-Modified: Wed, 22 Jun 2016 08:18:20 GMT
Vary: Accept-Encoding
Content-Type: text/html
X-Varnish: 32771
Age: 0
Via: 1.1 varnish-v4
ETag: W/"2c39-535d9949460b3-gzip"
Accept-Ranges: bytes
Connection: keep-alive

With Varnish installed, you can rest easy that your site will survive an unexpected surge of visitors and be better protected against any malicious attacks. If this guide was helpful to you, kindly share it with others who may also be interested.