Did You Know?
Got a tip? Let us know

LFCS - Linux Foundation Certified SysAdmin - Exam Preparation Guide

How to Install Varnish Cache (Web Accelerator) in RHEL/CentOS/Fedora and Ubuntu/Debian

Download Your Free eBooks NOW - 10 Free Linux eBooks for Administrators

Varnish Cache is an open source web application accelerator also referred to as a HTTP accelerator or caching HTTP reverse proxy, that developed for busy and heavy content sites, to significantly improve performance of web sites. Varnish can be installed as an front end to any server that serves HTTP requests and configure it to cache the pages. It makes your website really fast and accelerate your web site performance up-to 300 – 1000x (means 80% or more), depending upon your system architecture.

Install Varnish Cache for Apache

Install Varnish Cache Web Accelerator

How does it work?

When a user visits the website first time, varnish keeps the copy of that served page, and when a user re-visit the same page again, it will serve the saved copy page instead of re-requesting the page from web server. This way it improves your website’s performance and scalability and also able to handle high traffic and also saves server bandwidth.

This guide is intended for those web server administrators who wanting to install Varnish Cache on their Linux systems. The administrator must have a basic knowledge on how to configure and install a web server and little bit understanding of HTTP protocol.

Installing Varnish Cache Web Accelerator

Varnish Cache is distributed under the EPEL (Extra Packages Enterprise Linux) package repositories. But, epel allows to distribute new versions only and it does not distribute backwards-incompatible changes. Due to this, latest version not available under EPEL repository. If you were looking for latest major version of Varnish, then I recommend you to use the repository provided from varnish-cache.org. To add and enable Varnish repository run the following commands.

For RHEL / CentOS / Fedora
# wget http://repo.varnish-cache.org/redhat/varnish-3.0/el5/noarch/varnish-release-3.0-1.noarch.rpm
# rpm --nosignature -i varnish-release-3.0-1.noarch.rpm

Once, repository added, then run the following command to install the Varnish.

# yum install varnish
For Ubuntu

Varnish is distributed under Ubuntu repositories package, but the available version might be out of date. So, I generally suggest you to use the repository provided by varnish-cache.org.

# wget http://repo.varnish-cache.org/debian/GPG-key.txt 
# apt-key add GPG-key.txt 
# echo "deb http://repo.varnish-cache.org/ubuntu/ precise varnish-3.0" | sudo tee -a /etc/apt/sources.list
# sudo apt-get update
# sudo apt-get install varnish
For Debian

Varnish Cache is also distributed under Debian package repositories, but the available version might be out of date and I recommend you to use the repository provided by varnish-cache.org.

# wget http://repo.varnish-cache.org/debian/GPG-key.txt 
# apt-key add GPG-key.txt 
# echo "deb http://repo.varnish-cache.org/debian/ squeeze varnish-3.0" >> /etc/apt/sources.list
# apt-get update
# apt-get install varnish
Start Varnish Web Accelerator

To start Varnish at system start-up, type the following command to make it start at system boots.

# chkconfig --level 345 varnish on

Next, run the following command to start the Varnish first time.

# /etc/init.d/varnish start

Basic Varnish Configuration for Apache

Varnish provides three main configuration files. The start-up script, the system-wide configuration, and the VCL configuration file.

The first start-up script located at /etc/init.d/varnish. This file is used to start or stop varnish service, its rarely needs configuration.

The second system wide configuration file is located under /etc/sysconfig/varnish (on RHEL/CentOS/Fedora machines) or /etc/default/varnish (on Ubuntu and Debian systems). This file is the main global configuration file for Varnish, it has information of port number that used to run varnish on port 6081 and stores the cache.

The third file is basic VCL configuration file located at /etc/varnish/default.vcl. This VCL file holds most of varnish configuration, but we use the default configuration for communicate with Apache on port 80. The basic configuration look like this.

backend default {
    .host = "127.0.0.1";
    .port = "80";
}

The above basic configuration enable varnish to cache all files. To enable caching restart the both Apache and Varnish services to reflect changes.

# /etc/init.d/httpd restart
# /etc/init.d/varnish restart

Next, verify the Varnish by running the following command.

# curl -I http://tecmint.com

HTTP/1.1 200 OK
Server: Apache/2.2.15 (CentOS)
X-Powered-By: PHP/5.3.3
Vary: Accept-Encoding
Cache-Control: max-age=7200, must-revalidate
Content-Type: text/html; charset=UTF-8
Date: Mon, 25 Feb 2013 17:01:57 GMT
X-Varnish: 796827437
Age: 0
Via: 1.1 varnish
Connection: keep-alive
Useful Commands for Monitoring Varnish

Following are the some useful and helpful Varnish Cache commands for monitoring.

# varnishstat : Provides all the info you need to spot cache misses and errors.
# varnishhist : Provides a histogram view of cache hits/misses.
# varnishlog : Provides detailed information on requests.
# varnishtop : It reads varnishd shared memory logs and presents a most commonly occurring log entries.
# varnishadm : Command-line varnish administration used to reload vcl and purge urls.

Ravi Saive

Owner at TecMint.com
Simple Word a Computer Geek and Linux Guru who loves to share tricks and tips on Internet. Most Of My Servers runs on Open Source Platform called Linux.
Download Free eBooks
Advanced Bash-Scripting Guide
Linux Bible
A Newbie's Getting Started Guide to Linux
Ubuntu Linux Toolbox: 1000+ Commands

30 Responses

  1. Anton says:

    And how it to bridge the nginx?

  2. Wingnux says:

    This is what happens on Ubuntu 12.10 64bit:

    wingnux@wingnux-desktop:~$ chkconfig –level 345 varnish on
    The program ‘chkconfig’ is currently not installed. You can install it by typing:
    sudo apt-get install chkconfig
    wingnux@wingnux-desktop:~$ sudo apt-get install chkconfig
    Reading package lists… Done
    Building dependency tree
    Reading state information… Done
    Package chkconfig is not available, but is referred to by another package.
    This may mean that the package is missing, has been obsoleted, or
    is only available from another source

    E: Package ‘chkconfig’ has no installation candidate

  3. nima says:

    i see you have migrate from varnish. reason?
    also curl -I will show my website php version how can i disable it?
    also curl -I do not show varnish on command output

    • Ravi Saive says:

      Thanks for suggestion. If you want to disable your php version and info just change line that says in your php.ini file.

      disable_functions = phpinfo
      expose_php = Off
      

      If you not getting varnish info may be you not installed varnish correctly.

  4. can you provide us zimbra installation on 32 bit and 63 bit

    postfix dovecot with amavis clamd

  5. Paulo Silva says:

    I have tried Varnish but i don’t see any changes on speed of my sites.

    I am usuing a Linode VPS withphp, suphp, apache 2.2, centos 5.8 and kloxo.

    The result of curl -I http://google.com is :
    HTTP/1.1 301 Moved Permanently
    Location: http://www.google.com/
    Content-Type: text/html; charset=UTF-8
    Date: Sun, 17 Mar 2013 02:58:02 GMT
    Expires: Tue, 16 Apr 2013 02:58:02 GMT
    Cache-Control: public, max-age=2592000
    Server: gws
    Content-Length: 219
    X-XSS-Protection: 1; mode=block
    X-Frame-Options: SAMEORIGIN

    What can i do more?

    • Ravi Saive says:

      If you are looking for better optimization in speed, you need to check out /etc/varnish/default.vcl configuration file and make necessary changes for this i recommend you to read more on varnish configuration page.

  6. Paulo Silva says:

    It is working but just don’t cache anything…

    I think this just work good with static html pages, not with dynamic pages, like the ones used in the onlines stores i have in my server.

  7. This was surely an interesting and insigtful glimpse into
    your world. Do you think you’re setting up to put in writing some far more during the future?

  8. Samer says:

    thaaaaaaaaaaaaaaaaaaank you very much. You helped me aloooooooooot

  9. Michael says:

    Varnish seems to be easier to use and to configure and install unlike the rest.

  10. Intasar says:

    Hello,

    Its just the installation process. you have not telling us the configuration of Varnish so it work with Apache 2.2.25. because default installation doesn’t integrate varnish server into Apache as both apache and varnish listen 80 port.

  11. Bestretail says:

    Hello Team,

    Why when i try the command to verify the vanish on server. The connection status is ” Close ”

    root@bestretails varnish]# curl -I http://www.bestretails.com
    HTTP/1.1 200 OK
    Date: Thu, 26 Dec 2013 03:25:04 GMT
    Server: Apache/2.2.15 (CentOS)
    X-Pingback: http://www.bestretails.com/xmlrpc.php
    Link: ; rel=shortlink
    Connection: close
    Content-Type: text/html; charset=UTF-8

    Kindly need advise.

    Regards

  12. Mohsin says:

    Hi,

    Thanks for the information

    Is both Apache and Varnish will run on same port that 80, Do we need to change the port for Apache other than 80, So that Varnish will run on 80 and Apache run other than 80

    Thanks
    Mohsin

  13. Veli says:

    I have tried Varnish cache too. My website is still very slow. What could be problem here. I have apache2 server. I think Debian 3.2.46-1+deb7u1 x86_64

    Thanks for your help.

    root@km34416:~# curl -I http://newinternetshopping.com
    HTTP/1.1 200 OK
    Server: nginx
    Date: Mon, 06 Jan 2014 17:43:09 GMT
    Content-Type: text/html; charset=UTF-8
    Connection: keep-alive
    Expires: Thu, 19 Nov 1981 08:52:00 GMT
    Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
    Pragma: no-cache
    X-Pingback: http://newinternetshopping.com/xmlrpc.php
    Set-Cookie: PHPSESSID=526d06842a6c6c4e484b7005062ac49f; path=/
    Set-Cookie: kpg_stop_spammers_time=1389030189; expires=Mon, 06-Jan-2014 17:44:09 GMT
    Set-Cookie: wfvt_3488105620=52caeb2d105ef; expires=Mon, 06-Jan-2014 18:13:09 GMT; path=/
    Set-Cookie: slimstat_tracking_code=790638id.ed22868133e1b023ab189d537d4dbeb5; expires=Thu, 06-Feb-2014 17:43:09 GMT; path=/
    X-Powered-By: PleskLin
    Vary: Accept-Encoding

    root@km34416:~#

  14. Agus says:

    hi
    thanks for great articles
    i have install but i didn’t see varnish is working on my server

    [root@us1 download]# curl -I http://tecmint.com
    HTTP/1.1 301 Moved Permanently
    Date: Thu, 23 Jan 2014 04:42:31 GMT
    Server: Apache
    Location: http://www.tecmint.com/
    Cache-Control: max-age=86400
    Expires: Fri, 24 Jan 2014 04:42:31 GMT
    Vary: Accept-Encoding
    Content-Type: text/html; charset=iso-8859-1

  15. aarif says:

    Hi,

    I have varnish running but I am confused regarding AGE. As I can see when you checked if you varnish was running or not, it should “AGE 0″. I have the same problem, it shows me “age 0″.

    I would like to increase my age, I don’t know if its feasible to increase age or not.

    I have seen some other sites which “age” changing frequently.

    Cheers,
    Aarif

  16. Thomas says:

    I followed the instructions and it does not say that varnish is running on my site. Not sure what I missed.

  17. vyas says:

    default varnish starts on port 6081. so first of all you can check the status of port via telnet.

    to test the varnish whether it is caching or not. please follow below steps.

    GET -Used http://localhost:6081/

    you will get below response including varnish in last line

    GET http://localhost:6081/
    User-Agent: lwp-request/2.07

    200 OK
    Connection: close
    Date: Wed, 05 Mar 2014 12:16:09 GMT
    Via: 1.1 varnish
    Accept-Ranges: bytes
    Age: 94
    ETag: “7a857f-1c-fa3243c0″
    Server: Apache/2.2.3 (Red Hat)
    Content-Length: 28
    Content-Type: text/html; charset=UTF-8
    Last-Modified: Tue, 25 Sep 2012 09:03:19 GMT
    Client-Date: Wed, 05 Mar 2014 12:16:09 GMT
    Client-Peer: 127.0.0.1:6081
    Client-Response-Num: 1
    X-Varnish: 1951550557 1951550551

    first our request will hit to 6081 and internally connect to 80 as per the configuration /etc/varnish/default.vcl

    You can also install varnish on 80 and apache on another port

  18. Mike says:

    Hi,

    Good article. We are running varnish on redhat using RPM packages. Now we have a been tasked with installing additional VMods to extend the functionality of varnish. From what I have read this morning these VMods are compiled using the varnish code base ?

    Is there an alternate method for installing VMods into a varnish server that is managed with RPMs ? A link to a page with instructions would be super !

    Cheers

  19. Ravi Saive says:

    I’ve checked the configuration, seems correctly configured, may I know while browsing site, are you getting any error message. If possible can you send me a link to that error page or screen grab for better understanding..

  20. sohail says:

    I have installed varnish several times but it is not working. when I use curl -I localhost. It shows 403 error at the start.

    Kindly help me in resolving this issue

  21. RS says:

    Is this just for hosting a website? OR can it be used to cache all pages users are browsing. I.e users inside a LAN browsing out to the WWW?
    Also can it pre-fetch? I.e. every hour get the pages users have visited on facebook, youtube, (or whatever changes regularly) and update the cache with the newly added posts or videos.

    Thanks

  22. Hadi says:

    Hello,
    It was very usefull article;
    I have installed on my server and work great for me
    http://www.irwebhost.com
    Thank you

Leave a Reply

This work is licensed under a (cc) BY-NC | TecMint uses cookies. By using our services, you comply to use of our cookies. More info: Privacy Policy.
© 2012-2014 All Rights Reserved.