You are here
Drupal 7.x, Varnish 3.x and Apache 2.x configuration - all in one post
In this blog post i’ll shortly describe how to configure Drupal 7.x, Varnish 3.x, Apache 2.x and also expose some possible issues.
Below you can check the points you should go through to get Varnish cache work with your Drupal installation:
- Install Varnish 3.x
- Update /etc/varnish/default.vls
- Update /etc/default/varnish
- Configure Apache
- Configure Drupal
- Possible issues
- Is it working?
Install Varnish 3.x
I believe the best Varnish installation tutorial published on the official website. This is a quote for your convenience:
Installation on 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
Make back-up and override your /etc/varnish/default.vls by the Fourkitchens default.vcl.
Open /etc/default/varnish file
$ sudo nano /etc/default/varnish
Uncomment Alternative 2, and change the listener port from 6061 to 80. The configuration should match the next snippet:
DAEMON_OPTS="-a :80 \ -T localhost:6082 \ -f /etc/varnish/default.vcl \ -S /etc/varnish/secret \ -s malloc,256m"
Configure ApacheSet Apache to listen on port 8080:
$ sudo nano /etc/apache2/ports.conf
NameVirtualHost 127.0.0.1:8080 Listen 127.0.0.1:8080
$ sudo nano /etc/apache2/sites-available/default
Do the same for all other vhosts as well.
Restart Apache and Varnish
$ sudo service apache2 restart $ sudo service varnish restart
Download and install Varnish HTTP Accelerator Integration module
$ drush dl varnish $ drush en varnish -y
Add the next code to the end of your settings.php file. Please check if relative path to varnish.cache.inc and secret files are correct:
$conf['cache_backends'] = array('sites/all/modules/contrib/varnish/varnish.cache.inc'); $conf['cache_class_cache_page'] = 'VarnishCache'; $conf['page_cache_invoke_hooks'] = FALSE; $conf['reverse_proxy'] = TRUE; $conf['cache'] = 1; $conf['cache_lifetime'] = 0; $conf['page_cache_maximum_age'] = 21600; $conf['reverse_proxy_header'] = 'HTTP_X_FORWARDED_FOR'; $conf['reverse_proxy_addresses'] = array('127.0.0.1'); $conf['omit_vary_cookie'] = TRUE; $conf['varnish_control_key'] = substr(file_get_contents('/etc/varnish/secret'),0,-1);
Set non-zero value on admin/config/development/performance for "Expiration of cached pages"
Be awarded that Varnish caching doesn’t work:
- on pages with messages
- if session or other cookies have being set
- if zero time for "Expiration of cached pages".
Is it working?
To check if Varnish caching works as expected please take a look on Headers:
String ‘X-Varnish-Cache: HIT’ means that caching works.