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.
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.
This post assumes that you have installed a LAMP or LEMP server. If not, please install one of those stacks before proceeding.
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:
- Debian: 3.0.2-2+deb7u1
- Ubuntu: 3.0.2-1
- 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