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:
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:
We can check the version of the installed HAproxy by using the command:
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:
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:
Now we will update the packages to update the list of repositories of Ubuntu:
Finally we will install the package of HAProxy from its PPA repository:
To check the installed HAProxy version, we will run the command:
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:
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:
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:
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:
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:
Now again open the configuration file of the HAProxy and add the below mentioned lines:
backend myservers
server server1 10.0.2.15:8000
Restart the HAProxy service again to reload the new changes:
Now again make the curl request and now you will see a response from the server:
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.