Thursday, July 24, 2014

Installing Varnish cache in Centos 6

download you varnish cache from their official website
https://www.varnish-cache.org/releases
click on which OS you using, for mine, i using Centos, so i going to use red hat
once you click on it, it should be got 2 line command to install the repo so you can just install using yum

# rpm --nosignature -i https://repo.varnish-cache.org/redhat/varnish-4.0.el6.rpm
# yum install varnish

there will be got 3 file need to edit

  1. /etc/varnish/default.vcl
  2. /etc/sysconfig/varnish
  3. /etc/httpd/conf/httpd.conf

1. edit the file /etc/varnish/default.vcl
this file is to connect varnish cache to your apache. for me, i will decide to let apache use port 8080 while give varnish to use port 80. so for my configuration, i just change to this


backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

2. edit the file /etc/sysconfig/varnish
you should be notice that inside this file, it got 3 alternative way to setup the config.
by default, it will use alternative 3, but for me, i prefer to use alternative 2 because simple
all you need to do is remove alternative 3 and enable alternative 2

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,254M"

for the option, i set varnish to use port 80
set localhost:6082 as listening address and port
set to read option we set at /etc/varnish/default.vcl
set to use user varnish and group varnish when start up
and set the cache file of 254M

you can increase the cache file size according to you case

3. edit the /etc/httpd/conf/httpd.conf
at here, we will set apache to use port 8080

Listen 8080


with all that done
you can try start up your varnish and httpd

# /etc/init.d/varnish start
# /etc/init.d/httpd start
you can verify it is running on varnish by using this command

# curl -I <your-server-IP>

you should see something like this mention varnish name

HTTP/1.1 200 OK
Date: Wed, 23 Jul 2014 18:35:53 GMT
Server: Apache/2.2.15 (CentOS)
Last-Modified: Tue, 22 Jul 2014 20:07:18 GMT
ETag: "2032a-e-4fecdc55090a2"
Content-Type: text/html; charset=UTF-8
X-Varnish: 19
Age: 0
Via: 1.1 varnish-v4
Content-Length: 14
Connection: keep-alive

----------------------------------------------------------------------------------------------------
Useful command for varnish

# varnishstat : Provides all the info you need to spot cache misses and errors.
# varnishhist : Provides a histogram view of cache hits/misses.
# varnishlog : Provides detailed information on requests.
# varnishtop : It reads varnishd shared memory logs and presents a most commonly occurring log entries.
# varnishadm : Command-line varnish administration used to reload vcl and purge urls.
----------------------------------------------------------------------------------------------------
I still learning and will record down more on the option




No comments:

Post a Comment