Hosting + Ai Website Builder + Free Domain (3 Month Free Credit)
Shop Today

How to Use HAProxy on Linux – Boost Load Balancing

Use HAProxy on a Linux server to distribute network or application traffic efficiently across multiple backend servers. HAProxy (High Availability Proxy) is a reliable, high-performance TCP/HTTP load balancer and proxy server widely used to improve the availability, scalability, and fault tolerance of web applications and services. It supports sophisticated load balancing algorithms, health checks, SSL termination, and more.

Use HAProxy in Linux

This guide will help you install, configure, manage, and use HAProxy on a Linux server, providing a simple setup for load balancing HTTP traffic.

Prerequisites

  • A Linux server running supported distributions such as Ubuntu, Debian, CentOS, or RHEL
  • Root or sudo privileges for installation and configuration
  • Multiple backend servers (web or application servers) with known IPs or hostnames
  • Basic knowledge of Linux command line and networking

Use HAProxy on a Linux Server

HAProxy is a powerful, open-source load balancer and proxy server for TCP and HTTP traffic. Using HAProxy on a Linux server enhances availability, distributes traffic efficiently, and ensures fault tolerance across backend services, making it ideal for high-traffic or mission-critical environments.

Install HAProxy on the Linux Server

Before configuring HAProxy, you need to install it on your Linux system. Below are the steps for the most common distributions:

  • On Ubuntu/Debian

Update your package lists and install HAProxy:

sudo apt update
sudo apt install haproxy
  • On CentOS/Red Hat (including AlmaLinux, Rocky Linux)

Update your system and install HAProxy:

sudo yum update
sudo yum install haproxy

or on newer systems with dnf:

sudo dnf install haproxy

After installation, verify the HAProxy version:

haproxy -v

Enable and Start HAProxy Service

Start the HAProxy service and enable it to launch on system boot:

sudo systemctl start haproxy
sudo systemctl enable haproxy

Check the service status to ensure it’s running:

sudo systemctl status haproxy

Configure HAProxy

The main configuration file is located at /etc/haproxy/haproxy.cfg.

Before editing, it’s a good idea to back up the original:

sudo cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.backup

Open the config file in your preferred text editor:

sudo nano /etc/haproxy/haproxy.cfg

Example Basic HTTP Load Balancer Configuration

Clear the default content and add the following example:

global
log /dev/log local0
maxconn 2000
user haproxy
group haproxy
daemon

defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms

frontend http_front
bind *:80
default_backend http_back

backend http_back
balance roundrobin
server web1 192.168.1.101:80 check
server web2 192.168.1.102:80 check
  • frontend http_front listens on port 80 for incoming HTTP connections.
  • backend http_back lists backend servers (web1 and web2) with their IPs and ports to forward requests.
  • balance roundrobin ensures requests are distributed evenly.
  • check enables health checks on backend servers.

Save and close the file (Ctrl+O, Enter, Ctrl+X in nano).

Test HAProxy Configuration and Restart

Test the syntax of your configuration:

sudo haproxy -c -f /etc/haproxy/haproxy.cfg

If syntax is OK, restart HAProxy to apply changes:

sudo systemctl restart haproxy

Firewall Configuration

Allow traffic on port 80 through your firewall:

  • For UFW (Ubuntu/Debian):
sudo ufw allow 80/tcp
sudo ufw reload
  • For firewalld (CentOS/RHEL):
sudo firewall-cmd --permanent --add-port=80/tcp
sudo firewall-cmd --reload

Verify Load Balancer Operation

Open a web browser and navigate to your HAProxy server IP or domain on port 80:

http://your_haproxy_server_ip

Refresh the page multiple times to confirm traffic is distributed between the backend servers (you can configure backends to respond with distinct content to verify).

You can also check HAProxy status and stats if configured (advanced).

Conclusion

To use HAProxy on Linux server, install the HAProxy package with your distribution’s package manager, configure the main haproxy.cfg file with frontend and backend sections defining load balancing rules, then start and enable the HAProxy service. Proper firewall configuration allows incoming traffic to flow. HAProxy is a robust, flexible load balancer ideal for improving the scalability and availability of your Linux-hosted applications. For comprehensive details and advanced configurations, visit the official HAProxy documentation.

Himanshu Joshi

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top