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
Best Affordable Linux and WordPress Services For Your Business
Outsource Your Linux and WordPress Project and Get it Promptly Completed Remotely and Delivered Online.

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

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Get your own self-hosted blog with a Free Domain at ($3.45/month).
  4. Become a Supporter - Make a contribution via PayPal
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

We are thankful for your never ending support.

Gabriel Cánepa

Gabriel Cánepa is a GNU/Linux sysadmin and web developer from Villa Mercedes, San Luis, Argentina. He works for a worldwide leading consumer product company and takes great pleasure in using FOSS tools to increase productivity in all areas of his daily work.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

37 Responses

  1. Andy says:

    I followed all your steps but kept getting 503 service unavailable after reboot. I tried few combinations ( setting KeepAlive off for Apache; increasing the timeout etc. )but that didn’t help. Ultimately had to go back to apache listening on port 80.

  2. Mayuresh Mulye says:

    What do you think about Nginx vs Varnish ?

    • Steacy says:

      Nginx and Varnish are not the same thing… you should read about them a bit! But you could use them on the same server. But with benchmark Apache and varnish is better than Nginx and varnish… but lots of things also depends on number of visitor, server configurations and server materials ;)

  3. reza says:

    hello

    it’s good idea but when we install the varnish , that give high resource.
    my idea is xcache installation in cpanel.
    and yours?
    thanks

  4. Mayuresh Mulye says:

    Excellent article !!!
    Thanks you very much….was fun testing on AWS too :)

  5. shraddhesh says:

    We have configure SSL so what setting we have to do in below content ?

    backend default {
    .host = “127.0.0.1”;
    .port = “80”;
    }

    OR
    backend default {
    .host = “SERVER-IP-ADD”;
    .port = “443”;
    }

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

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

Leave a Reply to Ravi Saive Cancel reply

Your email address will not be published. Required fields are marked *

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