In this tutorial, we will show you how to install and configuration of HAProxy on your CentOS 7 server. For those of you who didn’t know, HAProxy is a free HTTP/TCP high availability load balancer and proxy server. It spreads requests among multiple servers to mitigate issues resulting from a single server failure. HA Proxy is used by a number of high-profile websites including GitHub, Bitbucket, Stack Overflow, Reddit, Tumblr, Twitter, and Tuenti, and is used in the OpsWorks product from Amazon Web Services.
This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo’ to the commands to get root privileges. I will show you through the step by step installation HAProxy on a CentOS 7 server.
Install HAProxy on CentOS 7
Step 1. First, let’s start by ensuring your system is up-to-date.
yum clean all yum -y update
Step 2. Installing HAProxy.
HAProxy package is by default available in CentOs and RHEL Repositories. We can install it by using the yum command as follows:
yum -y install haproxy
Step 3. Configuring HAProxy.
We have to modify the configuration file of HAProxy i.e. /etc/haproxy/haproxy.cfg as per our requirement. (Change this configuration as your network requirements). For more configuration details check this url.
#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
Once you have configured HAProxy, its time to start the service:
systemctl start haproxy systemctl enable haproxy
Step 4. Accessing HAProxy.
HAProxy will be available on HTTP port 8980 by default. Open your favourite browser and navigate to http://yourdomain.com:31337 or http://server-ip:31337. It will ask you for the username and password. Use the username and password you defined on the configuration file as “stats auth”. If you are using a firewall, please open port 31337 to enable access to the control panel.
Congratulations! You have successfully installed HAProxy. Thanks for using this tutorial for installing HAProxy on your CentOS 7 system. For additional help or useful information, we recommend you to check the official HAProxy website.