Varnish - Basic Install

Posted by Lucas Hedding on September 15, 2012

Steps to enable varnish on a server

Install varnish per your platform's standard install method. I installed it on a debian based system so I just ran

sudo apt-get install varnish

You'll now need to provide a varnish configuration & vcl that is customized for Drupal. There are some options that should be set when you start the daemon. Other's should go in the vcl. In our environment, we setup the default.vcl to read in a backend.vcl that is different for each environment based on backedn IP addresses, etc and a drupal.vcl that is identical for all environments.

# Configuration file for varnish
#
# /etc/init.d/varnish expects the variable $DAEMON_OPTS to be set from this
# shell script fragment.
#

# Maximum number of open files (for ulimit -n)
NFILES=131072

# Locked shared memory (for ulimit -l)
# Default log size is 82MB + header
MEMLOCK=82000

# Maximum size of corefile (for ulimit -c). Default in Fedora is 0
# DAEMON_COREFILE_LIMIT="unlimited"

# Set this to 1 to make init script reload try to switch vcl without restart.
# To make this work, you need to set the following variables
# explicit: VARNISH_VCL_CONF, VARNISH_ADMIN_LISTEN_ADDRESS,
# VARNISH_ADMIN_LISTEN_PORT, VARNISH_SECRET_FILE, or in short,
# use Alternative 3, Advanced configuration, below
RELOAD_VCL=1

# # Default address and port to bind to
# # Blank address means all IPv4 and IPv6 interfaces, otherwise specify
# # a host name, an IPv4 dotted quad, or an IPv6 address in brackets.
VARNISH_LISTEN_ADDRESS=
VARNISH_LISTEN_PORT=80
#
# # Telnet admin interface listen address and port
VARNISH_ADMIN_LISTEN_ADDRESS=
VARNISH_ADMIN_LISTEN_PORT=6082
#
# # Shared secret file for admin interface
VARNISH_SECRET_FILE=/etc/varnish/secret
#
# http://www.lullabot.com/articles/varnish-multiple-web-servers-drupal
# # The minimum number of worker threads to start
VARNISH_MIN_THREADS=400
#
# http://www.lullabot.com/articles/varnish-multiple-web-servers-drupal
# # The Maximum number of worker threads to start
VARNISH_MAX_THREADS=1000
#
# # Idle timeout for worker threads
VARNISH_THREAD_TIMEOUT=120
# # Main configuration file.
VARNISH_VCL_CONF=/etc/varnish/default.vcl
#
# # Cache file size: in bytes, optionally using k / M / G / T suffix,
# # or in percentage of available disk space using the % suffix.
VARNISH_STORAGE_SIZE=2G
#
# # Backend storage specification
VARNISH_STORAGE="malloc,${VARNISH_STORAGE_SIZE}"
#
# # Default TTL used when the backend does not specify one
VARNISH_TTL=120
#
# # Other parameters for Varnish.
VARNISH_CLI_TIMEOUT="cli_timeout=25"
VARNISH_SESS_TIMEOUT="sess_timeout=5"
VARNISH_SESS_WORKSPACE="sess_workspace=262144"

DAEMON_OPTS="-a ${VARNISH_LISTEN_ADDRESS}:${VARNISH_LISTEN_PORT} \
             -f ${VARNISH_VCL_CONF} \
             -T ${VARNISH_ADMIN_LISTEN_ADDRESS}:${VARNISH_ADMIN_LISTEN_PORT} \
             -t ${VARNISH_TTL} \
             -w ${VARNISH_MIN_THREADS},${VARNISH_MAX_THREADS},${VARNISH_THREAD_TIMEOUT} \
             -u varnish -g varnish \
             -S ${VARNISH_SECRET_FILE} \
             -s ${VARNISH_STORAGE} \
             -p ${VARNISH_CLI_TIMEOUT} \
             -p ${VARNISH_SESS_TIMEOUT} \
             -p ${VARNISH_SESS_WORKSPACE}"

Steps to enable varnish modules

Install several drupal modules to help manage the varnish caching.

References

  • Varnish -- provides a cache replacement for page cache and lets other modules communicate with the varnish servers admin interface.
  • Expire -- provides a means to expire the varnish cache when a node, entity, term, etc is edited.
  • Purge -- works with expire to ban items when they have expired.

Are you looking to build or customize a Drupal site? Write us about your project, and we’ll get back to you within 48 hours.

If you want to learn more about why Drupal is the most widely used open-source content management system in the world, visit drupal.org/about.