How to Install Varnish on Ubuntu 16.04

r00t September 18, 2017

Install Varnish on Ubuntu 16.04

In this tutorial we’ll learn how to install Varnish on Ubuntu 16.04. We will also install and configure its prerequisites. Varnish is a proxy and cache, or HTTP accelerator, designed to improve performance for busy, dynamic web sites. By redirecting traffic to static pages, varnish reduces the number of dynamic page calls, thus reducing load. Varnish is designed for content-heavy dynamic web sites as well as heavily consumed APIs. In contrast to other web accelerators, such as Squid, or Apache and nginx, which are primarily origin servers, Varnish was designed as an HTTP accelerator.

I recommend to use a minimal Ubuntu server setup as a basis for the tutorial, that can be a virtual or a root server image with an Ubuntu 16.04 minimal install from a web hosting company or you use our minimal server tutorial to install a server from scratch.

Install Varnish on Ubuntu 16.04

Step 1. First, ensure your system and apt package lists are fully up-to-date by running the following:

apt-get update -y
apt-get upgrade -y

Step 2. Installing Varnish.

We can install varnish on Ubuntu Server using apt-get install command. Open the terminal, first update the apt source list, then install the varnish package:

sudo apt-get install varnish

After the installation is finished, start and enable varnish.service using the systemctl command:

sudo systemctl start varnish.service
sudo systemctl enable varnish.service

Step 3. Configuring Varnish Cache.

We need to configure the Apache server to listen on different port. We will use the port 8080.

sudo sed -i "s/Listen 80/Listen 8080/" /etc/apache2/ports.conf

Once you have changed port, restart Apache and Varnish daemon manually:

systemctl restart httpd.service
systemctl restart varnish.service
sudo /usr/local/sbin/varnishd -a :80 -b localhost:8080

Step 4. Verifying Varnish Cache.

You can easily verify if Varnish is running by running the following command:

curl -I localhost

You should see the output something like this:

HTTP/1.1 200 OK
Date: Wed, 10 Dec 2017 9:26:07 GMT
Server: Apache/2.4.25 (Ubuntu)
Last-Modified: Fri, 16 Oct 2017 10:36:53 GMT
ETag: "6c-5211cdbf61c14"
Content-Length: 108
Content-Type: text/html; charset=UTF-8
X-Varnish: 32770
Age: 0
Via: 1.1 varnish-v4
Connection: keep-alive

Congratulation’s! You have successfully install and configured Varnish cache on your Ubuntu 16.04 LTS server. Thanks for using this tutorial for installing Varnish on Ubuntu 16.04 system.

The Tags:

Leave a Comment

Comments are closed.