Configure varnish cache on front of LAMP server with ubuntu 16.04

Varnish Cache is a caching HTTP reverse proxy, or HTTP accelerator, which reduces the time it takes to serve content to a user. The main technique it uses is caching responses from a web or application server in memory, so future requests for the same content can be served without having to retrieve it from the web server. Performance can be improved greatly in a variety of environments, and it is especially useful when you have content-heavy dynamic web applications.

I have implemented improves version and took references from digitalocean guide

You need a LAMP or LEMP stack to implement varnish. I consider you have already have installed it.

—————– Ubuntu 14.04 varnish install ————–

Now add the Varnish GPG key to apt

Then add the Varnish 4.0 repository to your list of apt sources:

Update apt-get and install Varnish with the following commands

—————– Ubuntu 16.04 varnish install ————–

Restart varnish

Ubuntu 16.04

Check varnish is installed & what version of varnish is installed.

By default, Varnish is configured to listen on port 6081 and expects your web server to be on the same server and listening on port 8080. Open a browser with port 6081 of your server.

You will get this message as follow. it means varnish is working and varnish could not find the cahed file.

503 Backend fetch failed

First, we will configure Varnish to use our LAMP as a backend.
The Varnish configuration file is located at /etc/varnish/default.vcl. Let’s backup and edit the configuration

Now you need to make varnish on the frontend and apache on the backend when varnish cache is not available.

Edit varnish configuration

Change varnish listening port to 80

There are changes in ubuntu 15.04 and the above file will not work. please follow this guide https://www.varnish-cache.org/docs/trunk/tutorial/putting_varnish_on_port_80.html

 

Open apache configuration and change apache port to 8888

Restart varnish and apache

Now open main domain and check if varnish is serving cached version or not.

First time it will load data from apache and in reponse header you will get x-Cache: MISS

You can reload the page again and varnish will serve cached data and you will get x-Cache: HIT – 3

To make sure you can stop apache to see if varnish is serving data.

Purge one file via command line

Purge whole domain via command line.

Ban one file via command line

Ban whole domain via command line.

 

This is the php function which can be used to test varnish purge or use “Chrome Postman” to send the call.

 

This is a very good configuration for varnish
https://github.com/mattiasgeniar/varnish-4.0-configuration-templates/blob/master/default.vcl

Varnish Book
https://www.varnish-software.com/static/book/

Varnish Documentation
https://www.varnish-cache.org/docs

Varnish 4 Toturial
https://www.varnish-cache.org/docs/4.0/tutorial/

Varnish 3,4 Configuration
http://foshttpcache.readthedocs.org/en/latest/varnish-configuration.html