TEL:
centos7 varnish5.2.1 完整安装教程
| 发布时间: 2018-11-05 15:09:51 | 1754 次浏览
varnish是一个非常优秀的图片缓存服务器,他是完全基于内存的缓存服务器,从图片缓存的角度来看,性能优于nginx和squid,下面我将为大家讲述如何完整的安装一个varnish缓存服务器。

本教程基于CentOs 7.3

第一步,登录操作系统,切换到root账户,然后创建varnish的专用账号
  groupadd varnish
  useradd varnish -g varnish -d /home/varnish
  passwd varnish
  # 输入密码


第二步,安装varnish,我们这里是基于源码的安装

   源码下载地址:http://varnish-cache.org/releases/

   本教程中,采用的是varnish 5.2版本(最新版本),下载地址:http://varnish-cache.org/_downloads/varnish-5.2.1.tgz


1. 安装varnish所需要的依赖包

yum install autoconf.noarch automake.noarch jemalloc-devel.x86_64 libedit-devel.x86_64 libtool.x86_64 ncurses-devel.x86_64 pcre-devel.x86_64 pkgconfig.x86_64 python-docutils.noarch python-sphinx.noarch graphviz.x86_64 -y


2. 执行安装步骤

tar -zxvf varnish-5.2.1.tar.gz

cd varnish-5.2.1

./autogen.sh
 
./configure
 
make
 
make install
 
ldconfig


3. 安装完成之后,执行如下命令查看varnish的版本号


/usr/local/sbin/varnishd -V


4. 创建配置文件

mkdir -p /etc/varnish
cd /etc/varnish
创建 default.vcl  

创建 secret 文件


5、修改vanish监听端口

vi /etc/sysconfig/varnish 
VARNISH_LISTEN_PORT=80

6. 创建启动脚本

vim varnish_startup.sh
/usr/local/sbin/varnish/varnishd -a 0.0.0.0:7070 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,2G
 
授权:chmod +x varnish_startup.sh


7. 创建reload脚本

vi varnish_reload_vcl.sh


    #!/bin/bash
    # Generate a unique timestamp ID for this version of the VCL
    TIME=$(date +%s)
 
    # Load the file into memory
    /usr/local/src/varnish-5.2.1/bin/varnishadm/varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082 vcl.load varnish_$TIME /etc/varnish/default.vcl
 
    # Active this Varnish config
    /usr/local/src/varnish-5.2.1/bin/varnishadm/varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082 vcl.use varnish_$TIME
 
授予执行权限:chmod +x varnish_reload_vcl

第三步、 给相关目录授权,把安装目录的所有者改为varnish用户
chown -R varnish:varnish /usr/local/src/varnish-5.2.1
chown -R varnish:varnish /usr/local/var/varnish
运行脚本,启动varnish

./varnish_startup.sh