How To Install HAProxy on Debian 11

Install HAProxy on Debian 11

In this tutorial, we will show you how to install HAProxy on Debian 11. For those of you who didn’t know, HAProxy is a free, open-source, and reliable solution for high availability and load balancing server. It is particularly suited for very high-traffic websites and powers quite a number of the world’s most visited ones. HAProxy is written in C and it provides a high availability load balancer for TCP and HTTP-based applications that run on multiple servers. It is popular due to its efficiency, reliability, and low memory and CPU footprint.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you the step-by-step installation of the HAProxy Load Balancing Server on a Debian 11 (Bullseye).

Prerequisites

  • A server running one of the following operating systems: Debian 11 (Bullseye).
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • SSH access to the server (or just open Terminal if you’re on a desktop).
  • A non-root sudo useror access to the root user. We recommend acting as a non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.

Install HAProxy on Debian 11 Bullseye

Step 1. Before we install any software, it’s important to make sure your system is up to date by running the following apt commands in the terminal:

sudo apt update
sudo apt upgrade
sudo apt install apt-transport-https ca-certificates curl gnupg2 software-properties-common

Step 2. Installing Apache to Setup Backend Web Servers.

Now we set up two backends Apache servers:

  • On the first backend server, install the Apache package using the following command below:
sudo apt install apache2

Next, create a sample Apache index page:

echo "<H1>Welcome to the first Apache Server</H1>" | tee /var/www/html/index.html
  • On the second backend server, install the Apache package using the following command below:
sudo apt install apache2

Then, create a sample Apache index page:

echo "<H1>Welcome to the second Apache Server</H1>" | tee /var/www/html/index.html

Step 3. Installing HAProxy on Debian 10.

By default, the HAProxy package is not available on Debian 11 base repository. Now we add the HAProxy repository to your system using the following command:

curl https://haproxy.debian.net/bernat.debian.org.gpg 
      | gpg --dearmor > /usr/share/keyrings/haproxy.debian.net.gpg

echo deb "[signed-by=/usr/share/keyrings/haproxy.debian.net.gpg]" 
      http://haproxy.debian.net bullseye-backports-2.4 main 
      > /etc/apt/sources.list.d/haproxy.list

Then, use the following commands to install HAProxy:

suso apt update
sudo apt install haproxy=2.4.*

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

sudocsystemctl start haproxy
sudo systemctl enable haproxy

Step 4. Configure HAProxy.

HAProxy’s configuration file is stored in the /etc/haproxy/haproxy.cfg file. This file is created automatically during installation. 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.77.20:80 check
        server             backend02 192.168.77.21:80 check

Save and close the file, then restart the HAProxy service so that the new configuration can take effect:

sudo systemctl restart haproxy

Step 5. Verify HAProxy.

Once successfully complete the configuration, open your web browser and type the URL http://your-haproxy-ip-address. You will see that HAProxy is sending requests to backend servers one by one after each refresh:

Install HAProxy on Debian 11 Bullseye

Congratulations! You have successfully installed HAProxy. Thanks for using this tutorial for installing the latest version of HAProxy Load Balancing Server on Debian 11 Bullseye. For additional help or useful information, we recommend you check the official HAProxy website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!