You are here: Home / Debian GNU/Linux / Servers / HTTP / Install and setup Varnish cache on Debian

Install and setup Varnish cache on Debian

by Pierre-Yves Landuré last modified Nov 11, 2017 09:45

Varnish is a cache server helping to speed-up web pages loading. It is particularly suited to the optimization of sites managed by a Content Management System (CMS).

This howto is tested on:

  • Debian 6.0 Squeeze

Configuration description

This howto setup a Varnish configuration where each clusted is linked to a listening port. With each cluster addition, a listening port is added. Connect to the cluster listening port to access to its contents.

The cache purge is allowed for the local host.

The configuration below is tested with Plone (and plone.app.caching add-on) and Wordpress (and W3 Total Cache extension).

Installation

Parameters

The Accept-Language header impact the cache Varnish. Restricting available languages ​​to those of hosted sites limit this impact. Provide the language replacing languages unsupported by the hosted sites:

DEFAULT_LANGUAGE="en"

Provide the comprehensive list of hosted sites' supported languages (separated by espaces):

SUPPORTED_LANGUAGES="en fr"

Setting-up the Varnish server

Install the software needed to set-up the repository:

command apt-get install lsb-release

Add the software repository to apt configuration:

command echo "# Varnish cache repository.
deb http://repo.varnish-cache.org/debian/ $(command lsb_release -s -c) varnish-3.0" \
> '/etc/apt/sources.list.d/varnish.list'

Add the repository signature public key to apt configuration:

command wget "http://repo.varnish-cache.org/debian/GPG-key.txt" \
--quiet --output-document=- \
| command apt-key add -

Update available packages list

command apt-get update

Install the software:

command apt-get install varnish

Make sure the server is stopped:

/etc/init.d/varnish stop

Download the configuration template:

command wget 'http://howto.biapy.com/fr/debian-gnu-linux/serveurs/http/installer-et-configurer-le-cache-varnish-sur-debian/varnish.vcl/at_download/file' \
--quiet --output-document='/etc/varnish/varnish.vcl'

Setup the daemon to use this template:

command echo "
## Start Varnish with varnish.vcl configuration.
START='yes'
DAEMON_OPTS=\"-a :6081 \\
-T localhost:6080 \\
-f /etc/varnish/varnish.vcl \\
-S /etc/varnish/secret \\
-s file,/var/lib/varnish/\${INSTANCE}/varnish_storage.bin,1G\"" \
  >> '/etc/default/varnish'

Setting-up the varnish-accept-language filter

The varnish-accept-language filter normalize the Accept-Language header to minimize the impact of the user language on the cache.

Install the needed software:

command apt-get install perl gcc make unzip

Download the filter sources:

command wget 'https://github.com/cosimo/varnish-accept-language/zipball/master' \
     --quiet --output-document='/tmp/varnish-accept-language.zip'

Extract the archive:

command unzip -d '/tmp' '/tmp/varnish-accept-language.zip'

Go to the created folder:

VARNISH_ACCEPT_LANGUAGE_FOLDER="$(command find '/tmp' -type d -name 'cosimo-varnish-accept-language*' | command head -n 1)"
pushd "${VARNISH_ACCEPT_LANGUAGE_FOLDER}"

Build the filter:

command make DEFAULT_LANGUAGE="${DEFAULT_LANGUAGE}" SUPPORTED_LANGUAGES="${SUPPORTED_LANGUAGES}"

Install the filter file in the Varnish configuration folder:

command cp "${VARNISH_ACCEPT_LANGUAGE_FOLDER}/accept-language.vcl" '/etc/varnish/'

Leave the folder:

command popd

Delete the folder and the archive:

command rm -r "${VARNISH_ACCEPT_LANGUAGE_FOLDER}"
command rm '/tmp/varnish-accept-language.zip'

Configuration

Parameters

Provide IP addresses and ports of servers constituting the cluster (list of server:port, separated by spaces):

CLUSTER_SERVERS="127.0.0.1:8080 127.0.0.1:8081"

Set-up

Detect the first available port for a cluster:

CLUSTER_PORT=6081
while [ -n "$(command grep "director cluster_${CLUSTER_PORT}" '/etc/varnish/varnish.vcl')" ]; do
  CLUSTER_PORT=$((${CLUSTER_PORT} + 1))
done

Set-up the daemon to listen on the port:

if [ -z "$(command grep "^DAEMON_OPTS=" '/etc/default/varnish' \
| command tail -n 1 | command grep ":${CLUSTER_PORT}")" ]; then
  command sed -i -e "s/-a[ \t]*/-a :${CLUSTER_PORT},/" '/etc/default/varnish'
fi

Create the cluster configuration for the port:

if  [  -z "$(command grep "director cluster_${CLUSTER_PORT}" '/etc/varnish/varnish.vcl')" ]; then
CLUSTER_CONFIGURATION="director cluster_${CLUSTER_PORT} round-robin {\\
"
for SERVER in ${CLUSTER_SERVERS}; do
SERVER_HOST="$(command echo "${SERVER}" | command cut --delimiter=':' --fields=1)"
SERVER_PORT="$(command echo "${SERVER}" | command cut --delimiter=':' --fields=2)"
CLUSTER_CONFIGURATION="${CLUSTER_CONFIGURATION} { .backend = { .host = \"${SERVER_HOST}\"; .port =\"${SERVER_PORT}\";\\
.probe = {\\
.url = \"/\"; .interval = 5s; .timeout = 1 s;\\
.window = 5; .threshold = 3; }\\
.connect_timeout = 600s; .first_byte_timeout = 600s;\\
.between_bytes_timeout = 600s;\\
} }\\
"
done
CLUSTER_CONFIGURATION="${CLUSTER_CONFIGURATION}}\\
"
command sed -i -e "/CLUSTER CONFIGURATION END/i\\
${CLUSTER_CONFIGURATION}" '/etc/varnish/varnish.vcl'
fi

Associate the cluster to the port:

if  [  -z "$(command grep "server.port == ${CLUSTER_PORT}" '/etc/varnish/varnish.vcl')" ]; then
command sed -i -e "/CLUSTER SELECTION END/i\\
if ( server.port == ${CLUSTER_PORT} ) {\\
set req.backend = cluster_${CLUSTER_PORT};\\
}\\
" '/etc/varnish/varnish.vcl'
fi

Reload the server configuration:

/etc/init.d/varnish restart

Display the cache server listening port used to host the cluster:

echo "## Use this port to access Varnish for the cluster:
    ${CLUSTER_PORT}"

References

These books can help you:

Thanks