HAProxy is an open-source solution with many benefits. As the name says, it offers a proxy server with a high-performance load balancer for TCP and HTTP-based applications. HAProxy is used on many web-based setups to enhance the performance and reliability of web-based applications by distributing the load among multiple servers. In this tutorial, let’s see how to install and configure HAProxy on Ubuntu.
Step 1 – Setup Backend Servers
The first step is to install Apache on both the backend and frontend servers. So login to your servers one by one and execute the command given
# sudo apt-get install apache2 -y
As the installation completes, let’s create a test index.html file on both servers
# echo "<h1>My first Apache Server</h1>" | tee /var/www/html/index.html
# echo "<h1>My second Apache Server</h1>" | tee /var/www/html/index.html
Step 2 – How to install HAProxy
In Ubuntu, HAProxy packages are available by default so you have to just install it by executing the command given
# sudo apt-get install haproxy -y
As the installation completes, let’s check the HAProxy version
# haproxy -v
Let’s start and enable the HAProxy service
# systemctl start haproxy
# systemctl enable haproxy
Step 3 – How to Configure HAProxy
HAProxy configuration is available in /etc/haproxy/haproxy.cfg. So let’s open it and add some configurations
# sudo nano /etc/haproxy/haproxy.cfg
copy and paste the following lines and save the file
frontend haproxy-main
bind *:80
option forwardfor
default_backend apache_webservers
backend apache_webservers
balance roundrobin
server websvr1 192.168.1.101:80 check
server websvr2 192.168.1.102:80 check
- Frontend section shows the HAProxy for the frontend server port that is the listening port (It’s on 80 as of now)
- Backend section has the details of the servers to which the traffic will be distributed
NOTE:- Make sure to replace 192.168.1.101 and 192.168.1.102 with your backend web servers IP
Step 4 – How to Setup HAProxy Authentication
Here we will use the 8800 port for HAProxy authentication, and statistics page. The below configuration shows an example configuration for basic authentication and backend servers. Let’s open haproxy.cfg file and add the configuration
# nano /etc/haproxy/haproxy.cfg
copy and paste the following
listen stats
bind :8800
stats enable
stats uri /
stats hide-version
stats auth admin:password
default_backend apache_webservers
Now save and close the file and restart the services
# sudo systemctl restart haproxy
Let’s verify HAProxy
# sudo systemctl status haproxy
Output:
● haproxy.service - HAProxy Load Balancer
Loaded: loaded (/lib/systemd/system/haproxy.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2024-07-12 1:32:27 IST; 9s ago
Docs: man:haproxy(1)
file:/usr/share/doc/haproxy/configuration.txt.gz
Process: 44208 ExecStartPre=/usr/sbin/haproxy -Ws -f $CONFIG -c -q $EXTRAOPTS (code=exited, status=0/SUCCESS)
Main PID: 44210 (haproxy)
Tasks: 5 (limit: 9188)
Memory: 70.0M
CPU: 78ms
CGroup: /system.slice/haproxy.service
├─44210 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -S /run/haproxy-master.sock
└─44212 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -S /run/haproxy-master.sock
May 25 12:32:27 ubuntupc systemd[1]: Starting HAProxy Load Balancer...
May 25 12:32:27 ubuntupc haproxy[44210]: [NOTICE] (44210) : New worker #1 (44212) forked
May 25 12:32:27 ubuntupc systemd[1]: Started HAProxy Load Balancer.
Step 5 – Test HAProxy
Now let’s test our HAProxy server. Open any browser enter your server’s IP and hit enter. It should show you the test page “My first Apache Server” page that we have created above.