## 安装依赖包
- haproxy部署安装对环境有要求,一键安装以下依赖即可。
```bash
yum -y install wget gcc gcc-c++
```
## 下载并解压安装包
- 先新建一个文件夹,博主喜欢在`/var`下面安装服务器软件
```bash
cd /var
mkdir haproxy
cd haproxy
```
- 下载tar包并解压缩
```bash
wget https://github.com/haproxy/haproxy/archive/v1.5-dev20.tar.gz && tar -zvxf v1.5-dev20.tar.gz
cd haproxy-1.5-dev20
```
## 安装nginx
- 查看内核版本
```bash
uname -a
```
![][image-1]
- 如图所示内核版本为3.10开头,执行下列编译命令:
```bash
make TARGET=linux310 ARCH=x86_64 PREFIX=/usr/local/haproxy
make install PREFIX=/usr/local/haproxy
```
> 这里可能会报错`cannot bind UNIX socket [/var/lib/haproxy/stats]`
> 解决方案:`mkdir -p /var/lib/haproxy && touch /var/lib/haproxy/stats`
## 配置haproxy.conf
- 进入编译后的haproxy文件夹
```bash
cd /usr/local/haproxy
```
- 新建conf目录,并新建一个配置文件
```bash
mkdir conf
touch /usr/local/haproxyconf/haproxy.conf
```
- 编辑配置文件
```bash
vim /usr/local/haproxyconf/haproxy.conf
```
> 这里配置从简,制作最基本的配置。
```bash
global
log 127.0.0.1 local2 #[err warning info debug]
chroot /usr/local/haproxy
pidfile /var/run/haproxy.pid #haproxy的pid存放路径,启动进程的用户必须有权限访问此文件
maxconn 65535 #最大连接数,默认4000
daemon #后台启动
defaults
mode tcp
log global
timeout connect 20s
timeout server 60s
timeout client 60s
retries 3
listen stats
mode http
bind 0.0.0.0:8989 #监听端口
stats refresh 10s #统计页面自动刷新时间
stats uri /stats #统计页面url
stats realm Haproxy Manager #统计页面密码框上提示文本
stats auth chihiro:chihiroadmin #统计页面用户名和密码设置
stats hide-version #隐藏统计页面上HAProxy的版本信息
listen chihirov1
bind :8901 #监听端口
server s1 xxx.xxx.xxx.xxx:1234 #转发IP+端口
listen chihirov2
bind :8902 #监听端口
server s2 xxx.xxx.xxx.xxx:5678 #转发IP+端口
```
## 启动haproxy
```bash
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg
```
- 查看nginx是否启动:`ps -ef | grep haproxy`
![][image-2]
## 打开防火墙
- 博主使用的CentOS7,开放防火墙的8989端口
```bash
firewall-cmd --zone=public --add-port=8989/tcp --permanent
firewall-cmd --reload
```
> 这里只打开了haproxy的监控端口,后续代理了什么端口也要相应的打开防火墙端口。
至此haproxy已经搭建完成,去做测试吧WwW
[image-1]: https://s2.ax1x.com/2019/08/01/ea2mm4.png
[image-2]: https://s2.ax1x.com/2019/08/01/ea2HuF.png

Linux下haproxy安装