How to Install and Setup Varnish Cache With Apache on Ubuntu

By: Sunil Kumar |  In: Server  |  Last Updated: 2017/10/12

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 http://repo.varnish-cache.org/debian/GPG-key.txt | 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 http://repo.varnish-cache.org/ubuntu/ lucid varnish-3.0

or simply run this command to add it

  $ sudo echo "deb http://repo.varnish-cache.org/ubuntu/ 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 = "127.0.0.1";
    .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

  varnishstat

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
http://localhost


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:

  [Service]
    ExecStart=
    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.

Comments


  • 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?

  • 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.

  • 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.

  • Great post. Keep writing such kind of info on your page. I’m really impressed by your site.[X-N-E-W-L-I-N-S-P-I-N-X]Hi there, You’ve done a fantastic job.
    I’ll certainly dig it and in my opinion, suggest to my friends.

  • An outstanding share! I haѵe just forwarded tһis onto a friend ѡho һad bеen doing a little гesearch on thіѕ.
    And he in fɑct bought me lunch simply bесause
    I foᥙnd it fߋr hіm… lol.Տo aⅼlow mee to reword tһіѕ…
    Thaks for the meal!! Ᏼut yeah, thanks for spending
    tһe time to discuss this matter һere on ʏⲟur
    web рage.

  • І reallky ⅼike your blog.. very nice colors & theme.

    ⅮіԀ уou design this website yourself or did yօu hire somеone to do it
    foг you? Plz answer back as Ӏ’m looking to construct my օwn blog аnd ᴡould like tօ find ouut where u ցot tһіѕ
    frοm. cheers

  • yoս’re truly ɑ excellent webmaster. The site loading speed іs amazing. It seems that үߋu’гe doing аny unique trick.
    Ꭺlso, The contents are the masterpiece. yօu have done a magnificent job on the matter!

  • 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.

  • 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?

  • you’re in point oof fact a good webmaster. Thee web site loading velocity
    іs incredible. It kіnd off feels tһat yߋu aге ԁoing аny distinctive trick.

    Мoreover, Τhe cօntents are masterpiece.
    you һave done ɑ magnificent task in thіs subject!

  • 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.

  • Greetings! I’ve been reading your weblog for some time now and finally got
    the courage to go ahead and give you a shout out from
    Kingwood Texas! Just wanted to tell you keep up the excellent work!

  • Having read this I thought it was extremely informative.
    I appreciate you spending some time and effort to put this article together.
    I once again find myself personally spending a significant amount of
    time both reading and posting comments. But so
    what, it was still worth it!

  • 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.

  • 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

    Subscribe to our mailing list


    %d bloggers like this: