HAProxy, an open-source load balancer, is essential for optimizing the performance and reliability of your web applications. By distributing incoming traffic across multiple servers, it ensures that no single server becomes overwhelmed, thus maintaining the availability and stability of your services. Ubuntu, a popular Linux distribution, provides an ideal environment for deploying HAProxy due to its robustness and user-friendly nature.

In this guide, we will walk you through the process of installing and configuring HAProxy on Ubuntu 24.04 and 22.04 LTS. Whether you are a beginner or have some experience with Linux, this tutorial is designed to be simple and straightforward, allowing you to set up HAProxy efficiently and effectively.
Step-by-Step Installation and Configuration
Step 1: Update Your System
Before installing HAProxy, it’s important to update your system’s package list to ensure you have the latest software versions. Open a terminal and run:
sudo apt update && sudo apt upgrade -y
Step 2: Install HAProxy
Once your system is updated, you can install HAProxy packages using the following command:
sudo apt install haproxy -y
Step 3: Verify HAProxy Installation
To verify that HAProxy has been installed successfully, check its version by running:
haproxy -v
You should see output similar to:
Step 4: Configure HAProxy
Now that HAProxy is installed, it’s time to configure it. Open the HAProxy configuration file in a text editor:
sudo nano /etc/haproxy/haproxy.cfg
Add the following basic configuration to the file:
Here we defined on frontend that will “http_front” that will listen requests on port 90. Then it will forward all incoming requests on port 80 to defined backend servers 192.168.1.2 and 192.168.1.3 on port 80.
Step 5: Restart HAProxy
After saving your configuration file, restart HAProxy to apply the changes:
sudo systemctl restart haproxy
Also, enable HAProxy to start on boot:
sudo systemctl enable haproxy
Step 6: Check HAProxy Status
Finally, check the status of HAProxy to ensure it’s running correctly:
sudo systemctl status haproxy
You should see output indicating that HAProxy is active and running.
Conclusion
Congratulations! You have successfully installed and configured HAProxy on your Ubuntu 24.04 or 22.04 LTS system. By following these steps, you’ve set up a robust load balancing solution that will help improve the performance and reliability of your web applications. Remember to periodically check your HAProxy configuration and logs to ensure everything runs smoothly and to make adjustments as needed.