1. 程式人生 > 其它 >編譯安裝haproxy

編譯安裝haproxy

一、安裝lua環境

1.1 安裝依賴包

[root@centos7 ~]# yum install gcc readline-devel

1.2 下線lua原始碼包並解壓

[root@centos7 ~]#wget http://www.lua.org/ftp/lua-5.3.5.tar.gz
[root@centos7 ~]# tar xvf lua-5.3.5.tar.gz  -C /usr/local/src

1.3 進行編譯

[root@centos7 ~]# cd /usr/local/src/lua-5.3.5
[root@centos7 lua-5.3.5]# make linux test
#檢視編譯安裝的版本
[root@centos7 lua-5.3.5]#src/lua -v
Lua 5.3.5 Copyright (C) 1994-2018 Lua.org, PUC-Rio

二、安裝haproxy

2.1安裝依賴包

[root@centos7 ~]#yum -y install gcc openssl-devel pcre-devel systemd-devel

2.2下載原始碼包

[root@centos7 ~]#wget https://www.haproxy.org/download/2.2/src/haproxy-2.2.20.tar.gz
[root@cetnos7 ~]#tar xvf haproxy-2.2.20.tar.gz
[root@cetnos7 ~]#cd haproxy-2.2.20/
[root@cetnos7 haproxy-2.2.20]#make ARCH=x86_64 TARGET=linux-glibc USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_LUA=1 LUA_INC=/usr/local/src/lua-5.3.5/src/ LUA_LIB=/usr/local/src/lua-5.3.5/src/
[root@cetnos7 haproxy-2.2.20]#make install PREFIX=/apps/haproxy
[root@cetnos7 haproxy-2.2.20]#ln -s /apps/haproxy/sbin/haproxy /usr/sbin/
[root@cetnos7 haproxy-2.2.20]#haproxy -v
HA-Proxy version 2.2.20-6e457a2 2022/01/12 - https://haproxy.org/
Status: long-term supported branch - will stop receiving fixes around Q2 2025.
Known bugs: http://www.haproxy.org/bugs/bugs-2.2.20.html
Running on: Linux 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 UTC 2020 x86_64

2.3 準備service檔案

[root@cetnos7 ~]#vim /usr/lib/systemd/system/haproxy.service
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target

[Service]
ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q
ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/lib/haproxy/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID
LimitNOFILE=100000

[Install]
WantedBy=multi-user.target

2.4 建立配置檔案

[root@centos7 ~]# vim /etc/haproxy/haproxy.cfg
global
	maxconn 100000
	chroot /apps/haproxy
	stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin
	#uid 99
	#gid 99
	user haproxy
	group haproxy
	daemon
	#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 local2 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 haadmin:123456  #狀態頁的登入賬戶和密碼
	
listen web_port
	bind 10.0.0.7:80
	mode http
	log global
	server web1 127.0.0.1:8080 check inter 3000 fall 2 rise 5

2.5 啟動haproxy

#準備socket檔案目錄
[root@centos7 ~]# mkdir /var/lib/haproxy
[root@centos7 ~]# useradd -r -s /sbin/nologin -d /var/lib/haproxy haproxy
[root@centos7 ~]# systemctl start haproxy

2.6 檢視hapoxy的狀態頁面



到此,haproxy已安裝完成