Table of Contents
Was this article helpful?
Try Vultr Today with

$50 Free on Us!

Want to contribute?

You could earn up to $600 by adding new articles.

Installing Varnish 4 with Apache 2 on Ubuntu 14.04

Last Updated: Mon, Jan 26, 2015
Ubuntu Web Servers
Archived content

This article is outdated and may not work correctly for current operating systems or software.

Apache is popular web server used by most web hosting companies. Varnish cache is an HTTP accelerator and reverse proxy. We can use Varnish with any HTTP server. In this example, we will be using Apache 2.

As a web server, Apache can use a considerable amount of server resources to serve pages. If you are running a high-traffic website, then you might need an HTTP accelerator to boost server performance. Varnish will help you with that.

Step 1

Install Apache server and activate it by using the following commands:

sudo apt-get update

sudo apt-get install apache2-mpm-event

You can test Apache's server status with this command:

sudo service apache2 status

If the service is running, "apache2 is running" will be printed to your terminal. Otherwise, you can start the service with this command:

sudo service apache2 start

Step 2

Install the latest stable version of Varnish 4. This version is not available in Ubuntu Repository by default, so you need to run the following commands to install it.

sudo apt-get install apt-transport-https

sudo curl | apt-key add -

echo "deb trusty varnish-4.0" >> /etc/apt/sources.list.d/varnish-cache.list

sudo apt-get update

sudo apt-get install varnish

Step 3

Configure Varnish Cache. Here we are going to change the Varnish server port to 80. Run the following command:

sudo nano /etc/default/varnish

Now look for DAEMON_OPTS=" under Alternative 2, Configuration with VCL. Change the DAEMON_OPTS=" section to match the following lines. This is only a port update.

DAEMON_OPTS="-a :80 \

             -T localhost:6082 \

             -f /etc/varnish/default.vcl \

             -S /etc/varnish/secret \

             -s malloc,256m"

Press CTRL + X, then Y to save the file.

Step 4

Configure Apache. We need to change the listening port of Apache from 80 to 8080.

Edit the ports file by run the following command:

sudo nano /etc/apache2/ports.conf

Change the Listen 80 to Listen 8080.

Next, update the virtual host file...

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

... change <VirtualHost *:80> to <VirtualHost *:8080>.

If you have other virtual host files, then they should be updated as well.

Restart both services.

sudo service apache2 restart

sudo service varnish restart

You're all set. See the following sections for advanced setup tips.

View stats

Run the following command to show Varnish stats:


Advanced VCL settings

You can edit the default.vcl file for various features.

Enable leverage browser caching

To enable browser caching for media files, your vcl_backend_response should match the following configuration.

sub vcl_backend_response {

    if (bereq.url ~ "\.(png|gif|jpg|swf|css|js)$") {

    unset beresp.http.set-cookie;

    set beresp.http.cache-control = "max-age = 2592000";



This will improve your site speed and SEO ranking.

Purge cache

To clear the Varnish's cache, you can change vcl_recv to match the following configuration:

sub vcl_recv {

     if (req.method == "PURGE") {

            return (purge);



After making this change, you can send a curl request in your ssh session with the following format:

curl <> -XPURGE

Here, -XPURGE will send the purge request to the Varnish server.

Want to contribute?

You could earn up to $600 by adding new articles.