Learn how to set up load balancing using HAProxy on Ubuntu 24.04 Server.
HAProxy (High Availability Proxy) is an open-source TCP and HTTP load balancer widely used for distributing network traffic across multiple servers. It enhances performance, scalability, and reliability by ensuring no single server becomes overloaded. Known for its speed, stability, and advanced features like health checks, SSL termination, and session persistence, HAProxy is a trusted choice for high-traffic environments and enterprise infrastructure.
In this guide, we’ll set up HAProxy on Ubuntu 24.04 and configure it to balance web traffic efficiently across backend servers.
Prerequisites
Before we begin, ensure we have the following:
- An Ubuntu 24.04 on dedicated server or KVM VPS.
- Basic Linux Command Line Knowledge.
Set Up Load Balancing with HAProxy on Ubuntu
Step 1: Update the System
Before installing anything, it’s a good practice to ensure our system is up to date.
sudo apt update && sudo apt upgrade -y
This keeps our server secure and ensures all dependencies are current.
Step 2: Install HAProxy
Ubuntu 24.04 includes the latest stable version of HAProxy in its repositories.
Run:
sudo apt install haproxy -y
Once installed, verify that HAProxy is available:
haproxy -v
You should see the version information confirming a successful installation.
Step 3: Enable and Start HAProxy Service
By default, the service is disabled. Let’s enable and start it:
sudo systemctl enable haproxy
sudo systemctl start haproxy
sudo systemctl status haproxy
If the status shows “active (running)”, our load balancer is now live.
Step 4: Configure Backend Web Servers
We’ll need at least two backend web servers for HAProxy to balance traffic.
Let’s assume our backend servers are:
Web Server 1: 192.168.1.101
Web Server 2: 192.168.1.102
Both servers should have a running web service (like Nginx or Apache).
Test them:
curl http://192.168.1.101
curl http://192.168.1.102
If both return valid responses, we’re good to proceed.
Step 5: Configure HAProxy Frontend and Backend
Now we edit HAProxy’s main configuration file:
sudo nano /etc/haproxy/haproxy.cfg
Scroll down and replace or add the following section at the end of the file:
frontend http_front
bind *:80
mode http
default_backend web_backends
backend web_backends
mode http
balance roundrobin
option httpchk
http-check send meth HEAD uri / ver HTTP/1.1 hdr Host localhost
server web1 192.168.1.101:80 check
server web2 192.168.1.102:80 check
Explanation:
- frontend listens for incoming HTTP connections on port 80.
- backend defines our actual web servers.
- balance roundrobin distributes requests evenly between all servers.
- httpchk performs health checks to ensure backend availability.
Step 6: Test the Configuration
Before restarting HAProxy, always test for syntax errors:
sudo haproxy -c -f /etc/haproxy/haproxy.cfg
If the output says Configuration file is valid, we’re ready to reload.
Restart the service:
sudo systemctl restart haproxy
Step 7: Verify Load Balancing
If UFW is enabled, allow HAProxy’s traffic:
sudo ufw allow 80/tcp
We can test load distribution by sending multiple requests:
curl http://<haproxy_server_ip>
Repeat this several times — responses should alternate between backend servers.
For a clearer view, open the two backend servers and display unique hostnames in their index pages. Each request routed through HAProxy will show which server responded.
Step 8: Enable HAProxy Statistics Dashboard (Optional but Useful)
To monitor performance and backend status, we can enable the HAProxy stats page.
Edit the configuration file again:
sudo nano /etc/haproxy/haproxy.cfg
Add this block at the end:
listen stats
bind *:8080
stats enable
stats uri /stats
stats refresh 10s
stats auth admin:securepass
Restart HAProxy:
sudo systemctl restart haproxy
Now open:
http://<haproxy_server_ip>:8080/stats
Login with admin and securepass to see real-time server metrics.
Step 9: Secure HAProxy (Firewall Configuration)
If UFW is enabled, allow HAProxy’s traffic:
sudo ufw allow 8080/tcp
sudo ufw reload
This ensures that clients and administrators can access HAProxy and the stats dashboard safely.
Step 10: Make HAProxy Start at Boot
It’s best practice to keep HAProxy running automatically after a reboot:
sudo systemctl enable haproxy
This guarantees our load balancing setup remains active without manual intervention.
Conclusion
By following these steps, we’ve built a reliable and scalable load balancing setup with HAProxy on Ubuntu 24.04. This setup ensures high availability, improves response time, and enhances redundancy across our backend servers.
In production environments, we can extend this by:
- Using SSL termination for HTTPS traffic
- Implementing sticky sessions for applications needing session persistence
- Adding backend weighting for performance tuning
With HAProxy, our infrastructure gains the stability and flexibility needed for modern web applications. It’s simple, powerful, and ready to handle serious traffic.