We’ll talk about how to setup HAProxy, also known as High Availability Proxy, on Ubuntu. Follow the guide and find out.
A high Availability Proxy server is an open-source HTTP load balance that provides high availability and throughput to web applications. To elaborate, this software provides maximum uptime and is guaranteed to be fast and efficient.
In addition, HAProxy SSL ensures that the web server is not overloaded with incoming requests by distributing the load across several distributed nodes. In this article, we’ll look at how to set up HAProxy on Ubuntu. We’ll also cover its SSL termination to encrypt and secure the incoming HTTP traffic. So, let’s get started!
Prerequisites
For this tutorial, you will need a minimum of three Ubuntu/Linux systems. One of the systems will work as an HAProxy load balancer, while the remaining will work as web servers. On the other hand, the other servers will send requests to the HAProxy server.
Setup HAProxy SSL Termination on Ubuntu
Update the System
First, update the APT package in all the systems to the latest version. For this step, open the terminal using the shortcut key “Ctrl + Alt + T” and execute the command given below:
sudo apt update
You should get a similar output:

Install HAProxy Load Balancer on Ubuntu
Second, check if HAProxy software already exists in the system or not. Execute the command given below in the Ubuntu system which you’re using as an HAProxy Load Balancer:
sudo apt show haproxy
If you get a similar output, that means HAProxy isn’t installed in the system. Hence, to install it, update the PPA repository. For instance, type:
sudo add-apt-repository ppa:vbernat/haproxy-2.6 -y
The output should look something like this:

After that, install the HAProxy. Input:
sudo apt install -y haproxy=2.6.\*
Wait for the installation to complete. Once installed, verify the version as shown below:
haproxy -v
Output:

Start HAProxy Service
By default, the HAProxy service works on TCP port 80. To ensure that the service is running, check its status. specifically, type:
sudo systemctl status haproxy
Now to configure the system to start this server on reboot, enable this service. For instance, type:
sudo systemctl enable haproxy
You should get a similar output:

Configure HAProxy
The next step is configuring HAProxy software to distribute HTTP traffic among several web servers. In our tutorial, we only have two web servers. To configure the HAProxy, first, locate the config file. It is usually found at /etc/haproxy/haproxy.cfg
.
Before editing the file, create its duplicate:
sudo cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bk
Next, open the text editor of your choice:
sudo nano /etc/haproxy/haproxy.cfg
There is a global
, default
, and listen-to
section already given in the file, as shown below:

Leave the setting as is, and add the configuration for frontend and backend as shown below:
frontend <Domain name>
bind <WebsiteIP>
stats uri /haproxy?stats
default_backend web-servers
backend web-servers
balance roundrobin
server web1 <Server1IP>
server web2 <Server2IP>
Make sure to replace the Server IPs with your web servers. If you have more than 2 web servers, list those under the backend web-servers section.
Next, add the following lines in the listen to stats
section:
listen to stats
bind *:8080
stats enable
stats uri /
stats refresh 5s
stats realm Haproxy\ Statistics
stats auth <Username>:<Password> #Login User and Password for the website
stats realm Haproxy\ Statistics
stats auth <Username>:<Password> #Login User and Password for the website
Lastly, save all the changes and exit the editor. After that, restart the HAProxy service using the restart
command. For example:
sudo systemctl restart haproxy
Next, edit the /etc/hosts
file, and add all the IP addresses and hostnames of the machines involved in this setup. In our case, it is the main HAProxy server and two web servers.
<IP Address> haproxy
<IP Address> web1
<IP Address> web2
Replace the web server IP addresses with actual addresses. Make sure to save the changes and exit the editor.
Configure Web Servers
After the HAProxy server is configured, install Apache web server packages on both web servers. Make sure that the Apache service is running on both servers. Configure both for HAProxy requests so that the HAProxy server can be pinged. First, head over to the first web server and add the HAProxy IP address in /etc/hosts
. Specifically, type:
#nano editor
sudo nano /etc/hosts
#vim editor
sudo vim /etc/hosts
<IPAddress> haproxy
Save the changes and exit the editor. Perform the same step for the other web servers.
Secondly, type the following command in the terminal on both web servers. Make sure to execute the commands as a root user.
# For Web Server 1
echo "<H1>Hello! This is webserver1: 10.128.0.27 </H1>" > /var/www/html/index.html
The command should look something like this:

#For Web Server 2
echo "<H1>Hello! This is webserver2: 10.128.0.26 </H1>" > /var/www/html/index.html
Test HAProxy Load Balancing on Ubuntu
Finally, we’ve completed the configurations. Now, it’s time to test the proxy server for load balancing between the web servers. In the Ubuntu system with the HAProxy server, head over to the browser and type its IP address.
If you refresh the page, you should get the web page of web server 2. This ensures that the HAProxy server performs load balancing between the two web servers. If you have more than 2 web servers, you’d see load balancing among those servers.
Alternatively, run the loop to verify the load balancing feature. For this, run a loop that visits the website continuously after 1 second. Specifically, type:
while true;
do
curl 10.128.0.25;
sleep1;
done
You should get a similar output:

This command will send the CURL request to the given IP Address after each second. You can easily view the stats for these requests on the given URL:
http://10.128.0.25:8080/stats
Finally, you have setup HAProxy on Ubuntu and balanced the web traffic!
And that’s a wrap! We’ve discussed how to setup HAProxy on Ubuntu and perform load balancing through HAProxy software. We’ve also discussed the method to set up web servers and perform load balancing using the Round Robin technique.
The article serves as a guide for beginners. For a detailed guide on HAProxy Server, check out its official documentation. Also, be sure to check out a guide on setting up File Servers on Linux!
If this guide helped you, please share it.