如何在 CentOS 7 上安装 HAProxy

在本教程中,我们将向您展示如何在您的 CentOS 7 服务器上安装和配置 HAProxy。 对于那些不知道的人,HAProxy 是一个免费的 HTTP/TCP 高可用性负载平衡器和代理服务器。 它将请求分布在多个服务器之间,以减轻由单个服务器故障引起的问题。 HA Proxy 被许多知名网站使用,包括 GitHub、Bitbucket、Stack Overflow、Reddit、Tumblr、Twitter 和 Tuenti,并用于 Amazon Web Services 的 OpsWorks 产品中。

本文假设您至少具备 Linux 的基本知识,知道如何使用 shell,最重要的是,您将网站托管在自己的 VPS 上。 安装非常简单,假设您在 root 帐户下运行,如果不是,您可能需要添加 ‘sudo‘ 到命令以获取 root 权限。 我将向您展示如何在 CentOS 7 服务器上逐步安装 HAProxy。

在 CentOS 7 上安装 HAProxy

第 1 步。首先,让我们首先确保您的系统是最新的。

yum clean all yum -y update

步骤 2. 安装 HAProxy。

HAProxy 包默认在 CentOs 和 RHEL 存储库中可用。 我们可以使用 yum 命令安装它,如下所示:

yum -y install haproxy

步骤 3. 配置 HAProxy。

我们必须修改HAProxy的配置文件,即 /etc/haproxy/haproxy.cfg 根据我们的要求。 (根据您的网络要求更改此配置)。 有关更多配置详细信息,请检查此 网址.

#nano /etc/haproxy/haproxy.cfg global  log 127.0.0.1 local0 log 127.0.0.1 local1 debug maxconn 45000 # Total Max Connections. This is dependent on ulimit user haproxy group haproxy daemon  defaults timeout server 86400000 timeout connect 86400000 timeout client 86400000 timeout queue 1000s  # Configuration for HTTP site listen http_idroot 192.168.2.102:80 mode http balance roundrobin # Load Balancing algorithm option httpchk option forwardfor server server1 192.168.2.100:80 weight 1 maxconn 512 check server server2 192.168.2.101:80 weight 1 maxconn 512 check  # Configuration for HTTPS site listen   https_idroot 192.168.2.102:443 mode tcp balance source# Load Balancing algorithm reqadd X-Forwarded-Proto: http server server1 192.168.2.100:443 weight 1 maxconn 512 check server server2 192.168.2.101:443 weight 1 maxconn 512 check  listen stats 192.168.2.102:31337 mode http option httpclose balance roundrobin stats uri / stats realm Haproxy Statistics stats refresh 5s stats auth admin:passwd123

配置 HAProxy 后,就该启动服务了:

systemctl start haproxy systemctl enable haproxy

步骤 4. 访问 HAProxy。

默认情况下,HAProxy 将在 HTTP 端口 8980 上可用。 打开您喜欢的浏览器并导航到 https://yourdomain.com:31337 或者 https://server-ip:31337. 它会要求您输入用户名和密码。 使用您在配置文件中定义的用户名和密码作为“stats auth”。 如果您使用防火墙,请打开端口 31337 以启用对控制面板的访问。

恭喜! 您已成功安装 HAProxy。 感谢您使用本教程在您的 CentOS 7 系统上安装 HAProxy。 如需其他帮助或有用信息,我们建议您查看 HAProxy 官方网站.

Save

Save