CentOS安裝Haproxy源碼編譯安裝

下載地址:http://download.chinaunix.net/download/0013000/12508.shtml  可以在這裏下載,
[root@localhost ~]# yum -y install cmake  gcc gcc-c++  autoconf automake zlib*  libxml* \
ncurses ncurses-devel libtool-ltdl-devel* make  bison bison-devel libaio 
[root@localhost ~]# tar zxvf haproxy-1.4.25.tar.gz 
[root@localhost ~]# cd haproxy-1.4.25
[root@localhost haproxy-1.4.25]# 
開始編譯安裝
(MAKE 參數請參考文件中的README)
[root@localhost haproxy-1.4.25]# make TARGET=linux26 PREFIX=/usr/local/haproxy
[root@localhost haproxy-1.4.25]# make install PREFIX=/usr/local/haproxy
[root@localhost haproxy-1.4.25]# install -d /usr/local/sbin
[root@localhost haproxy-1.4.25]# install haproxy /usr/local/sbin
[root@localhost haproxy-1.4.25]# install -d /usr/local/share/man/man1
[root@localhost haproxy-1.4.25]# install -m 644 doc/haproxy.1 /usr/local/share/man/man1
[root@localhost haproxy-1.4.25]# install -d /usr/local/doc/haproxy
[root@localhost haproxy-1.4.25]# for x in configuration architecture haproxy-en haproxy-fr; do \
> install -m 644 doc/$x.txt /usr/local/doc/haproxy ; \
> done
[root@localhost haproxy-1.4.25]#

安裝完成,
我們檢測 一下是否安裝成功
[root@localhost haproxy-1.4.25]# haproxy -v
HA-Proxy version 1.4.25 2014/03/27
Copyright 2000-2014 Willy Tarreau <[email protected]>
則安裝成功

配置haproxy.cfg文件
這裏解釋一下,我用的時候 安裝文件夾用 /root/haproxy-1.4.25/examples/haproxy.cfg   這個是自帶的配置文件
[root@localhost examples]# vim haproxy.cfg

# this config needs haproxy-1.1.28 or haproxy-1.2.1

global
        log 127.0.0.1   local0
        log 127.0.0.1   local1 notice
        #log loghost    local0 info
        maxconn 4096
#       chroot /usr/share/haproxy
        chroot  /usr/local/haproxy
        uid 99
        gid 99
        daemon
        #debug
        #quiet

defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        retries 3
        #redispatch
        maxconn 2000
        contimeout      5000
        clitimeout      50000
        srvtimeout      50000
#============================這一段是我後面加上去了。 就是WEB代理
listen web_proxy
        bind *:80
        mode    http
        option httpchk GET /index.html
        server s1 192.168.11.210:80 weight 3 check
        server s2 192.168.11.211:80 weight 3 check
#後面的我做的註釋
#listen appli1-rewrite 0.0.0.0:10001
#       cookie  SERVERID rewrite
#       balance roundrobin
#       server  app1_1 192.168.34.23:8080 cookie app1inst1 check inter 2000 rise 2 fall 5
#       server  app1_2 192.168.34.32:8080 cookie app1inst2 check inter 2000 rise 2 fall 5
#       server  app1_3 192.168.34.27:8080 cookie app1inst3 check inter 2000 rise 2 fall 5
#       server  app1_4 192.168.34.42:8080 cookie app1inst4 check inter 2000 rise 2 fall 5
#
#listen appli2-insert 0.0.0.0:10002
#       option  httpchk
#       balance roundrobin
#       cookie  SERVERID insert indirect nocache
#       server  inst1 192.168.114.56:80 cookie server01 check inter 2000 fall 3
#       server  inst2 192.168.114.56:81 cookie server02 check inter 2000 fall 3
#       capture cookie vgnvisitor= len 32
#
#       option  httpclose               # disable keep-alive
#       rspidel ^Set-cookie:\ IP=       # do not let this cookie tell our internal IP address
#       
#listen appli3-relais 0.0.0.0:10003
#       dispatch 192.168.135.17:80
#
#listen appli4-backup 0.0.0.0:10004
#       option  httpchk /index.html
#       option  persist
#       balance roundrobin
#       server  inst1 192.168.114.56:80 check inter 2000 fall 3
#       server  inst2 192.168.114.56:81 check inter 2000 fall 3 backup
#
#listen ssl-relay 0.0.0.0:8443
#       option  ssl-hello-chk
#       balance source
#       server  inst1 192.168.110.56:443 check inter 2000 fall 3
#       server  inst2 192.168.110.57:443 check inter 2000 fall 3
#       server  back1 192.168.120.58:443 backup
#listen appli5-backup 0.0.0.0:10005
#       option  httpchk *
#       balance roundrobin
#       cookie  SERVERID insert indirect nocache
#       server  inst1 192.168.114.56:80 cookie server01 check inter 2000 fall 3
#       server  inst2 192.168.114.56:81 cookie server02 check inter 2000 fall 3
#       server  inst3 192.168.114.57:80 backup check inter 2000 fall 3
#       capture cookie ASPSESSION len 32
#       srvtimeout      20000
#       option  httpclose               # disable keep-alive
#       option  checkcache              # block response if set-cookie & cacheable
#       rspidel ^Set-cookie:\ IP=       # do not let this cookie tell our internal IP address
#       errorloc        502     http://192.168.114.58/error502.html
#       errorfile       503     /etc/haproxy/errors/503.http

