centos6.4安裝varnish4.0

1. 安裝好CentOS6.4系統後,安裝更新
yum update

2. 下載varnish4.0安裝包並解壓
https://github.com/varnish/Varnish-Cache 放到/usr/local目錄下
[root@localhost ~]# cd /usr/local/
[root@localhost local]# ls
bin  etc  games  include  lib  lib64  libexec  sbin  share  src  Varnish-Cache-master.zip  vmtools
[root@localhost local]# chmod 775 Varnish-Cache-master
[root@localhost local]# unzip Varnish-Cache-master
[root@localhost local]# ls
bin  etc  games  include  lib  lib64  libexec  sbin  share  src  Varnish-Cache-master  Varnish-Cache-master.zip  vmtools

3. 開始安裝
[root@localhost local]# cd Varnish-Cache-master
[root@localhost Varnish-Cache-master]# ls
autogen.des  ChangeLog     CONTRIBUTING  flint.lnt  lib      Makefile.am       man     varnishapi.pc.in
autogen.sh   config.phk    doc           include    LICENSE  Makefile.inc.phk  README  varnishapi-uninstalled.pc.in
bin          configure.ac  etc           INSTALL    m4       Makefile.phk      redhat  varnish.m4
[root@localhost Varnish-Cache-master]# chmod -R 755 *
[root@localhost Varnish-Cache-master]# ls
autogen.des  ChangeLog     CONTRIBUTING  flint.lnt  lib      Makefile.am       man     varnishapi.pc.in
autogen.sh   config.phk    doc           include    LICENSE  Makefile.inc.phk  README  varnishapi-uninstalled.pc.in
bin          configure.ac  etc           INSTALL    m4       Makefile.phk      redhat  varnish.m4
[root@localhost Varnish-Cache-master]#

[root@localhost Varnish-Cache-master]# ./autogen.sh
./autogen.sh: line 29: automake: command not found
WARNING: unable to determine automake version
+ libtoolize --copy --force
./autogen.sh: line 44: libtoolize: command not found
[root@localhost Varnish-Cache-master]#
報錯,那就是依賴包未安裝徹底,所需依賴包以下:

 autoconf
 automake
 jemalloc-devel
 libedit-devel
 libtool
 ncurses-devel
 pcre-devel
 pkgconfig
 python-docutils
 python-sphinx
參考地址:https://www.varnish-cache.org/docs/4.0/installation/install.html

而後所有用yum install便可
[root@localhost Varnish-Cache-master]# yum install autoconf automake jemalloc-devel libedit-devel libtool ncurses-devel pcre-devel pkgconfig python-docutils python-sphinx

中間可能會提示無可用包,可是不要緊,能夠嘗試繼續安裝varnish
No package jemalloc-devel available.

