一、 varnish 服务器软件
在Centos / RHEL 系安装需要以下 packages (官方说明)
- autoconf
- automake
- jemalloc-devel
- libedit-devel
- libtool
- ncurses-devel
- pcre-devel (名叫pcre的版本貌似不能用)
- pkgconfig
- python-docutils
- python-sphinx
前面试过装 4.0.1 报错中途没有安装结束。 因为我这里确实要用 3.0.+ 的,所以直接安装了 varnish 3.0.6
虽然写了一大篇, 其实本文的配置设置挺简单。
# wget -c https://repo.varnish-cache.org/source/varnish-3.0.6.tar.gz
# tar -zxvf varnish-3.0.6.tar.gz
# cd varnish-3.0.6
# ./configure --prefix=/usr/local/varnish
# make && make install
# cd /usr/local/varnish
# sbin/varnishd -V
// 看到 copyright 就是安装成功了
配置并设置启动:
// 在varnish解压包下,有两个文件 varnish.initrc, varnish.sysconfig
# cp /usr/local/src/varnish-3.0.6/redhat/varnish.initrc /etc/init.d/varnish
# cp /usr/local/src/varnish-3.0.6/redhat/varnish.sysconfig /etc/sysconfig/varnish
# cp /usr/local/src/varnish-3.0.6/redhat/varnish_reload_vcl /usr/local/varnish/bin
# chkconfig --level 2345 varnish on
# chkconfig --list
更改刚刚复制的 /etc/init.d/varnish 配置文件,找到此部分并修改
exec="/usr/local/varnish/sbin/varnishd" // 正确的安装目录下 varnishd 路径
reload_exec="/usr/local/varnish/bin/varnish_reload_vcl" // reload使用
prog="varnish" // 进程名
config="/etc/sysconfig/varnish" // 上面复制的 varnish.sysconfig 文件
lockfile="/var/lock/subsys/varnish"
Alternative,也可以通过软链接将安装目录**/varnish/etc/varnish 目录 链接 到 /etc/varnish.
更改刚刚复制的 /etc/sysconfig/varnish 系统配置文件。
backend default {
# -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=/usr/local/varnish/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.
# 监听的前端请求限制IP,一般为空
# VARNISH_LISTEN_ADDRESS=
# 监听的前端请求端口。 测试正常后改为 80
VARNISH_LISTEN_PORT=8000
#
# # 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 cache保存文件
VARNISH_STORAGE_FILE=/data/varnishcache/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=800M
#
# # 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.
# 这里的参数,需要指定user和group
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_T
IMEOUT} \
-u varnish -g varnish \
-S ${VARNISH_SECRET_FILE} \
-s ${VARNISH_STORAGE}"
#
## Alternative 4, Do It Yourself. See varnishd(1) for more information.
#
# DAEMON_OPTS=""
# /etc/init.d/varnish configtest
Syntax ok
# useradd -s /bin/nologin -M -G varnish varnish // 创建用户和组
# service start varnish
除了系统脚本启动外,还可以指定临时参数启动
/usr/local/varnish/sbin/varnishd -f /usr/local/varnish/etc/varnish/default.vcl -P /var/run/varnish.pid -T 127.0.0.1:8021 -a 0.0.0.0:8000 -p http_resp_hdr_len=8192
二、Magento 插件
下载适用于magento CE 的免费插件,官网connect2.0链接:
http://connect20.magentocommerce.com/community/Varnish_Cache
安装完毕后,varnishcache/etc/ 下有 default_xx.vcl , vars.vcl, 复制到varnish配置指定的目录下,确保文件名路径和配置中一致。
magento后台启用即可。
附一大神文档链接:http://www.tuicool.com/articles/mqaiyeA
更多文档见官网 https://www.varnish-cache.org/docs/trunk/tutorial/putting_varnish_on_port_80.html