啓動服務
[root@localhost examples]# haproxy -f /root/haproxy-1.4.25/examples/haproxy.cfg 
[WARNING] 316/220055 (2376) : parsing [/root/haproxy-1.4.25/examples/haproxy.cfg:22]: keyword 'redispatch' is deprecated, please use 'option redispatch' instead.
[ALERT] 316/220055 (2376) : [haproxy.main()] Cannot chroot(/usr/local/sbin/haproxy).
出現的報錯信息,下面我們來解決問題。因爲上面配置文件是我改後的,應該沒有什麼問題。 
[ALERT] 316/220055 (2376) : [haproxy.main()] Cannot chroot(/usr/local/sbin/haproxy).
是配置文件chroot的目錄不對,根據我的文檔安裝改成 chroot /urs/local/haproxy 就可以解決了  執行 mkdir /usr/share/haproxy
[WARNING] 316/220055 (2376) : parsing [/root/haproxy-1.4.25/examples/haproxy.cfg:22]: keyword 'redispatch' is deprecated, please use 'option redispatch' instead.
這個報錯信息是配置文件22行的問題, 我是做了註釋掉就可以起動服務了。   註釋到相對應的錯誤行
再次起動服務:
[root@localhost examples]# haproxy -f /root/haproxy-1.4.25/examples/haproxy.cfg 
[root@localhost examples]#
沒有任何提示,那我們檢測一下是否啓動成功
看一下進程信息
[root@localhost examples]# ps -ef |grep haproxy
nobody    3826     1  0 22:38 ?        00:00:00 haproxy -f /root/haproxy-1.4.25/examples/haproxy.cfg
root      3837  1260  0 22:38 pts/0    00:00:00 grep haproxy
進程已經在

看一下端口信息
下圖就成功了80端口是我們自定義的服務。最後加的配置文件
[root@localhost examples]# netstat -anp |grep haproxy
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      3826/haproxy        
udp        0      0 0.0.0.0:43549               0.0.0.0:*                               3826/haproxy  
把配置文件移動到/etc目錄下
[root@localhost examples]# cd /etc/
[root@localhost etc]# 
複製haproxy配置文件到/etc/目錄下面
[root@localhost etc]# cp /root/haproxy-1.4.25/examples/haproxy.cfg haproxy.cfg
我們再一次查看進程 kill掉。 在用/etc/haproxy.cfg的配置文件啓動看看
[root@localhost etc]# ps -ef | grep haproxy
nobody    3826     1  0 22:38 ?        00:00:00 haproxy -f /root/haproxy-1.4.25/examples/haproxy.cfg
root      3901  1260  0 22:40 pts/0    00:00:00 grep haproxy
[root@localhost etc]# kill 3826
[root@localhost etc]# ps -ef | grep haproxy
root      3928  1260  0 22:41 pts/0    00:00:00 grep haproxy

