How to Install HAProxy on Debian 11 server

HAProxy is a free open-source load balancer, a high-performance, and proxy server for TCP and HTTP-based applications. It distributes the workload between the web and application servers. It is specifically developed for high-traffic websites to improve performance, availability, and redundancy.
Because of Haproxy’s reliability, efficiency, and low memory and CPU footprint, it is widely used for load balancing. Load balancing is a popular solution to distribute web applications horizontally across multiple servers while providing users with a single point of access to the service.
Let us see how to install and configure HAProxy load balancing with two web servers on Debian 11.
Update the Debian System
Before installing any software, ensure that your system uses the latest available software packages on your Debian system
sudo apt update
sudo apt -y upgrade
Once completed, restart the server
sudo reboot
Install Apache Backend Servers
Now we will set up two backend Apache servers. To install and configure the Apache package, run the following commands:
sudo apt install apache2
After the apache2 installation, use the below command to predefine message in index.html
Server 1:
echo “<H1>Hello from Apache Server1</H1>” | sudo tee /var/www/html/index.html
Server 2:
echo “<H1>Hello from Apache Server2</H1>” | sudo tee /var/www/html/index.html
Note: This step is optional if you wish to use HAProxy without Apache.
Install HAProxy on Debian 11
To install HAProxy, run the below command on both the servers:
sudo apt -y install haproxy
Configure HAProxy as a load balancer
Now, set up HAproxy to employ a round-robin balance between the two servers.
Open the file /etc/haproxy/haproxy.cfg and add the following configuration. Then, save and close your file.
frontend apache_front
# Frontend listen port – 80
bind *:80
# Set the default backend
default_backend    apache_backend_servers
# Enable send X-Forwarded-For header
option             forwardfor
 
# Define backend
backend apache_backend_servers                                                                                                                    
# Use roundrobin to balance traffic
balance            roundrobin
# Define the backend servers
server             backend01 192.168.10.20:80 check
server             backend02 192.168.10.21:80 check
Restart HAProxy on both servers by following these steps:
sudo systemctl restart haproxy
Once the configuration is completed, then open your web browser and enter the URL http://your-haproxy-ip-address. After each refresh, HAProxy will send requests to backend servers one by one.
Configure SSL
Combine a private key and a certificate file with the following command:
cat fullchain.pem privkey.pem > haproxy.pem
Next, configure the HAProxy to use the SSL certificate on the front end.
frontend apache-frontend
        bind *:80
        bind *:443 ssl crt /etc/letsencrypt/live/webapp.computingforgeeks.com/haproxy.pem
 
You have now successfully installed and configured the HAProxy load-balancing server. This will help to increase your web server performance.
Also, check: HAProxy vs NGINX for Load Balancing
To get more updates you can follow us on Facebook, Twitter, LinkedIn
Subscribe to get free blog content to your Inbox

Related articles

Latest articles