Learn how to install HAProxy on CentOS 7 with this comprehensive guide. Step-by-step instructions, prerequisites, and tips to get your HAProxy load balancer up and running efficiently. #centlinux #linux #haproxy
Table of Contents
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.
Here are some key features and functions of HAProxy:
- Load Balancing: Distributes incoming traffic across multiple servers to ensure no single server is overwhelmed, enhancing performance and reliability.
- High Availability: Ensures continuous service by automatically rerouting traffic to healthy servers if one or more servers fail.
- Scalability: Allows for the addition of more servers to handle increasing traffic demands without downtime.
- Layer 4 (TCP) and Layer 7 (HTTP) Proxying: Can manage traffic at both the transport layer (TCP) and the application layer (HTTP), providing flexibility in handling various types of network traffic.
- SSL Termination: Offloads the SSL encryption and decryption process from the backend servers, improving their performance.
- Health Checks: Continuously monitors the health of backend servers and only directs traffic to servers that are up and running.
- Session Persistence: Ensures that a user’s session remains on the same server, which is important for applications that require user state to be maintained.
- Security: Provides various features like rate limiting, IP whitelisting/blacklisting, and protection against common web threats.
Overall, HAProxy is widely used in many high-traffic websites and applications due to its robustness, efficiency, and versatility.

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.
If you’re serious about leveling up your Linux skills, I highly recommend the Linux Mastery: Master the Linux Command Line in 11.5 Hours by Ziyad Yehia course. It’s a practical, beginner-friendly program that takes you from the basics to advanced command line usage with clear explanations and hands-on exercises. Whether you’re a student, sysadmin, or developer, this course will help you build the confidence to navigate Linux like a pro.
👉 Enroll now through my affiliate link and start mastering the Linux command line today!
Disclaimer: This post contains affiliate links. If you purchase through these links, I may earn a small commission at no extra cost to you, which helps support this blog.
Linux Server Specification
We have two Apache Web Servers webserver-01.example.com and webserver-02.example.com and we want to configure a HTTP Load Balancer for them i.e. haproxy-01.example.com.
| Hostname | haproxy-01.example.com | webserver-01.example.com | webserver-02.example.com |
| IP Address | 192.168.116.30/24 | 192.168.116.31/24 | 192.168.116.32/24 |
| Operating System | CentOS 7 | CentOS 7 | CentOS 7 |
| Web Server | NA | Nginx | Nginx |
Install HAProxy on CentOS 7
To ensure that our webservers are properly configured and browsable, open their URLs in a Browser.


I have set different index pages on both servers, to differentiate between servers, when we are accessing using the load balancer.
Connect to the haproxy-01.example.com and install HAProxy.
yum install -y haproxyHAProxy has been installed.
Now configure rsyslog to handle HAProxy logs.
vi /etc/rsyslog.confSearch and uncomment following lines.
$ModLoad imudp
$UDPServerRun 514Add following directive in the same file.
# HAProxy Logging
local2.* /var/log/haproxy.logSave settings and exit.
Restart rsyslog service to apply changes.
systemctl restart rsyslog.serviceNow configure HAProxy settings according to our environment.
vi /etc/haproxy/haproxy.cfgAdd following directives in this file.
# HAProxy Load Balancer for Apache Web Server
frontend http-balancer
bind 192.168.116.30:80
default_backend web-servers
backend web-servers
mode http
balance roundrobin
stats enable
stats auth admin:123
server webserver-01 192.168.116.31:80 check
server webserver-02 192.168.116.32:80 checkSave settings and exit.
Start and enable haproxy service.
systemctl start haproxy
systemctl enable haproxyAllow HTTP service through Linux firewall.
firewall-cmd --permanent --add-service=http
firewall-cmd --reloadTest HAProxy Configuration
Open the URL of the haproxy-01.example.com in a browser.

You may observe that the HTTP request has been served by webserver-01.example.com. It shows that our Load Balancer has forwarded the request to this server.
Press F5 to refresh the webpage.

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 http://haproxy-01.example.com/haproxy?stats.

We have successfully configured a HTTP Load Balancer with HAProxy.
Read Also: How to install KeepAlived on CentOS 7
Final Thoughts
Installing HAProxy on CentOS 7 is a straightforward process that can significantly improve the performance and reliability of your web applications. By following this guide, you should now have a functional HAProxy setup ready to manage and distribute traffic efficiently.
Whether you need cloud optimization, server management, or automation, I provide comprehensive AWS and Linux services. Hire me to elevate your systems.