已經kill掉進程,進程已經沒有在運行了, 我們現在用/etc/haproxy.cfg 來運行服務
[root@localhost etc]# haproxy -f /etc/haproxy.cfg
[root@localhost etc]# ps -ef |grep haproxy.cfg
nobody    3959     1  0 22:42 ?        00:00:00 haproxy -f /etc/haproxy.cfg
root      3965  1260  0 22:43 pts/0    00:00:00 grep haproxy.cfg

我們已經看到運行成功

設置以服務形式啓動
目錄切換到/etc/init.d

[root@localhost etc]# cd /etc/init.d/
[root@localhost init.d]# vi haproxy

#!/bin/bash
    #
    # haproxy
    #
    # chkconfig: 35 85 15
    # description: HAProxy is a free, very fast and reliable solution \
    # offering high availability, load balancing, and \
    # proxying for TCP and HTTP-based applications
    # processname: haproxy
    # config: /etc/haproxy.cfg
    # pidfile: /var/run/haproxy.pid
    # Source function library.
    . /etc/rc.d/init.d/functions
    # Source networking configuration.
    . /etc/sysconfig/network
    # Check that networking is up.
    [ "$NETWORKING" = "no" ] && exit 0
    config="/etc/haproxy.cfg"
    exec="/usr/local/haproxy/sbin/haproxy"
    prog=$(basename $exec)
    [ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog
    lockfile=/var/lock/subsys/haproxy
    check() {
        $exec -c -V -f $config
    }
    start() {
        $exec -c -q -f $config
        if [ $? -ne 0 ]; then
            echo "Errors in configuration file, check with $prog check."
            return 1
        fi
        echo -n $"Starting $prog: "
        # start it up here, usually something like "daemon $exec"
        daemon $exec -D -f $config -p /var/run/$prog.pid
        retval=$?
        echo
        [ $retval -eq 0 ] && touch $lockfile
        return $retval
    }
    stop() {
        echo -n $"Stopping $prog: "
        # stop it here, often "killproc $prog"
        killproc $prog
        retval=$?
        echo
        [ $retval -eq 0 ] && rm -f $lockfile
        return $retval
    }
    restart() {
        $exec -c -q -f $config
        if [ $? -ne 0 ]; then
            echo "Errors in configuration file, check with $prog check."
            return 1
        fi
        stop
        start
    }
    reload() {
        $exec -c -q -f $config
        if [ $? -ne 0 ]; then
            echo "Errors in configuration file, check with $prog check."
            return 1
        fi
        echo -n $"Reloading $prog: "
        $exec -D -f $config -p /var/run/$prog.pid -sf $(cat /var/run/$prog.pid)
        retval=$?
        echo
        return $retval
    }
    force_reload() {
        restart
    }
    fdr_status() {
        status $prog
    }
    case "$1" in
        start|stop|restart|reload)
            $1
            ;;
        force-reload)
            force_reload
            ;;
        checkconfig)
            check
            ;;
        status)
            fdr_status
            ;;
        condrestart|try-restart)
          [ ! -f $lockfile ] || restart
        ;;
        *)
            echo $"Usage: $0 {start|stop|status|checkconfig|restart|try-restart|reload|force-reload}"
            exit 2
    esac
重起服務haproxy

[root@localhost init.d]# service haproxy restart
env: /etc/init.d/haproxy: Permission denied
這個提示。大概是權限問題。我們下面加上執行權限
[root@localhost init.d]# ll haproxy 
-rw-r--r--. 1 root root 2588 Nov 13 23:02 haproxy
[root@localhost init.d]# chmod -R 655 haproxy 
[root@localhost init.d]# ll haproxy 
-rw-r-xr-x. 1 root root 2588 Nov 13 23:02 haproxy
選 看一下進程是否存在,不存在我們重起haproxy服務
[root@localhost init.d]# ps -ef | grep haproxy
root      7409  1260  0 23:56 pts/0    00:00:00 grep haproxy
[root@localhost init.d]# service haproxy restart
Stopping haproxy:                                          [FAILED]
Starting haproxy:                                          [  OK  ]
[root@localhost init.d]# ps -ef | grep haproxy
nobody    7444     1  0 23:56 ?        00:00:00 /usr/local/haproxy/sbin/haproxy -D -f /etc/haproxy.cfg -p /var/run/haproxy.pid
root      7449  1260  0 23:56 pts/0    00:00:00 grep haproxy
[root@localhost init.d]# 

配置上面就完成了







發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章