Install and Configure Varnish 4 on Debian 8

Varnish is a lightweight HTTP Accelerator/Reverse Proxy for Linux. The idea is that Varnish sits in front of the http server, saving what it can to memory. When a page is requested, Varnish will check if it has the content in memory. If so, it serves it. If not, then and only then, is the HTTP server called upon.

I am currently experimenting with a HA web cluster and while attempting to configure Varnish 4 on Debian 8, came across an odd issue whereby Varnish would always listen on the default 6081 port despite having this value changed. Here I’ll run through the basic command line install process for Varnish along with a quick fix for this problem.

Update repositories

apt-get update

Install Varnish

apt-get install varnish

Open the default config file

nano /etc/default/varnish

Search for the following line and change 6081 to 80

DAEMON_OPTS="-a :6081

This is the port Varnish will listen for incoming connections

Restart Varnish

service varnish restart

Check which port Varnish is listening on

cat /lib/systemd/system/varnish.service

If it is listening on port 80; great stuff. This issue has been resolved. You can skip the next step. If not, carry on…

Edit the varnish service file

nano /lib/systemd/system/varnish.service

Find the following line and change the port number 6081 to 80

ExecStart=/usr/sbin/varnishd -a :6081

Restart system daemon

systemctl daemon-reload

Time to point Varnish at the HTTP server

nano /etc/varnish/default.vcl

Find the following lines. .host should be changed to the address of your HTTP server, or set to 127.0.0.1 if Varnish and the HTTP server will run on the same machine. Change the port to 8080

.host = "192.168.1.10";
.port = "8080";

Restart Varnish

service varnish restart

You can test the varnish is working by browsing to

http://your.ip.address

If Varnish is configured properly you should see the following message

Screen Shot 2015-08-15 at 01.28.38

Now you need to reconfigure the HTTP server to run on port 8080. Here I’m using Nginx but the principle is the same for Apache.

Open your site config file

nano /etc/nginx/sites-available/example

Find the following segment and change the port number to 8080

listen 80;

Restart Nginx

service nginx restart

The status of Varnish can be seen with the following command

varnishstat

Happy caching!

Leave a Reply

Your email address will not be published. Required fields are marked *