In this blog post, we will show you how to install and configure HAProxy on Ubuntu 24.04 step by step, and set it up to load balance traffic between two Apache web servers.
What is HAProxy?
HaProxy, short for High Availability Proxy, is a free and open-source HTTP load balancer and reverse-proxy solution that is widely used to provide high availability to web applications and guarantee maximum possible uptime.
It is a high-performance application that injects performance improvements to your web apps by distributing traffic across multiple endpoints. This way, it ensures that no webserver is overloaded with incoming HTTP requests since the workload is equitably distributed across several nodes.
While free, the Enterprise Edition provides added features such as WAF ( Web Application Firewall ), application acceleration, advanced DDoS protection, advanced health checks and so much more.
Lab setup
To demonstrate HAProxy in action, you need to have at least three Linux systems. One will act as the HAProxy load balancer, while the rest will act as web servers.
Step 1) Install HAProxy on Ubuntu 24.04
The first step is to install HAProxy on Ubuntu 24.04. Although HAProxy is available in the default Ubuntu repositories, it may not always be the latest version. To check the available HAProxy package version in the default repositories, run the following apt command
$ sudo apt update $ sudo apt show haproxy
As shown in the output above, Ubuntu 24.04 provides HAProxy version 2.8.5 in its default repositories. However, at the time of writing this post, the latest LTS release of HAProxy is version 3.2. To install HAProxy 3.2 on Ubuntu 24.04, we need to add its official PPA repository using the following command.
$ sudo add-apt-repository ppa:vbernat/haproxy-3.2
Now, install haproxy 3.2 by executing the following commands
$ sudo apt update $ sudo apt install -y haproxy=3.2.\*
Once installed, confirm the version of HAProxy installed as shown.
$ haproxy -v
After installation, the HAProxy service starts automatically and listens on TCP port 80 by default. To verify that HAProxy is running on Ubuntu 24.04, use the following command:
$ sudo systemctl status haproxy
It’s recommended to enable the service to auto-start on very system reboot as shown.
$ sudo systemctl enable haproxy
Step 2) Configure HAProxy
The next step is to configure HAProxy to distribute traffic evenly between two web servers. The configuration file for haproxy is /etc/haproxy/haproxy.cfg
Before making any changes to the file, first, make a backup copy.
$ sudo cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bk
Then open the file using your preferred text editor. Here, we are using Nano.
$ sudo nano /etc/haproxy/haproxy.cfg
The HAProxy configuration file on Ubuntu 24.04 is divided into several important sections, each serving a specific purpose:
- global: This section appears at the top of the configuration file and defines system-wide parameters, such as performance tuning, logging, and security settings.
- defaults: This section provides default settings for HAProxy, including timeout values and error handling. These configurations work well in most cases without additional customization.
- frontend and backend: The frontend section defines how HAProxy accepts client connections, while the backend section specifies the web servers that handle the requests. In our setup, HAProxy acts as the frontend and distributes requests to multiple Apache backend servers using the round-robin load balancing method.
- listen: This optional section allows you to configure monitoring and view HAProxy statistics through a dedicated web interface.
Now define the frontend and backend settings:
frontend linuxtechi bind 10.128.0.25:80 stats uri /haproxy?stats default_backend web-servers backend web-servers balance roundrobin server web1 10.128.0.27:80 server web2 10.128.0.26:80
Here, we have configured both the HAProxy server and the web server nodes to listed to port 80. Be sure to replace the IP address for the HAProxy and webservers with your setup.
In order to enable viewing the HAProxy statistics from a browser, add the following ‘listen’ section.
listen stats bind *:8080 stats enable stats uri / stats refresh 5s stats realm Haproxy\ Statistics stats auth linuxtechi:P@ss123 #Login User and Password for the monitoring
The stats auth directive specifies the username and password for the login user for viewing statistics on the browser.
Now save all the changes and exit the configuration file. To reload the new settings, restart haproxy service.
$ sudo systemctl restart haproxy
Next edit the /etc/hosts file and define the hostnames and IP addresses of the haproxy main server and the web servers.
10.128.0.25 haproxy 10.128.0.27 web1 10.128.0.27 web2
Save the changes and exit.
Step 3) Configure Web Servers for HAProxy
In this step, we will configure the remaining Linux systems which are the web servers.
So, log in to each of the web servers and install the Apache web server package.
$ sudo apt update $ sudo apt install -y apache2
Next, verify that Apache is running on each of the servers.
$ sudo systemctl status apache2
Then enable Apache webserver to start on boot on both servers.
$ sudo systemctl enable apache2
Next, modify the index.html files for each web server.
For Web Server 1
Switch to the root user
$ sudo su
Then run the following command.
# echo "<H1>Hello! This is webserver1: 10.128.0.27 </H1>" > /var/www/html/index.html
For Web Server 2
Similarly, switch to the root user
$ sudo su
And create the index.html file as shown.
# echo "<H1>Hello! This is webserver2: 10.128.0.26 </H1>" > /var/www/html/index.html
Next, configure the /etc/hosts file.
$ sudo nano /etc/hosts
Add the HAProxy entry to each node.
10.128.0.25 haproxy
Save the changes and exit the configuration file.
Be sure you can ping the HAProxy server from each of the web server nodes.
Note: Make Sure port 80 is allowed in the OS firewall in case it is enabled on web servers.
Step 4) Test HAProxy Load Balancing
Up until this point, we have successfully configured our HAProxy and both of the back-end web servers. To test if your configuration is working as expected, browse the IP address of the HAProxy server
http://10.128.0.25
When you browse for the first time, you should see the web page for the first web server
Upon refreshing, you should the webpage for the second web server
This shows that the HAProxy server is performing its load balancing job spectacularly by distributing incoming web traffic across the two web servers in a Round Robbin algorithm.
Moreover, you can use the following do-while loop with the curl command:
$ while true; do curl 10.128.0.25; sleep1; done
To view monitoring statistics, browse the following URL:
http://10.128.0.25:8080/stats
You will be required to authenticate, so provide your details as specified in Step 2.
You will see the following page with statistics on the performance of the HAProxy server.
Conclusion
You have successfully installed the latest version of HAProxy on Ubuntu 24.04. With HAProxy running, you can now configure it for load balancing, SSL termination, or reverse proxying based on your project needs.
Using HAProxy not only improves the performance and reliability of your applications but also strengthens your server security.
Also Read: How to Configure Static IP Address on Ubuntu 24.04 (Desktop)
Thank you!
When should I use HAProxy instead of Nginx in terms of load balancing?
What benefit of using HAProxy in that case?
Use HAProxy for high-performance load balancing, especially when dealing with large-scale traffic or TCP-based applications. It offers advanced load-balancing algorithms, robust health checks, and superior scalability compared to Nginx.
HAProxy is faster & smaller. You are able to routing & rewriting with it. And you get a basic stats gui.
It also conforms to the Unix ethos of “do one thing and donit well”
HAProxy keeps failing if I set it to port 80 on the frontend of the cfg. Can I use port 80 through some other means
Hi,
Yes, you can definitely use a different port. However, I recommend checking if any other service is currently listening on port 80 besides HAProxy.