How to install and setup Varnish in Ubuntu 16.04

How to install and setup Varnish in Ubuntu 16.04
by Janeth Kent Date: 02-01-2017 ubuntu varnish cache linux

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.

 
by Janeth Kent Date: 02-01-2017 ubuntu varnish cache linux hits : 6149  
 
Janeth Kent

Janeth Kent

Licenciada en Bellas Artes y programadora por pasión. Cuando tengo un rato retoco fotos, edito vídeos y diseño cosas. El resto del tiempo escribo en MA-NO WEB DESIGN AND DEVELOPMENT.

 
 
 

Related Posts

How To Use Varnish As A Highly Available Load Balancer On Ubuntu 20.04 With SSL

Load balancing with high availability can be tough to set up. Fortunately, Varnish HTTP Cache server provides a dead simple highly available load balancer that will also work as a…

How to move Chrome, Firefox or Edge cache to save space

Caching is a technique used by many programs to improve their performance, especially for applications that run over the Internet, such as streaming programs or web browsers. This technique consists…

Linux For Dummies: Permissions

In the previous articles I made a short introduction to the Unix world and in the following article I have dealt with the basic commands for the file system management. Today we are…

Linux for Dummies: Ubuntu Terminal

I introduced in the previous article, available here, the basic concepts concerning the Linux world. Today we are going to have a look to some basic operations that we can perform…

The Best RSS Readers for Ubuntu

Even if most of the tech experts actively claim that RSS (Rich Site Summary) is dead especially after Google Reader was discontinued 5 years ago but it isn’t yet as…

How to install Letsencrypt Certificates with Certbot in Ubuntu

In this article we will explain how to install, manage and configure the SSL Security certificate, Let's Encypt in NGINX server used as proxy. This certificate is free but does…

How to Set up a Fully Functional Mail Server on Ubuntu 16.04 with iRedMail

Setting up your own mail server from scratch on Linux is complex and tedious, until you meet iRedMail. This tutorial is going to show you how you can easily and…

GIMP 2.10 released: Features 32-bit support, new UI and A Ton Of Improvements

It's been over a half-decade since the GIMP 2.8 stable debut and today marks the long-awaited release of GIMP 2.10, its first major update in six years. And among other…

Setting Up SFTP on Ubuntu 16.04

I recently had a request to setup SFTP for a customer so they could manage a set of files in their environment through an FTP GUI. Being an avid user…

Install Java in Ubuntu 16.04

Java and the JVM (Java's virtual machine) are widely used and required for many kinds of software. This article will guide you through the process of installing and managing different…

ArangoDB, install and configure the popular Database in ubuntu 16.04

Introduction to ArangoDb, open source, NoSQL, multi-model database BigData seems to be getting stronger every day and more and more NoSQL databases are coming out to the market, all trying to position…

How to Choose a Laptop for Web Design and Development

Efficiency is important at work, no matter the type of job you do. The better the tools that you use, the more productive you are. So if you’re a web…

Clicky