HAProxy is a free, open-source, and reliable solution for high availability and load balancing. It distributes the load across the multiple application servers and to simplify the request processing tasks. It can be installed on all major Linux operating systems. It is popular due to its efficiency, reliability, and low memory and CPU footprint.

In this post, we will explain how to install HAProxy on a Debian 11 system.

Prerequisites

  • A server running Debian 11 for HAProxy.
  • Two servers running Debian 11 for Apache Backend server.
  • A root password is configured on all servers.

Setup Backend Web Servers

For the purpose of this tutorial, you will need to set up two backends Apache servers.

On the first backend server, install the Apache package with the following command:

apt-get install apache2 -y

Once the Apache is installed, create a sample Apache index page using the following command:

echo "

Welcome to the first Apache Server

" | tee /var/www/html/index.html

On the second backend server, install the Apache package with the following command:

apt-get install apache2 -y

Next, create a sample Apache index page using the following command:

echo "

Welcome to the second Apache Server

" | tee /var/www/html/index.html

Once you are finished, you can proceed to the next step.

Install HAProxy

By default, HAProxy is included in the Debian 11 default repository. You can install it by running the following command:

apt-get install haproxy -y

Once the HAProxy is installed, start the HAProxy service and enable it to start at system reboot:

systemctl start haproxy

systemctl enable haproxy

Once you are finished, you can proceed to the next step.

Configure HAProxy

Next, you will need to edit the HAProxy default configuration file and define the backend web servers.

nano /etc/haproxy/haproxy.cfg

Add the following lines:

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.1.10:80 check
        server             backend02 192.168.1.11:80 check

Save and close the file when you are finished.

Where: 192.168.1.10 is the IP address of the first Apache backend server and 192.168.1.11 is the IP address of the second Apache backend server.

Next, restart the HAProxy service to apply the changes:

systemctl restart haproxy

You can now check the status of the HAProxy with the following command:

systemctl status haproxy

You will get the following output:

? haproxy.service - HAProxy Load Balancer
     Loaded: loaded (/lib/systemd/system/haproxy.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2021-11-07 03:47:14 UTC; 9s ago
       Docs: man:haproxy(1)
             file:/usr/share/doc/haproxy/configuration.txt.gz
    Process: 86678 ExecStartPre=/usr/sbin/haproxy -f $CONFIG -c -q $EXTRAOPTS (code=exited, status=0/SUCCESS)
   Main PID: 86680 (haproxy)
      Tasks: 3 (limit: 4679)
     Memory: 34.2M
        CPU: 61ms
     CGroup: /system.slice/haproxy.service
             ??86680 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -S /run/haproxy-master.sock
             ??86682 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -S /run/haproxy-master.sock

Nov 07 03:47:14 debian11 systemd[1]: Starting HAProxy Load Balancer...
Nov 07 03:47:14 debian11 haproxy[86680]: Proxy apache_front started.
Nov 07 03:47:14 debian11 haproxy[86680]: Proxy apache_front started.
Nov 07 03:47:14 debian11 systemd[1]: Started HAProxy Load Balancer.
Nov 07 03:47:14 debian11 haproxy[86680]: [NOTICE] 310/034714 (86680) : New worker #1 (86682) forked
Nov 07 03:47:14 debian11 haproxy[86680]: Proxy apache_backend_servers started.
Nov 07 03:47:14 debian11 haproxy[86680]: Proxy apache_backend_servers started.

Verify HAProxy

At this point, HAProxy is configured and running. Now, it’s time to test the HAProxy.

Open your web browser and type the URL http://your-haproxy-ip. You will see that HAProxy is sending requests to backend servers one by one after each refresh.

<img alt="Server 1" data-ezsrc="https://kirelos.com/wp-content/uploads/2021/11/echo/p1.png618d66309284b.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="410" loading="lazy" src="data:image/svg xml,” width=”750″>

<img alt="Server 2" data-ezsrc="https://kirelos.com/wp-content/uploads/2021/11/echo/p2.png618d6630c636f.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="483" loading="lazy" src="data:image/svg xml,” width=”750″>

Conclusion

Congratulations! you have successfully installed HAProxy on Debian 11. You can now implement HAProxy in the production environment to increase your web application performance and availability.

<img alt="Hitesh Jethva" data-ezsrc="https://kirelos.com/wp-content/uploads/2021/11/echo/hitesh-80.jpg618d6630e7139.jpg" ezimgfmt="rs rscb5 src ng ngcb5" src="data:image/svg xml,”>

About Hitesh Jethva

Over 8 years of experience as a Linux system administrator. My skills include a depth knowledge of Redhat/Centos, Ubuntu Nginx and Apache, Mysql, Subversion, Linux, Ubuntu, web hosting, web server, Squid proxy, NFS, FTP, DNS, Samba, LDAP, OpenVPN, Haproxy, Amazon web services, WHMCS, OpenStack Cloud, Postfix Mail Server, Security etc.