I have been working with Varnish a number of years now. It's truly an amazing open source product for web acceleration.
I want to start this blog entry with my opinion on Varnish and Apache before I get into the details of how I made it all work together. Sure, there is lightspeed, there is nginx and a dozen other web servers out there that all claim to be the fastest, work with Varnish, etc. If you are one of those people that feel you'll get better performance without using Apache then go for it. I personally am very familiar with Apache. I use Apache every day and my preference is to use that since it is a mature product with a huge amount of support and options. The others while new and fast have their own share of trouble. That being said my approach here was to improve a web server with Varnish and leave Apache in place to do what it has been doing very well for a very long time. This article is not pro-Apache it is simply an illustration on how a minimal amount of work can get you huge performance gains on modest hardware.
So let's talk about the modest hardware I have here. I have a customer who has a dedicated VPS and I provide the support and management of the hosting.
The hardware used:
- 8 Cores - Intel(R) Xeon(R) CPU L5506 @ 2.13GHz
- The hypervisor is Parallels so the Kernel and other OS components are in shared memory not specifically the memory of my VPS. This allows you to have more ram available.
- 4GB Ram available to VPS (originally 2GB)
- 120GB Hard Disk
- MySQL 5.1
- PHP 5.4.x
- Apache 2.2
- varnishd (varnish-3.0.5 revision 1a89b1f)
- WHM 11.42.1 with cPanel
- CENTOS 5.9 i686 virtuozzo
- Wordpress MU
sudo yum install varnish
sudo python installvarnishNow it will check dependencies and install a huge amount of .vcl files for varnish. When I checked out how many configuration files were out there after this application did its thing I had new respect for how much time and effort went into the building of this. The $6/mo is a steal for the time and effort you will save with this.
Note, once you perform this process Varnish will be running and active on your system. It will NOT however cache anything and it performs in a pass through mode doing nothing at all to your system.
Once it is installed successfully, you'll need to go into WHM and set it up. You can find it by searching for varnish in WHM.
If you are this far it's time to get this baby running. I'll now go through the simple configuration values that will get your server some huge performance gains.
You need to go here and turn on the Malloc setting if you want to use the varnish memory. This will then tell varnish to use memory vs. file cache for performance. Make absolutely sure that the memory you allocate is always available. If its not then you'll likely have Varnish, MySQL or Apache crash on you.
Browse the site, use the admin, check to see that changes are being reflected on the site.
A few tips on using Varnish long term.
- Version all your dynamic content, e.g. images, CSS, JS, etc. This way you never have an old version stuck in cache.
- Use a CSS and JS Minifier. Generally speaking they will change the filename each time they detect a change and this works the same as file versioning.
- Never upload a replacement image. Change the filename, use myimage-1.png or what not. This will prevent you from having varnish cache the wrong image for hours or days.
- Flush cache in off hours. If you need to flush your varnish cache remember that when you do the site is going to suddenly get a huge burst of traffic to Apache to rebuild that cache.
- Monitor everything. Keep track with tools like Cacti to see what performance improvements you made.
- cPanel users are going to get some free bandwidth as part of this. The reason being is that PHP/Apache is not being called and thus the cPanel hooks to track traffic are not there. So cache items don't count towards the overall statistics for a user. However YOUR traffic will be counted. Service providers meter your data usage differently. So make sure you're no where near your bandwidth cap before you implement this.