#cnetos系统编译安装haproxy
#需要依赖lua库,官网:http://www.lua.org
#harpoxy 官网下载:http://www.haproxy.org/download/
DIR=pwd
file_lua=“lua-5.4.3”
file_haproxy=“haproxy-2.2.11”
suffix=".tar.gz"
lua_install_dir=/usr/local/src
haproxy_install_dir=/apps/haproxy
tar_dir=/usr/local/src
local_ip=ifconfig | grep 'netmask' | head -n1 |awk '{print $2}'
#主机的ip地址
port=9999 #定义状态页监听端口
check(){
echo “正在安装环境包,请勿退出!”
yum install -y make vim wget libtermcap-devel ncurses-devel libevent-devel readline-devel gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel net-tools iotop zip unzip zlib-devel lrzsz tree lsof tcpdump systemd-devel
[ -f DIR/{DIR}/DIR/{file_lua}KaTeX parse error: Expected '}', got 'EOF' at end of input: …} ] || { echo "{lua_file}${suffix} is not find" ; exit 1; }
[ -f DIR/{DIR}/DIR/{file_haproxy}KaTeX parse error: Expected '}', got 'EOF' at end of input: …} ] || { echo "{lua_haproxy}${suffix} is not find" ; exit 1; }
if ! id haproxy &>/dev/null ;then
useradd -s /sbin/nologin -r haproxy
else
echo “haproxy用户已存在,略过用户创建!”
fi
[ -d ${lua_install_dir} ] || mkdir -p ${lua_install_dir}
}
install_lua(){
tar xf DIR/{DIR}/DIR/{file_lua}${suffix} -C ${lua_install_dir}
cd luainstalldir/{lua_install_dir}/luainstalldir/{file_lua}
make all test
if [ $? -eq 0 ];then
luainstalldir/{lua_install_dir}/luainstalldir/{file_lua}/src/lua -v
echo “lua install success!”
fi
}
install_haproxy(){
tar xf DIR/{DIR}/DIR/{file_haproxy}${suffix} -C ${tar_dir}
cd tardir/{tar_dir}/tardir/{file_haproxy}
make PREFIX=/apps/haproxy ARCH=x86_64 TARGET=linux-glibc USE_OPENSSL=1 USE_ZLIB=1 USE_LUA=1 USE_PCRE=1 USE_SYSTEMD=1 USE_CPU_AFFINITY=1 LUA_INC=luainstalldir/{lua_install_dir}/luainstalldir/{file_lua}/src LUA_LIB=luainstalldir/{lua_install_dir}/luainstalldir/{file_lua}/src
[ KaTeX parse error: Expected 'EOF', got '&' at position 11: ? -eq 0 ] &̲& make install …{haproxy_install_dir} || echo “haproxy make is error!”
#cp tardir/{tar_dir}/tardir/{file_haproxy}/haproxy /usr/sbin/
#配置haproxy启动服务脚本
cat > /usr/lib/systemd/system/haproxy.service << END
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target
[Service]
ExecStartPre=haproxyinstalldir/sbin/haproxy−f/etc/haproxy/haproxy.cfg−c−qExecStart={haproxy_install_dir}/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q
ExecStart=haproxyinstalldir/sbin/haproxy−f/etc/haproxy/haproxy.cfg−c−qExecStart={haproxy_install_dir}/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/lib/haproxy/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID
[Install]
WantedBy=multi-user.target
END
mkdir /etc/haproxy
#配置haproxy主配置文件
cat > /etc/haproxy/haproxy.cfg << EOF
global
maxconn 100000
chroot ${haproxy_install_dir}
stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin
user haproxy
group haproxy
#nbproc 4
#cpu-map 1 0
#cpu-map 2 1
#cpu-map 3 2
#cpu-map 4 3
pidfile /var/lib/haproxy/haproxy.pid
log 127.0.0.1 local3 info
defaults
option http-keep-alive
option forwardfor
maxconn 100000
mode http
timeout connect 300000ms
timeout client 300000ms
timeout server 300000ms
listen stats
mode http
bind 0.0.0.0:9999
stats enable
log global
stats uri /haproxy-status
stats auth admin:123456
EOF
mkdir /var/lib/haproxy
chown -R haproxy.haproxy /var/lib/haproxy
systemctl daemon-reload haproxy
systemctl enable --now haproxy
if [ ?−eq0];thenecho"haproxyinstallsuccess!"echo"可以访问状态页面http://? -eq 0 ];then
echo "haproxy install success!"
echo "可以访问状态页面http://?−eq0];thenecho"haproxyinstallsuccess!"echo"可以访问状态页面http://{local_ip}😒{port}/haproxy-status"
echo “账户/密码:admin/123456”
${haproxy_install_dir}/sbin/haproxy -v
fi
}
check
install_lua
install_haproxy
脚本执行完毕后 服务启动不起来,而且就连重启的reboot命令都不能执行! 希望老铁们指教!