我是靠谱客的博主 淡然御姐,最近开发中收集的这篇文章主要介绍CentOS源码编译安装Haproxy,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

第一步:先看系统内核版本 
这里看到的是: 2.6

[root@localhost ~]# uname -a
Linux localhost.localdomain 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux 

第二步:获取haproxy
下载地址: http://download.chinaunix.net/download/0013000/12508.shtml  可以在这里下载,
我是从51cto里面获取到的 
http://v2.51cto.com/wyfs02/M01/54/11/wKiom1R232uAXpKdAAzMd2QlaHk5198.gz?skey=cyfQ5MDLjT_2Q0rYaNDMJw&stime=1447299800&sname=51CTO%E4%B8%8B%E8%BD%BD-haproxy-1.4.25.tar.gz

<span style="font-size:14px;">[root@localhost ~]# tar zxvf haproxy-1.4.25.tar.gz 
[root@localhost ~]# cd haproxy-1.4.25
[root@localhost haproxy-1.4.25]# </span>

第三步  开始编译安装
(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 <w@1wt.eu>

则安装成功

第四步  配置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 就可以解决了
[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]# cp /root/haproxy-1.4.25/examples/haproxy.cfg 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

这段是参考:http://blog.chinaunix.net/uid-24250828-id-3778032.html

[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]# 

配置上面就完成了

最后

以上就是淡然御姐为你收集整理的CentOS源码编译安装Haproxy的全部内容,希望文章能够帮你解决CentOS源码编译安装Haproxy所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(39)

评论列表共有 0 条评论

立即
投稿
返回
顶部