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



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.


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 | apt-key add -
echo "deb 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 = "";

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




sudo nano /etc/apache2/ports.conf

Change the port on the following line from

Listen 80


Listen 8080

Edit the available sites conf

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

Change from

<VirtualHost *:80>


<VirtualHost *:8080>

Restart Apache and Varnish

sudo service apache2 restart
sudo service varnish restart



Edit the Nginx available sites conf

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

Change the line

listen 80 default_server;


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 *