Install and Configure Varnish 4 on Ubuntu

 

What are we doing?

  • Adding the Varnish repository to Ubuntu
  • Downloading and installing Varnish
  • Implementing a basic configuration

 

Why?

The Ubuntu repo still hasn’t been updated and will install version 3 by default. Adding the updated repository will allow installation of version 4. Varnish is an HTTP accelerator, generally used in front of an HTTP server to cache content in memory. Configuration allows full pages or individual data types such as CSS or images to be cached.
 

How?

If you already run Varnish and want to check the version, run from the command line

varnishd -V

Add the Varnish repository to Ubuntu. The default repo includes the now outdated version 3

sudo curl https://repo.varnish-cache.org/ubuntu/GPG-key.txt | apt-key add -
echo "deb https://repo.varnish-cache.org/ubuntu/ trusty varnish-4.0" >> /etc/apt/sources.list.d/varnish-cache.list

 

Update repositories

sudo apt-get update

 

Install Varnish

sudo apt-get install varnish

 

Configure the Varnish listening port

sudo nano /etc/default/varnish

Look for the following line, after ## Alternative 2

DAEMON_OPTS="-a :6081 

If Varnish will be at the front of your web stack, you’ll likely want to change this to

DAEMON_OPTS="-a :80 

Press ctrl + x then y to save and exit nano
 

Point Varnish at your HTTP server

sudo nano /etc/varnish/default.vcl

Look for the following code block, after # Default backend definition

.host = "127.0.0.1";

If the HTTP server is on the same host this is fine. If not, set your host here

Change the port if required

Configure web server listening port

 

Apache

 

sudo nano /etc/apache2/ports.conf

Change the port on the following line from

Listen 80

to

Listen 8080

Edit the available sites conf

sudo nano /etc/apache2/sites-available/000-default.conf

Change from

<VirtualHost *:80>

to

<VirtualHost *:8080>

Restart Apache and Varnish

sudo service apache2 restart
sudo service varnish restart

 

Nginx

 
Edit the Nginx available sites conf

sudo nano /etc/nginx/sites-available/default

Change the line

listen 80 default_server;

to

listen 8080 default_server;

Restart Nginx and Varnish

sudo service nginx restart
sudo service varnish restart

Leave a Reply

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