Šioje trumpoje pamokoje parodysime, kaip įdiegti ir paleisti
Varnish tarnybą susietą su
Apache, CentOS 6 aplinkoje.
1. Atsiunčiame ir įdiegiame reikalingą repozistoriją:
wget http://repo.varnish-cache.org/redhat/varnish-3.0/el5/noarch/varnish-release-3.0-1.noarch.rpm
rpm --nosignature -i varnish-release-3.0-1.noarch.rpm
2. Įdiegiame
Varnish:
yum install varnish -y
3. Nurodome paleisti
Varnish serverio paleidimo metu:
chkconfig --level 345 varnish on
4. Paleidžiame
Varnish tarnybą:
/etc/init.d/varnish start
5. Pakoreguojame "
/etc/varnish/default.vcl" faile žemiau nurodytas eilutes, nurodant mūsų pasirinktą 8080 prievadą, kurio klausysis Apache:
nano /etc/varnish/default.vcl
backend default {
.host = "127.0.0.1";
.port = "8080";
}
6. Pakoreguojame "
/etc/sysconfig/varnish" konfigūracinį failą. Pagal nutylėjimą šiame konfigūraciniame faile yra neužkomentuotas "
Alternative 3" skyrius - visas šiame skyriuje neužkomentuotų eilučių pradžiose pridedame komentaro ženklus. Ir konfigūracinio failo pabaigoje pridedame žemiau nurodytas eilutes".
nano /etc/sysconfig/varnish
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,1G"
Šis failas atlikus pakeitimus turi atrodyti taip:
# 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 number of threads (for ulimit -u)
NPROCS="unlimited"
# 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
# This file contains 4 alternatives, please use only one.
## Alternative 1, Minimal configuration, no VCL
#
# Listen on port 6081, administration on localhost:6082, and forward to
# content server on localhost:8080. Use a fixed-size cache file.
#
#DAEMON_OPTS="-a :6081 \
# -T localhost:6082 \
# -b localhost:8080 \
# -u varnish -g varnish \
# -s file,/var/lib/varnish/varnish_storage.bin,1G"
## Alternative 2, Configuration with VCL
#
# Listen on port 6081, administration on localhost:6082, and forward to
# one content server selected by the vcl file, based on the request. Use a
# fixed-size cache file.
#
#DAEMON_OPTS="-a :6081 \
# -T localhost:6082 \
# -f /etc/varnish/default.vcl \
# -u varnish -g varnish \
# -S /etc/varnish/secret \
# -s file,/var/lib/varnish/varnish_storage.bin,1G"
## Alternative 3, Advanced configuration
#
# See varnishd(1) for more information.
#
# # Main configuration file. You probably want to change it

#VARNISH_VCL_CONF=/etc/varnish/default.vcl
#
# # 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=6081
#
# # Telnet admin interface listen address and port
#VARNISH_ADMIN_LISTEN_ADDRESS=127.0.0.1
#VARNISH_ADMIN_LISTEN_PORT=6082
#
# # Shared secret file for admin interface
#VARNISH_SECRET_FILE=/etc/varnish/secret
#
# # The minimum number of worker threads to start
#VARNISH_MIN_THREADS=50
#
# # The Maximum number of worker threads to start
#VARNISH_MAX_THREADS=1000
#
# # Idle timeout for worker threads
#VARNISH_THREAD_TIMEOUT=120
#
# # Cache file location
#VARNISH_STORAGE_FILE=/var/lib/varnish/varnish_storage.bin
#
# # 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=1G
#
# # Backend storage specification
#VARNISH_STORAGE="file,${VARNISH_STORAGE_FILE},${VARNISH_STORAGE_SIZE}"
#
# # Default TTL used when the backend does not specify one
#VARNISH_TTL=120
#
# # DAEMON_OPTS is used by the init script. If you add or remove options, make
# # sure you update this section, too.
#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}"
#
## Alternative 4, Do It Yourself. See varnishd(1) for more information.
#
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,1G"
7. Koreguojame
Apache tarnybos konfigūraciją, nurodant ties "
Listen: 80" parametru prieš tai nurodytą 8080 prievadą iš 80. Taip pat pakeičiame prievadus ties "
<VirtualServer *:80>" eilutėmis į 8080.
nano /etc/httpd/conf/httpd.conf
8. Perkrauname
Varnish ir
Apache tarnybas:
/etc/init.d/httpd restart
/etc/init.d/varnish restart
9. Patikriname ar kreipiantis tiesiai į mūsų domeną užklausą priima
Varnish įvykdant šią komandą:
curl -I adresas.lt
Numatomas rezultatas jeigu užklausą priima Varnish:
[root@server ~]# curl -I http://adresas.lt
HTTP/1.1 200 OK
Server: Apache/2.2.15 (CentOS)
Last-Modified: Wed, 14 Aug 2013 06:43:41 GMT
ETag: "2420590-4-4e3e2ad933a6a"
Content-Type: text/html; charset=UTF-8
Content-Length: 4
Accept-Ranges: bytes
Date: Wed, 14 Aug 2013 06:56:04 GMT
X-Varnish: 1208454722
Age: 0
Via: 1.1 varnish
Connection: keep-alive
Numatomas rezultatas jeigu užklausą priima tiesiogiai
Apache tarnyba:
[root@server ~]# curl -I http://adresas.lt:8080
HTTP/1.1 200 OK
Date: Wed, 14 Aug 2013 06:56:06 GMT
Server: Apache/2.2.15 (CentOS)
Last-Modified: Wed, 14 Aug 2013 06:43:41 GMT
ETag: "2420590-4-4e3e2ad933a6a"
Accept-Ranges: bytes
Content-Length: 4
Connection: close
Content-Type: text/html; charset=UTF-8
Taip pat tokį rezultatą gauname, jeigu būtų tikrinamas 8080 prievadas veikiant
Varnish tarnybai.
Naudingos Varnish stebėjimui skirtos komandos:
- varnishstat : Nurodo visą kešavimo statistiką.Provides all the info you need to spot cache misses and errors;
- varnishhist : Histogramos pavidalu pateikia kešavimo duomenis;
- varnishlog : Pateikia detalią statistiką apie gautas užklausas ir išsiunčiamus atsakymui klientui;
- varnishtop : Iš Varnish žurnalų išrašų yra pateikiamos dažniausiai pasitaikantys įrašai;
- varnishadm : Varnish administracinė komandinė eilutė.
Daugiau informacijos apie Varnish konfigūravimą rasite
šiuo adresu.