Jump to content

How to Install HAProxy on Debian Linux


Linux Hint

Recommended Posts

Even when running Debian, you still need HAProxy to handle the traffic on your web server. For instance, you might have an Apache or Python3 web server running on your Debian system and you want to distribute the traffic across the servers to ensure that no server gets overloaded. Such a case requires you to install HAProxy and configure it to distribute the traffic based on the rules that you create. By the end of this post, you will know what it takes to install and configure HAProxy on Debian Linux. We are using Debian 11 for this case, but the same steps work on Debian 10.

Step-by-Step Guide on How to Install HAProxy on Debian Linux

HAProxy is compatible with different operating systems and distro. You can use it as a load balancer, a reverse proxy, or to secure your traffic between the client and the server. Whatever your use case is, the first step is to install HAProxy. Once installed, you can edit the configuration file to meet your needs. The following are the steps to install HAProxy on Debian Linux.

Step 1: Update and Upgrade the System

As a rule of the thumb, updating and upgrading the system is essential whenever you want to install packages. The main benefit is that you will get the latest version of the package that you wish to install. Quickly update your Debian as follows:

$ sudo apt update && sudo apt upgrade -y

word-image-413008-1.png

Step 2: Install HAProxy

HAProxy is available from the official Debian repository and on the other Linux systems. Therefore, you can quickly install it using the following APT command:

$ sudo apt install haproxy

word-image-413008-2.png

Give the required permissions and confirm that you want to continue with the installation by pressing “y” on your keyboard.

Once the installation runs successfully, verify that HAProxy has been installed by checking the available version. For this case, the following output shows that we have HAProxy version 2.2.9 installed:

word-image-413008-3.png

Step 3: Configure HAProxy

After installing HAProxy, you can access and edit its configuration file to define how you want to use it with your web servers. The configuration file is at “/etc/haproxy/haproxy.cfg”. Open it with a text editor and you will have a file like the one in the following.

The first section is the “global” section. It’s where you define what to log and where to send the log files. Leave the “global” and “defaults” section as they are.

word-image-413008-4.png

Our focus is on the “frontend” and the “backend” sections where you define what ports you want to listen to for connections and how to handle the traffic by specifying what servers to use based on the rules you define.

word-image-413008-5.png

For this example, we specify to bind all the connections that come through ports 80 and 81. Moreover, we create a rule that distributes the traffic depending on the incoming port. Our rule redirects all the connections via port 81 to use a different backend server as one way of distributing the load that would otherwise have been handled by the default backend server.

word-image-413008-6.png

Once you defined your frontend, ensure that you create the backend servers and give the IP addresses or the host names for the servers that you want to use for the traffic. Lastly, save and exit the HAProxy config file.

Step 4: Test HAProxy

Start by restarting HAProxy via “systemctl”.

$ sudo systemctl restart haproxy

word-image-413008-7.png

You must then check the validity of the configuration file to detect any error that it may contain. The following command should show “configuration file is valid” if no error is detected.

$ sudo haproxy -c -f /etc/haproxy/haproxy.cfg

word-image-413008-8.png

For the first test, we will try sending the traffic to our web servers using the “curl” command and see how it handles them. We channeled the traffic via ports 80 and 81 but we can’t get any response since our web servers are not running.

word-image-413008-9.png

To run our Python3 web servers, we run the following command and bind the first port:

word-image-413008-10.png

Running the “curl” command now shows that our web server is up and running, and our HAProxy is distributing the load to the default web server since we used port 81.

word-image-413008-11.png

Suppose we bind the other port as follows. If we try to run the same “curl” command via port 81, we confirm that the rule that we specified is working, and HAProxy now uses the second web server that we defined in the configuration file.

word-image-413008-12.png

With the given steps and explanations, you can now comfortably install and configure HAProxy on your Debian Linux.

Conclusion

HAProxy is available on Debian and can be installed from the default repository. Using the APT command, this tutorial demonstrated how to install and configure HAProxy on Debian Linux. Hopefully, the presented insights are clear and you managed to follow along and install HAProxy on your Debian Linux.

View the full article

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...