Share on Social Media

In this configuration guide, you will learn, how to install HAProxy on CentOS 7 or other Red hat based Linux servers. #centlinux #linux #haproxy

What is HAProxy?

HAProxy is an open-source, high-performance TCP/HTTP load balancer and proxy server. It is commonly used to distribute incoming traffic across multiple servers to ensure high availability and reliability of web services and applications. HAProxy can efficiently handle a large number of concurrent connections and provides features such as SSL termination, health checks, request routing, and session persistence. It’s often deployed in front of web servers, application servers, or other backend services to improve performance, scalability, and fault tolerance. Additionally, HAProxy supports various load-balancing algorithms and provides detailed monitoring and logging capabilities to help administrators manage and troubleshoot their infrastructure effectively.

What is Load Balancing?

Load balancing refers to efficiently distributing incoming network traffic across a group of backend servers, also known as a server farm or server pool. Load Balancing improves the distribution of workloads across multiple computing resources. Using multiple components with load balancing instead of a single component may increase reliability and availability through redundancy.

Load Balancer is the device that perform Load Balancing. Hardware and software load balancers may have a variety of special features. The fundamental feature of a load balancer is to be able to distribute incoming requests over a number of backend servers in the cluster according to a scheduling algorithm.

In this post, we will configure a HTTP Load Balancer with HAProxy to distribute HTTP workload between two webservers.

Table of Contents:

System Specification:

We have two Apache Web Servers and and we want to configure a HTTP Load Balancer for them i.e.
IP Address192.168.116.30/24192.168.116.31/24192.168.116.32/24
Operating SystemCentOS 7CentOS 7CentOS 7
Web ServerNANginxNginx

Install HAProxy on CentOS 7:

To ensure that our webservers are properly configured and browsable, open their URLs in a Browser.

Webserver-01 Main Page
Webserver-02 Main Page

I have set different index pages on both servers, to differentiate between servers, when we are accessing using the load balancer.

Connect to the and install HAProxy.

# yum install -y haproxy

HAProxy has been installed.

Now configure rsyslog to handle HAProxy logs.

# vi /etc/rsyslog.conf

Search and uncomment following lines.

$ModLoad imudp
$UDPServerRun 514

Add following directive in the same file.

# HAProxy Logging
local2.*        /var/log/haproxy.log

Save settings and exit.

Restart rsyslog service to apply changes.

# systemctl restart rsyslog.service

Now configure HAProxy settings according to our environment.

# vi /etc/haproxy/haproxy.cfg

Add following directives in this file.

# HAProxy Load Balancer for Apache Web Server
frontend http-balancer
 default_backend web-servers

backend web-servers
 mode http
 balance roundrobin
 stats enable
 stats auth admin:123
 server webserver-01 check
 server webserver-02 check

Save settings and exit.

Start and enable haproxy service.

# systemctl start haproxy ; systemctl enable haproxy
ln -s '/usr/lib/systemd/system/haproxy.service' '/etc/systemd/system/'

Allow HTTP service through Linux firewall.

# firewall-cmd --permanent --add-service=http
# firewall-cmd --reload

Test HAProxy Configuration:

Open the URL of the in a browser.

Webserver-01 Main Page Through Load Balancer

You may observe that the HTTP request has been served by It shows that our Load Balancer has forwarded the request to this server.

Press F5 to refresh the webpage.

Webserver-02 Main Page Through Load Balancer

Since, we have configured the roundrobin balancing in HAProxy. Therefore, it forwards next request to alternate server. You can refresh webpage multiple times to observe the functionality.

To see the detailed statistics of the HAProxy. Browse to

HAProxy Statistics Webpage

We have successfully configured a HTTP Load Balancer with HAProxy.


In this configuration guide, you have learned, how to install HAProxy on CentOS 7 or other Red hat based Linux servers.