Ubuntu

How to install and configure HAproxy on Ubuntu 22.04 LTS

HAProxy is an open-source Linux load balancer and it is fast, secure, and easy to understand to use. The readers who are unfamiliar with the term LOAD BALANCER, for them, the Load Balancer distributes the traffic of your web applications among different web servers so your clients will experience high performance and speed. Moreover, if any server fails, HAProxy detects it and re-route the web traffic of that server to other servers so the clients will not experience any interruption.

In this guide, different installation methods of HAProxy on Ubuntu will be explored and also the method of configuring the HAProxy on Ubuntu.

How to install HAProxy on Ubuntu 22.04

We can install HAProxy on Ubuntu by two methods, one is from the default repository of Ubuntu and other is from its PPA repository. The PPA repository method is recommended as it will help you to install the latest version of HAProxy on Ubuntu.

Method 1: How to install the HAproxy on Ubuntu through repository

The installation package of the HAproxy comes with the installation of the Ubuntu, but might be possible it is of the older version, to confirm this we will first check the version of the HAproxy:

$ apt show haproxy

The version of 2.4.14 is available of the HAproxy on the Ubuntu repository, if you want to install it, you can use the command:

$ sudo apt install haproxy -y

We can check the version of the installed HAproxy by using the command:

$ haproxy -v

We can see that the version 2..4.14 which is available in the repository has been installed and to uninstall the installed package of HAProxy, run the mentioned-below command:

$ sudo apt purge haproxy -y

Method 2: How to install the HAproxy on Ubuntu 22.04 through PPA repository

Another method to install HAProxy on Ubuntu is by adding its PPA repository, for this we will add the PPA repository:

$ sudo add-apt-repository ppa:vbernat/haproxy-2.4 -y

Now we will update the packages to update the list of repositories of Ubuntu:

$ sudo apt update

Finally we will install the package of HAProxy from its PPA repository:

$ sudo apt install haproxy=2.4.\* -y

To check the installed HAProxy version, we will run the command:

$ haproxy -v

How to configure the HAProxy on Ubuntu 22.04

As we have already installed the HAProxy on Ubuntu 22.04, we will configure and make it ready to use. For the configuration of the HAProxy, we will open its configuration file by using the nano editor:

$ sudo nano /etc/haproxy/haproxy.cfg

Now at the end of the file, we will end the ip address of our localhost and the port with the bind keyword, so that it can get traffic on the following mentioned port and IP address:

defaults
  mode http
  timeout client 10s
  timeout connect 5s
  timeout server 10s
  timeout http-request 10s

frontend myfrontend
  bind 10.0.2.15:80

When these changes are appended, we will restart the service of HAProxy using the systemctl command:

$ sudo systemctl restart haproxy

Now we have configured so according to it, it will listen the requests on port 80 and to check this out, we will make a request using the curl command:

$ curl 10.0.2.15:80

The output is showing that there is no server available to correspond the requests because we have not configured any server for HAProxy. Now we will open another window of terminal and run the below-mentioned command to initialize a small web-application based on Python programming at port 8000:

$ python3 -m http.server 8000 --bind 10.0.2.15

Now again open the configuration file of the HAProxy and add the below mentioned lines:

default_backend myservers

backend myservers
  server server1 10.0.2.15:8000

Restart the HAProxy service again to reload the new changes:

$ sudo systemctl restart haproxy

Now again make the curl request and now you will see a response from the server:

$ curl 10.0.2.15:80

Similarly, you can make more web servers and add them in the configuration files of HAProxy to get the response from them.

Conclusion

HAProxy is an open-source load balancer proxy that is used to run the different web applications with the high performance and improving the speed by distributing loads on different servers. In this write-up, HAProxy is being installed by two different methods, one from Ubuntu’s repository and other is through its PPA repository and also configuration method is discussed in detail.

About the author

Hammad Zahid

I'm an Engineering graduate and my passion for IT has brought me to Linux. Now here I'm learning and sharing my knowledge with the world.