How to Install HAProxy on Debian 11

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 "<H1>Welcome to the first Apache Server</H1>" | 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 "<H1>Welcome to the second Apache Server</H1>" | 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.

Server 1

Server 2

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.

Share this page:

1 Comment(s)