Noupe Editorial Team March 13th, 2013

Varnish – Free Cache-Server Extraordinaire Speeds Up Your Website

Complex web apps and high traffic spell doom to many a web server. Web as well as database server have to put together the right data at each new visit to deliver it as renderable HTML. No wonder caching solutions have become popular throughout the last few years. Caching means storing already deliverable content in memory instead of compiling it anew at each new hit. The duration or lifetime of cached content is configurable and should be fine-tuned according to the needs of your web project from minutes to hours to days. Quite a few web apps already are equipped with some kind of caching strategy. If you want the real deal, say a speed boost of 300 to 1,000 times faster, dedicated solutions should be considered. varnish_speed_up-w550

Photo by on flickr under Creative Commons License

Varnish is easily installed and useful even with its default configuration

Varnish - what sounds like an aggressive detergent in fact is a lightning-fast cache-software. If you think twice you realize that varnish is just what the cache-software does to your web project. Speed boosts from 300 to 1,000 times are promised by its creators. Varnish focuses, other than most other caches, solely on HTTP, which is what the common visitor will experience perceivably. Other than you might think, getting started with Varnish is easy. What you really need to make use of Varnish is a web server (obviously) running Linux. Root is only necessary at installation time. As Varnish keeps the whole cache in the memory you should have quite a bit of it, the more the better. There is no technical minimum, but I'd say 2 GB should always be there, even for smaller projects. The project offers several ready-made installation packages. Ubuntu
curl http://repo.varnish-cache.org/debian/GPG-key.txt | sudo apt-key add -
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
CentOS/Fedora
rpm --nosignature -i http://repo.varnish-cache.org/redhat/varnish-3.0/el5/noarch/varnish-release-3.0-1.noarch.rpm
yum install varnish
That's about it. The cache server should have gotten installed without any error messages. It does not yet function, though. Delivery of web content is still done by our Apache or any other web server you might be using. We'll have to change the configuration slightly, to make sure, that Varnish takes the place of the main actor, only making exceptional recourses to our web server if necessary. Fortunately, the changes are few. Varnish's main configuration file is stored under /etc/default/varnish. The only necessary change is to remove the quotation marks from "DAEMON_OPTS". Afterwards the file should look like this:
DAEMON_OPTS="-a :80 \
-T localhost:6082 \
-f /etc/varnish/default.vcl \
-S /etc/varnish/secret \
-s malloc,256m"
A second step is needed, where we tell our cache-server via default.vcl under /etc/varnish/default.vcl where our web server is running. The web server can be addressed from the same machine, but also externally. So you could think about setting up an exclusive cache-server.
backend default {
.host = "127.0.0.1";
.port = "8080";
}
Webmasters will notice that we changed the web server's port to 8080, which usually isn't the case, at least not in standard settings. To get this configuration running we need to take one last step and tell the web server that it shall be running on 8080 from now on. This has to be done in the web server's config file. Apache has it at /etc/apache2/ports.conf.
NameVirtualHost 127.0.0.1:8080
Listen 127.0.0.1:8080
Important: Don't forget to change the vhost configuration, too, to reflect the port change. Otherwise, your Apache will not function flawlessly. One more thing: after a reboot of both Varnish and your Apache or whatever web server you are using, everything should be running smoothly. You'll soon see, that Varnish is a speed wonder. Being extremely fast it can be recommended for use in the biggest of projects. Before you think of investing into new hardware, think of using Varnish. Its effects can make buying new machines obsolete to a certain extent. For a more detailed guide to how you can fine-tune the freely available software, turn to the website of its developers and their extensive documentation.

Photo Credits: Éole via photopin cc Article written by Adrian Bechtold and first published in our German sister publication Dr. Web Magazin

(dpe)

Noupe Editorial Team

The jungle is alive: Be it a collaboration between two or more authors or an article by an author not contributing regularly. In these cases you find the Noupe Editorial Team as the ones who made it. Guest authors get their own little bio boxes below the article, so watch out for these.

3 comments

  1. We’re running NGINX+php-fpm+APC with W3 Total Cache for WP. I’ve installed Varnish which it isn’t configured yet, but even switching from Apache to NGINX has made a huge difference, especially during traffic spikes.

    I really want to get Varnish up and running to see how much more efficient we can make this stack.

  2. Pingback: The WordPress Weekend Roundup - WP Daily

Leave a Reply

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