Simple varnish setup for Drupal 7 sites
To further improve the performance of my Drupal 7 site I decided to experiment with the excellent caching HTTP reverse proxy, Varnish.
In an ideal world, with limitless budget etc, I would have Varnish running on a seperate server (or servers) but for now it is installed on the same server as apache, mysql and php.
As I was using Centos 6 I downloaded from the below repository - note the release version number will probably change:
rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-5.noarch.rpm
Before using yum to install varnish, I configured the apache virtual hosts to listen to port 8080 instead of port 80.
This was done by amending the appropriate parts of the configuration file to read:
Listen 8080 NameVirtualHost *:8080
For each virtual host I amended the first line to read:
I can now install and configure Varnish!
To install type:
yum install varnish Auto start varnish sudo /sbin/chkconfig varnish on --level 2345
To configure Varnish, amend the Varnish default configuration file located here:
DAEMON_OPTS="-a :80 \ -T localhost:6082 \ -f /etc/varnish/default.vcl \ -u varnish -g varnish \ -S /etc/varnish/secret \ -s file,/var/lib/varnish/varnish_storage.bin,512MB"
This tells Varnish to listen on port 80. Again in an ideal world, if I had a dedicated server with large amounts or RAM, I would have Varnish serving content from memory rather than a file, to further increase performance.
Next is to setup the default vcl file:
Restart apache and varnish
Finally to get apache and Varnish working with the amended configuration restart apache and varnish:
service httpd restart service varnish restart
This is a very basic, proof of concept, which will need to be built apon and improved for a production site. But hopefully this helps to understand the basics.
There are further things you will need to look at:
For example, you will need to review and possibly update the cookie removal part, if you have cookies set other than has_js and Google Analytics. If a cookie is set Varnish does not serve a cached page.
Also, Varnish allows you to have different vcl files for individual sites, which might be useful to use.
If you are using a virtualisation platform such as OpenVZ, you will need to reduce to maximum stack size.