CentOS 编译安装 Varnish 3

安装约定

varnish源码路径:/usr/local/src
varnish安装路径:/usr/local/varnish
varnish配置文件路径:/usr/local/varnish/conf/httpd.conf

软件下载地址

https://www.varnish-cache.org/

下载源代码包

# cd /usr/local/src/
# wget http://repo.varnish-cache.org/source/varnish-3.0.5.tar.gz

安装gcc、make等

# yum -y install gcc gcc-c++ make autoconf automake

安装编译所需的库

# yum -y install automake autoconf libtool ncurses-devel libxslt groff pcre-devel pkgconfig readline-devel

添加varnish用户和组

# groupadd varnish
# useradd -g varnish -c "Varnish Cache" -d /var/lib/varnish -s /sbin/nologin varnish

安装varnish

# cd ..
# tar zxvf varnish-3.0.5.tar.gz
# cd varnish-3.0.5
# ./configure --prefix=/usr/local/varnish
# sh autogen.sh
# make
# make install

varnish启动配置

# cp redhat/varnish.initrc /etc/init.d/varnish
# cp redhat/varnish.sysconfig /etc/sysconfig/varnish
# cp redhat/varnish_reload_vcl /usr/local/varnish/bin

编辑varnish启动参数配置文件

# vim /etc/sysconfig/varnish
VARNISH_VCL_CONF=/usr/local/varnish/etc/varnish/default.vcl
VARNISH_LISTEN_PORT=80
VARNISH_SECRET_FILE=/usr/local/varnish/etc/varnish/secret

修改启动服务

# vim /etc/init.d/varnish
exec="/usr/local/varnish/sbin/varnishd"
reload_exec="/usr/local/varnish/bin/varnish_reload_vcl"

编辑动态加载VCL配置的脚本:

VARNISHADM=”/usr/local/varnish/bin/varnishadm $secret -T $VARNISH_ADMIN_LISTEN_ADDRESS:$VARNISH_ADMIN_LISTEN_PORT”

生成varnish管理秘钥:

# uuidgen > /usr/local/varnish/etc/varnish/secret
# chmod 644 /usr/local/varnish/etc/varnish/secret

添加环境变量

# vim /etc/profile

在其文件末尾添加如下变量

export PATH=$PATH:/usr/local/varnish/bin

或者用以下命令添加

# sed -i '/unset -f pathmunge/a\export PATH=$PATH:/usr/local/varnish/bin' /etc/profile

运行如下命令使环境变量生效

# source /etc/profile

配置开机启动

# chkconfig varnish on

检查vcl配置是否正确

# service varnish configtest

# /usr/local/varnish/sbin/varnishd -C -f /usr/local/varnish/etc/varnish/default.vcl

启动varnish

# service varnish start

动态加载vcl配置

# service varnish reload

开启日志记录

# cp redhat/varnishncsa.initrc /etc/init.d/varnishncsa
# chmod +x /etc/init.d/varnishncsa

修改启动服务

exec="/usr/local/varnish/bin/varnishncsa"

配置开机启动

# chkconfig varnishncsa on

启动varnishncsa

# service varnishncsa start

配置日志轮询

# vim /etc/logrotate.d/varnish

添加如下内容:

/var/log/varnish/*.log {
    daily
    rotate 30
    missingok
    notifempty
    sharedscripts
    delaycompress
    dateext
    dateformat .%Y%d%m
    postrotate
        /bin/kill -HUP `cat /var/run/varnishlog.pid 2>/dev/null` 2> /dev/null || true
        /bin/kill -HUP `cat /var/run/varnishncsa.pid 2>/dev/null` 2> /dev/null || true
    endscript
}

日志轮询debug测试

# logrotate -df /etc/logrotate.d/varnish