千家信息网

如何在Ubuntu系统中编译安装HAProxy

发表于:2025-01-23 作者:千家信息网编辑
千家信息网最后更新 2025年01月23日,这篇文章给大家分享的是在Ubuntu系统中编译安装HAProxy的详细教程,相信大部分人都还不知道怎么安装,为了让大家学会,给大家总结了以下内容,话不多说,一起往下看吧。环境OS:Ubuntu1804
千家信息网最后更新 2025年01月23日如何在Ubuntu系统中编译安装HAProxy

这篇文章给大家分享的是在Ubuntu系统中编译安装HAProxy的详细教程,相信大部分人都还不知道怎么安装,为了让大家学会,给大家总结了以下内容,话不多说,一起往下看吧。

环境

OS:Ubuntu1804haproxy: http://www.haproxy.org/download/2.0/src/haproxy-2.0.4.tar.gzlua:http://www.lua.org/ftp/lua-5.3.5.tar.gzIP:192.168.7.182

安装步骤

1.准备编译安装HAProxy的基础环境# apt install make gcc build-essential libssl-dev zlib1g-dev libpcre3 libpcre3-dev libsystemd-dev libreadline-dev -y2.编译安装lua,为HAProxy支持基于其实现功能扩展。注:HAProxy要求的lua最低版本为5.32.1 下载并且安装lua# wget -P /usr/local/src/ http://www.lua.org/ftp/lua-5.3.5.tar.gz# cd /usr/local/src/# tar xf lua-5.3.5.tar.gz# cd lua-5.3.5/src/# make linux2.2 查看编译后的版本# ./lua -vLua 5.3.5  Copyright (C) 1994-2018 Lua.org, PUC-Rio3.编译安装haproxy3.1 解压并安装# cd /usr/local/src/# tar xf haproxy-2.0.4.tar.gz# cd haproxy-2.0.4# make -j `lscpu |awk 'NR==4{print $2}'` ARCH=x86_64 TARGET=linux-glibc USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_CPU_AFFINITY=1 USE_LUA=1 LUA_INC=/usr/local/src/lua-5.3.5/src/ LUA_LIB=/usr/local/src/lua-5.3.5/src/ PREFIX=/apps/haproxy && make install PREFIX=/apps/haproxy3.2 查看编译安装好的版本信息# ./haproxy -vHA-Proxy version 2.0.4 2019/08/06 - https://haproxy.org/4.编写haproxy启动脚本# cat > /lib/systemd/system/haproxy.service << EOF                [Unit]Description=HAProxy Load BalancerAfter=syslog.target network.target[Service]ExecStartPre=/apps/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -cExecStart=/apps/haproxy/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/lib/haproxy/haproxy.pidExecReload=/bin/kill -USR2 \$MAINPID[Install]WantedBy=multi-user.targetEOF5.编写配置文件/etc/haproxy/haproxy.cfg# mkdir /etc/haproxy# cat > /etc/haproxy/haproxy.cfg << EOFglobal    maxconn 100000    chroot /usr/local/haproxy    stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin    uid 99    gid 99    daemon    pidfile /var/lib/haproxy/haproxy.pid    log 127.0.0.1 local3 infodefaults    option http-keep-alive    option forwardfor    maxconn 100000    mode http    timeout connect 300s    timeout client  300s    timeout server  300slisten stats    bind  :9009    stats enable    stats uri /status    stats auth admin:123456    stats realm HAPorxy\ Stats\ PageEOF6.创建相关目录# mkdir /var/lib/haproxy# mkdir /usr/local/haproxy# chown 99.99 /var/lib/haproxy/ -R7.启动并设为开机自启动和查看haproxy状态# systemctl enable --now haproxy# systemctl status haproxy● haproxy.service - HAProxy Load Balancer   Loaded: loaded (/lib/systemd/system/haproxy.service; enabled; vendor preset: enabled)   Active: active (running) since Sun 2020-01-12 15:18:00 CST; 11min ago  Process: 9898 ExecStartPre=/apps/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c (code=exited, status=0/SUCCESS) Main PID: 9901 (haproxy)    Tasks: 3 (limit: 2290)   CGroup: /system.slice/haproxy.service           ├─9901 /apps/haproxy/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/lib/haproxy/haproxy.pid           └─9911 /apps/haproxy/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/lib/haproxy/haproxy.pidJan 12 15:18:00 ubuntu1804-02 systemd[1]: Starting HAProxy Load Balancer...Jan 12 15:18:00 ubuntu1804-02 haproxy[9898]: Configuration file is validJan 12 15:18:00 ubuntu1804-02 systemd[1]: Started HAProxy Load Balancer.Jan 12 15:18:00 ubuntu1804-02 haproxy[9901]: [NOTICE] 011/151800 (9901) : New worker #1 (9911) forked8.访问测试状态页


遇到的报错及解决办法

编译lua相关

报错 lua.c:82:10: fatal error: readline/readline.h: No such file or directory

# apt install libreadline-dev -y

编译haproxy相关

报错 include/common/regex.h:31:10: fatal error: pcre.h: No such file or directory

# apt install libpcre3-dev -y

报错 include/common/openssl-compat.h:5:10: fatal error: openssl/bn.h: No such file or directory

# apt install libssl-dev -y

报错 include/types/compression.h:32:10: fatal error: zlib.h: No such file or directory

# apt install zlib1g-dev -y

报错 src/haproxy.c:72:10: fatal error: systemd/sd-daemon.h: No such file or directory

# apt install libsystemd-dev -y

以上就是在Ubuntu系统中编译安装HAProxy的详细教程了,看完之后是否有所收获呢?如果想了解更多相关内容,欢迎关注行业资讯!

0