
Ready to set up a powerful load balancer on your Ubuntu 24.04 server? HAProxy is an excellent choice, known for its high performance and reliability. This guide walks you through the essential steps to install and configure HAProxy, getting your applications distributed efficiently.
First, ensure your system’s package list is current. Open your terminal and run:
sudo apt update
Then, proceed with the HAProxy installation:
sudo apt install haproxy
Once installed, the primary configuration file is located at /etc/haproxy/haproxy.cfg
. It’s recommended to back up the original before making changes:
sudo cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bak
Now, you can edit the configuration file using your preferred text editor, for example:
sudo nano /etc/haproxy/haproxy.cfg
The configuration is structured into sections like global
, defaults
, frontend
, and backend
.
A basic setup involves defining a frontend
to listen on a specific IP and port (commonly port 80 or 443 for web traffic) and a backend
specifying the servers where your application runs. For instance:
frontend http_front
bind *:80
mode http
default_backend http_back
backend http_back
mode http
balance roundrobin
server server1 192.168.1.10:80 check
server server2 192.168.1.11:80 check
This example sets up HAProxy to listen on all interfaces on port 80, forwarding traffic to server1
(192.168.1.10) and server2
(192.168.1.11) using a round-robin load balancing algorithm. The check
keyword enables health checking.
After modifying the configuration, it’s crucial to check for syntax errors:
sudo haproxy -c -f /etc/haproxy/haproxy.cfg
If the syntax is valid, you can proceed to restart or reload the HAProxy service to apply the changes:
sudo systemctl restart haproxy
or
sudo systemctl reload haproxy (for less disruptive updates if the config changes allow)
To ensure HAProxy starts automatically on boot, enable the service:
sudo systemctl enable haproxy
Finally, if you are using the UFW firewall, you may need to allow traffic to the port HAProxy is listening on (e.g., port 80 or 443):
sudo ufw allow 80/tcp
sudo ufw reload
With these steps completed, your HAProxy load balancer should be active on your Ubuntu 24.04 server, efficiently directing traffic to your backend servers. This setup provides a robust foundation for scaling your applications and improving availability.
Source: https://kifarunix.com/install-and-configure-haproxy-on-ubuntu-24-04/