How to Setup Varnish on Ubuntu 14

Try it in our public cloud & Get $50 Credit

Varnish is a fast, efficient HTTP caching layer that sits between browsers and a web application. Perhaps the process of generating a page is time-consuming and resource-intensive, despite the fact that the page changes infrequently. Or perhaps a page and its assets transfer slowly between geographically disparate sites, but are served much more quickly within specific regions. With Varnish cache in place between a site and its viewers, a variety of caching scenarios can be accounted for. Using an HTTP cache like Varnish is one of many strategies that can be used to speed up a website.

Getting Started

To complete this guide, you will need the following:
• 1 Node (Cloud Server or Dedicated Server) running Ubuntu 14.
• A LAMP stack configured
• Root Access to your server

When finished, your Apache assets will be fronted by a Varnish cache.


Begin by installing the Varnish package itself.

apt-get install varnish -y

Varnish won’t do much unless it is configured. We’ll next configure various Varnish options to integrate it with the installed LAMP stack.

nano /etc/default/varnish

Find the Alternative 2 section. Once there, change:

DAEMON_OPTS="-a :6081 \

to the following:

DAEMON_OPTS="-a :80 \

We must now configure the Varnish VCL.

nano /etc/varnish/default.vcl

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

Varnish’s default port is 8080. If you use this, you’ll need to update your Apache configuration separately as shown in the next section.

Apache’s default port must be set to match the port as configured in the Varnish VCL above.

nano /etc/apache2/ports.conf

Listen 80

Set it to:

Listen 8080

In order for your new configuration to take effect, both Apache and Varnish must be restarted. We’ll do that now.

service apache2 restart
service varnish restart

To determine if Varnish is caching, let’s look at its statistics.


We’ll further confirm that things are working by ensuring that Varnish and Apache are communicating correctly.

curl -I http://your_ip <---------------- Change for your main IP

HTTP/1.1 200 OK
Server: Apache/2.4.7 (Ubuntu)
Vary: Accept-Encoding
Content-Type: text/html;charset=UTF-8
Date: Tue, 21 Jun 2016 10:59:29 GMT
X-Varnish: 1661719520
Age: 0
Via: 1.1 varnish
Connection: keep-alive


Apache is now fronted by Varnish. If PHP is slow at generating a page but its content rarely changes, the slowness will only manifest itself on first access or cache invalidation. Future loads will be significantly faster, and load on the server will lessen as PHP is no longer relied upon for every single site access. If this guide was helpful to you, kindly share it with others who may also be interested.