How to Install and Setup Varnish Cache With Apache on Ubuntu

By: Sunil Kumar |  In: Server  |  Last Updated: 2018/07/26

How to Install and Setup Varnish Cache With Apache on Ubuntu

Varnish is an HTTP caching server which is used to accelerate any web application. Varnish is focused exclusively on HTTP, unlike other proxy servers that often support FTP, SMTP and other network protocols.

How does Varnish work

For starters, varnish is a layer between your web server and the client(browser or any bot). So when a client sends a request a request, its served through Varnish cache instead of directly served by your web server. And Varnish passes this request to your web server. The back-end server does its regular work and returns a response to Varnish, which in turn gives the response to the client that sent the original request.

If that’s all Varnish did, it wouldn’t be much help.

What gives us the performance gains is that Varnish can store responses from the back end in its cache for future use. Varnish quickly can serve the next response directly from its cache without placing any needless load on the back-end server.

The result is that the load on the back end is reduced significantly, response times improve, and more requests can be served per second. One of the things that make Varnish so fast is that it keeps its cache completely in memory instead of on disk.

Installing Varnish

To install varnish you need to have Apache(of course you can use any web server, but here we are configuring Varnish on Apache) installed on your server. If you don’t have Apache installed on your server, you can install by

 $ sudo apt-get update
 $ sudo apt-get install apache2 

Now we are ready to install varnish. For Installing varnish we have to add Varnish Repository to our sources

To add the repository, run the following command:

   $ sudo curl | sudo apt-key add -

Now to edit List of apt Sources Open the /etc/apt/sources.list

 $ sudo nano /etc/apt/sources.list

Next, add the Varnish repository at the end of the file.

  deb lucid varnish-3.0

or simply run this command to add it

  $ sudo echo "deb lucid varnish-3.0" | sudo tee -a /etc/apt/sources.list 

Now we can use the usual apt-get to install Varnish.

  $ sudo apt-get install varnish

Configure Varnish

Once you have installed apache and varnish, you can start to configure varnish on your server to avail the service of it by following these steps
There are two main steps to configure varnish on the system

  • Change Listening port of Apache
  • Change listening port of Varnish

1. Change Listening port of Apache

Open /etc/apache2/ports.conf
And change the Listen 80 to Listen 8080.
And also update the Virtual hosts file

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

change <VirtualHost *:80> to <VirtualHost *:8080>.
Now we are going to restart apache to test whether Apache request are going to port 8080 or not.

   $ sudo service apache2 restart

and now hit
curl -I http://locahost:8080

Its HTTP status should be 200 OK.

2. Change listening port of Varnish

Now change the default port where Varnish listens on to 80,
To do so open /etc/default/varnish look for “alternative 2” and uncomment the following code. Change the ports as necessary.

  DAEMON_OPTS="-a :80 \
        -T localhost:6082 \
        -f /etc/varnish/default.vcl \
        -S /etc/varnish/secret \
        -s malloc,256m"

So what we are doing here is, setting Varnish to use Port 80, using /etc/varnish/default.vcl as our configuration file, and allocating 256MB of memory.

3. Redirect incoming traffic from Varnish to the web server

Fortunately, Varnish does this automatically after we have completed 1) and 2) steps.
To confirm this open up the default.vcl file:

  $ sudo nano /etc/varnish/default.vcl

This file tells varnish where to look for the web server content. Within this file, we will tell varnish to look for the content on port 8080.
The configuration should like this:

  backend default {
    .host = "";
    .port = "8080";

After completing these steps we can restart both the services(apache and varnish)

 $ sudo service apache2 restart
 $ sudo service varnish restart

If your server restart successfully, then We are all set. now we can proceed to further advanced setup.

View varnish stats

Accessing your domain should instantly take you to the varnish cached version, and you can see the details of varnish?s workings with this command


It will output like this –

Now as everything is setup, we can test whether our request is being served by Apache or varnish(should be served by Varnish)
to test run

   curl -I http://locahost

If it’s output looks like this then everything is fine.

Or we can also hit this URL in our browser

Your request are now served by varnish.

If the request is still served by Apache server instead of varnish server or you get ERR_CONNECTION_REFUSED error on your browser that means that a port is blocked by a firewall, or there is nothing listening on that port.
To fix this, we need to override the systemd config. Run these commands:

  $ sudo mkdir -p /etc/systemd/system/varnish.service.d
  $ sudo nano /etc/systemd/system/varnish.service.d/customexec.conf

This will create a custom execution file for Varnish’s service, which will contain your custom settings. In this file, make sure the contents are:

    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

now save the file and Reload the system service cache by running this command:

    $ sudo systemctl daemon-reload

Make sure your firewall allows port 80 through, and you should be good to go.
Now we have Ready Varnish working in front of your apache server to speed up your website.
Now we can Proceed with Advance backend caching Options to make More use of Varnish.


  • I do trust all the ideas you’ve introduced in your post.They’re really convincing and can definitely work.Nonetheless, the posts are very quick for novices. May youplease prolong them a little from next time? Thanks for the post.

  • I just want to mention I am newbie to blogging and site-building and truly savored you’re website. More than likely I’m likely to bookmark your blog post . You certainly come with impressive article content. Cheers for revealing your webpage.

  • What’s up it’s me, I am also visiting this website
    on a regular basis, this web page is actually fastidious and the viewers are in fact
    sharing nice thoughts.

  • Aw, this ԝaѕ an extremely ɡood post. Spending ѕome tіme and actual effort tо generate a gօod article… but what cɑn I sɑy… I procrastinate a ⅼot ɑnd
    don’t ѕeem to get nearlү anything done.

  • of course like your website but you need to check the spelling on several of your posts. Several of them are rife with spelling issues and me to find it very troublesome to tell the reality, on the other hand, I’ll definitely come back again.

  • wonderful points altogether, yοu jᥙst won a new reader.
    Wһat ѡould yоu suggеst in гegards tο youг submit that you simply made a few daʏs in the pɑst? Any positive?

  • Howdy very cool blog!! Μan .. Beautiful .. Amazing ..
    I wilⅼ bookmark ʏouг blog ɑnd tаke the feeds
    additionally?I ɑm happy to search out numerous helpful іnformation hеrе in thе submit, ԝe
    want develop extra strategies in this regard, tһanks for sharing.

  • Hey there! Would you mind if I share your blog with my twitter group?

    There’s a lot of folks that I think would really appreciate your content.
    Please let me know. Many thanks

  • Fantastic blog you have here but I was wondering if you knew of any message boards that cover the same topics
    talked about here? I’d really love to be a part of group where I can get suggestions from
    other experienced individuals that share the same interest.
    If you have any suggestions, please let me know.

  • I and also my buddies have been reading through the good procedures from
    your site and so suddenly came up with a horrible feeling I
    never expressed respect to the web blog owner for those strategies.

    Those ladies ended up certainly joyful to learn them and
    now have truly been enjoying them. We appreciate you getting well thoughtful and for picking variety of wonderful things most people are really needing to learn about.
    My sincere regret for not expressing gratitude to earlier.

  • I am curious to find out what blog system you happen to be working with?
    I’m having some small security issues with my latest website and I’d like to find something more risk-free.
    Do you have any recommendations?

  • Leave a Comment

    Your email address will not be published.


    Sunil Kumar

    I am the owner of acmeextension. I am a passionate writter and reader. I like writting technical stuff and simplifying complex stuff.
    Know More

    Join more than 10,000 others Web Developers

    %d bloggers like this: