How to Install ‘Varnish’ (HTTP Accelerator) and Perform Load Testing Using Apache Benchmark

Think for a moment about what happened when you browsed to the current page. You either clicked on a link that you received via a newsletter, or on the link on the homepage of Tecmint.com, and then were taken to this article.

In few words, you (or actually your browser) sent a HTTP request to the web server that hosts this site, and the server sent back a HTTP response.

Install Varnish Cache for Apache and Nginx

As simple as this sounds, this process involves much more than that. A lot of processing had to be done server-side in order to present the nicely formatted page that you can see with all the resources in it – static and dynamic. Without digging much deeper, you can imagine that if the web server has to respond to many requests like this simultaneously (make it only a few hundred for starters), it can either bring itself or the whole system to a crawl before long.

And that is where Varnish, a high-performance HTTP accelerator and reverse proxy, can save the day. In this article I’ll explain how to install and use Varnish as a front-end to Apache or Nginx in order to cache HTTP responses faster and without placing further load on the web server.

However, since Varnish normally stores its cache in memory instead of on disk we will need to be careful and limit the RAM space allocated for caching. We will discuss how to do this in a minute.

How Varnish Works
How Varnish Works

Installing Varnish

This post assumes that you have installed a LAMP or LEMP server. If not, please install one of those stacks before proceeding.

  1. Install LAMP in CentOS 7
  2. Install LEMP in CentOS 7

The official documentation recommends installing Varnish from the developer’s own repository because they always provide the latest version. You can also choose to install the package from your distribution’s official repositories, although it may be a little outdated.

Also, please note that the project’s repositories only provide support for 64-bit systems, whereas for 32-bit machines you’ll have to resort to your distribution’s officially maintained repositories.

In this article we will install Varnish from the repositories officially supported by each distribution. The main reason behind this decision is to provide uniformity in the installation method and ensure automatic dependency resolution for all architectures.

On Debian and Ubuntu
# aptitude update && aptitude install varnish 	[preface each command with sudo on Ubuntu]
On RHEL, CentOS and Fedora

For CentOS and RHEL, you will need to enable the EPEL repository before installing Varnish.

# yum update && yum install varnish 

If the installation completes successfully, you will have one of the following versions depending on your distribution:

  1. Debian: 3.0.2-2+deb7u1
  2. Ubuntu: 3.0.2-1
  3. Fedora, CentOS, and RHEL (the version is the same as Varnish is available from the EPEL repository): v4.0.2

Finally, you need to start Varnish manually if the installation process didn’t do it for you, and enable it to start on boot.

On Debian, Ubuntu, CentOS/RHEL 6.x and Fedora 15-20
# service varnish start
# service varnish status
# chkconfig --level 345 varnish on
On CentOS/RHEL 7.x and Fedora 21
# systemctl start varnish
# systemctl status varnish
# system enable varnish
If you liked this article, then do subscribe to email alerts for Linux tutorials. If you have any questions or doubts? do ask for help in the comments section.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

37 thoughts on “How to Install ‘Varnish’ (HTTP Accelerator) and Perform Load Testing Using Apache Benchmark”

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

    Reply
  2. 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..

    Reply
  3. 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

    Reply
  4. 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

    Reply
  5. 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

    Reply
  6. 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.

    [email protected]:~# 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

    [email protected]:~#

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

    Reply

Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.