[root@localhost Varnish-Cache-master]# ./autogen.sh
+ libtoolize --copy --force
libtoolize: putting auxiliary files in AC_CONFIG_AUX_DIR, `build-aux'.
libtoolize: copying file `build-aux/ltmain.sh'
libtoolize: putting macros in AC_CONFIG_MACRO_DIR, `m4'.
libtoolize: copying file `m4/libtool.m4'
libtoolize: copying file `m4/ltoptions.m4'
libtoolize: copying file `m4/ltsugar.m4'
libtoolize: copying file `m4/ltversion.m4'
libtoolize: copying file `m4/lt~obsolete.m4'
+ aclocal -I m4
configure.ac:25: warning: AC_COMPILE_IFELSE was called before AC_USE_SYSTEM_EXTENSIONS
../../lib/autoconf/specific.m4:386: AC_USE_SYSTEM_EXTENSIONS is expanded from...
../../lib/autoconf/specific.m4:332: AC_GNU_SOURCE is expanded from...
configure.ac:25: the top level
configure.ac:25: warning: AC_RUN_IFELSE was called before AC_USE_SYSTEM_EXTENSIONS
+ autoheader
configure.ac:25: warning: AC_COMPILE_IFELSE was called before AC_USE_SYSTEM_EXTENSIONS
../../lib/autoconf/specific.m4:386: AC_USE_SYSTEM_EXTENSIONS is expanded from...
../../lib/autoconf/specific.m4:332: AC_GNU_SOURCE is expanded from...
configure.ac:25: the top level
configure.ac:25: warning: AC_RUN_IFELSE was called before AC_USE_SYSTEM_EXTENSIONS
+ automake --add-missing --copy --foreign
configure.ac:25: warning: AC_COMPILE_IFELSE was called before AC_USE_SYSTEM_EXTENSIONS
../../lib/autoconf/specific.m4:386: AC_USE_SYSTEM_EXTENSIONS is expanded from...
../../lib/autoconf/specific.m4:332: AC_GNU_SOURCE is expanded from...
configure.ac:25: the top level
configure.ac:25: warning: AC_RUN_IFELSE was called before AC_USE_SYSTEM_EXTENSIONS
configure.ac:15: installing `build-aux/config.guess'
configure.ac:15: installing `build-aux/config.sub'
configure.ac:19: installing `build-aux/install-sh'
configure.ac:19: installing `build-aux/missing'
bin/varnishadm/Makefile.am: installing `build-aux/depcomp'
+ autoconf
configure.ac:25: warning: AC_COMPILE_IFELSE was called before AC_USE_SYSTEM_EXTENSIONS
../../lib/autoconf/specific.m4:386: AC_USE_SYSTEM_EXTENSIONS is expanded from...
../../lib/autoconf/specific.m4:332: AC_GNU_SOURCE is expanded from...
configure.ac:25: the top level
configure.ac:25: warning: AC_RUN_IFELSE was called before AC_USE_SYSTEM_EXTENSIONS
[root@localhost Varnish-Cache-master]#

沒報錯 
[root@localhost Varnish-Cache-master]# ls
aclocal.m4      bin          config.log    CONTRIBUTING  include  m4                Makefile.phk  varnishapi.pc.in
autogen.des     build-aux    config.phk    doc           INSTALL  Makefile.am       man           varnishapi-uninstalled.pc.in
autogen.sh      ChangeLog    configure     etc           lib      Makefile.in       README        varnish.m4
autom4te.cache  config.h.in  configure.ac  flint.lnt     LICENSE  Makefile.inc.phk  redhat
[root@localhost Varnish-Cache-master]#

再configure,沒報錯,期間若有報錯,能夠根據報錯內容網上搜一下解決辦法
[root@localhost Varnish-Cache-master]# ./configure --prefix=/usr/local/varnish PKG_CONFIG_PATH=/usr/lib/pkgconfig

接着就make
[root@localhost Varnish-Cache-master]#make
[root@localhost Varnish-Cache-master]#make install

此時/usr/local目錄底下就有了varnish目錄
[root@localhost local]# cd varnish/
[root@localhost varnish]# pwd
/usr/local/varnish
[root@localhost varnish]# ls
bin  include  lib  sbin  share  var
[root@localhost varnish]#

到此varnish就安裝完成了,可是。。。還要配置
4. 配置varnish
因爲varnish3.0與varnish4.0的配置文件有較大改動,且一些變量名,函數等都有改動,因此不採用3.0的配置文件
其實舊的配置文件模版在這裏:
/usr/local/Varnish-Cache-master/etc/example.vcl

網上找到4.0的配置模版:
http://loftor.com/archives/varnish-4_0-vcl.html
但這個模版還須要修改,有3個地方要修改
在/usr/local/varnish目錄下新建etc目錄用來存放配置文件
在/usr/local/varnish/etc目錄下建立varnish配置文件web.conf 其實這裏配置文件的命名是能夠隨意的。只不事後面將配置文件與varnish服務關聯起來的時候,文件名必定要對應
將模版的內容所有copy到web.conf文件內,而後做修改
修改1:
backend default {
    .host = "127.0.0.1";
    .port = "81";
    .probe = {
    .url = "/ping";
    .timeout = 1s;
    .interval = 10s;
    .window = 5;
    .threshold = 2;
    }
    .first_byte_timeout = 300s;   # How long to wait before we receive a first byte from our backend?
    .connect_timeout  = 5s;     # How long to wait for a backend connection?
    .between_bytes_timeout = 2s;     # How long to wait between bytes received from our backend?
}

backend web1 {
    .host = "127.0.0.1";
    .port = "81";
}

backend web2 {
    .host = "127.0.0.1";
    .port = "81";
}
前面的backend default{}、backend web1{}、backend web1{}是須要緩存的源站,命名可隨意,也可增刪,裏面的參數也可根據實際需求修改,這裏我修改以下:
backend ddostest1 {
    .host = "192.168.6.102"; #源站IP
    .port = "80";
    .first_byte_timeout     = 300s;   # How long to wait before we receive a first byte from our backend?
    .connect_timeout        = 5s;     # How long to wait for a backend connection?
    .between_bytes_timeout  = 2s;     # How long to wait between bytes received from our backend?
}
修改2:
import directors;
sub vcl_init {
    new cluster1 = directors.round_robin();
    cluster1.add_backend(web1);    # Backend web1 defined above
    cluster1.add_backend(web2);    # Backend web2 defined above
}
修改後:
import directors;
sub vcl_init {
    new test = directors.round_robin();
    test.add_backend(ddostest1);    # 這裏是加速節點,與上面的ddostest1對應
}
固然這裏也是能夠new多個的,只要與源站對應便可
修改3:
在sub vcl_recv {}裏
將set req.backend_hint = cluster1.backend();註釋掉
將set req.http.Host = regsub(req.http.Host, ":[0-9]+", "");
修改成:
if (req.http.host ~ "(?i)^(www.)?ddostest.com$") {
set req.backend_hint = test.backend();
    }
這裏用到正則表達式,大體意思是從客戶端請求的域名,只要是以ddostest.com結尾的域名,都使用test這個節點響應,而後保存。

5. 將varnish服務與配置文件web.conf關聯起來,並開機自啓動:
在/etc/init.d/目錄下新建varnish文件,並給予755權限,內容以下:
# chkconfig: 2345 10 90 
# description: varnish ....
#!/bin/sh

start()
{
        echo -n $"starting varnish..."
        /usr/local/varnish/sbin/varnishd -P /tmp/varnish.pid -a 0.0.0.0:80 -T 127.0.0.1:3500 -f /usr/local/varnish/etc/web.conf -n /var/varnish_cache -s malloc,1G -P client_http11=on
        echo
}

stop()
{
        echo -n $"stopping varnish..."
        pkill varnish
        echo
}

restart()
{
   stop
   sleep 2
   start
}

case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo $"Usage: $0 {start|stop|restart}"
esac

以後還要建立緩存目錄/var/varnish_cache
關閉防火牆,開機自啓動varnish服務
[root@localhost var]# service iptables stop
iptables: Flushing firewall rules: [  OK  ]
iptables: Setting chains to policy ACCEPT: filter [  OK  ]
iptables: Unloading modules:                            [  OK  ]
[root@localhost var]# chkconfig
iptables        0:off   1:off   2:on    3:on    4:on    5:on    6:off
要將全部終端防火牆關閉
[root@localhost var]# chkconfig iptables --level 2345 off

將varnish加入chkconfig
[root@localhost var]# chkconfig --add varnish
[root@localhost var]# chkconfig
varnish         0:off   1:off   2:on    3:on    4:on    5:on    6:off
[root@localhost var]# chkconfig varnish --level 016 on
啓動varnish服務
[root@localhost var]# service varnish start
starting varnish...

6. 測試
源站:192.168.6.102
加速節點:192.168.6.106
客戶端:192.168.6.99
在客戶端hosts文件裏將www.ddostest.com域名與加速節點綁定
127.0.0.1       localhost
192.168.6.106   www.ddostest.com
直接訪問源站:
 
ping站點域名www.ddostest.com
 
訪問站點域名www.ddostest.com:
  完畢!若有說錯的內容,還請指教!
相關文章
相關標籤/搜索