How to Install HAProxy on Ubuntu 20.04 / 22.04

How to Install HAProxy on Ubuntu 20.04 / 22.04. All in all, load balancing is a common tool which you apply to web applications horizontally across multiple hosts. At the same time users maintain a single point of access to services. Therefore we introduce the perfect tool for it. HAProxy as one of the most popular open source load balancing software.

Moreover, in enterprise IT infrastructure, load balancing is key in ensuring that applications work optimally without performance degradation. Hence, load balancing is the process of carefully distributing network traffic across multiple servers. This boosts the speed and performance of applications. Without a load balancer, a single node would easily get overwhelmed by enormous requests or traffic leading to degraded performance or ultimate downtime.  So what is HAProxy, let’s find out.  

What is HAProxy?

Written in C,  HA Proxy , short for High Availability Proxy, is an open source and high performance HTTP/TCP load balancer that improves websites/application performance by routing network traffic across multiple backend web servers. Used in high traffic services to ensure applications are running normally. Performance improvements include high throughput and minimized response time.

Key Features of HAProxy

HaProxy provides the following features which are essential in improving application performance.

  • SSL/TLS termination.
  • Layer 4 and 7 load balancing (TCP and HTTP respectively).
  • Support for HTTP, HTTP/2, FastCGI, and gRPC.
  • Detailed logging of events.
  • Content switching and inspection.
  • HTTP authentication.
  • URL rewriting.
  • Advanced health checking.
  • Multithreading.
  • Transparent proxying.

This article walks you through the installation and configuration of HA Proxy on Ubuntu  20.04 / Ubuntu 22.04.  For demonstration, we have the following setup.

How to Install HAProxy on Ubuntu 20.04 / 22.04

Lab setup

To demonstrate how HA Proxy works, we have three nodes. One node acts as the HA Proxy load balancer and the remaining two act as web servers.        HA Proxy          web1                 web2               

Without much ado, let’s dive in and get started.

Step 1: Configure the /etc/hosts file ( all the nodes )

To get off the ground, you need to modify the /etc/hosts file of every node with the IP addresses and hostnames of all the other nodes in the setup. Therefore, log into every node and access the /etc/hosts file.

					$ sudo nano  /etc/hosts

Update the file as follows

 haproxy web1 web2


Save the changes and exit.

Step 2: Install HA Proxy on Ubuntu 22.04.

Next,  log in to your HA proxy node and update the local package index as follows.

					$ sudo apt update

The Haproxy package is available on the official Ubuntu repository and you confirm this using the following command.

					$ sudo apt show haproxy

From the following output see that HaProxy 2.4.18 is available from the repository. However, this is not the latest release of HaProxy at the time of writing this guide.

Currently, the latest release of HAproxy is HaProxy 2.7.  We need to manually add its repository before installing it as shown.

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

Once the repository is added, update the package lists  in order for the system to be notified of the  new repostory.

					$ sudo apt update

Next, install  the latest version of HAProxy as shown.

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

Once the installation is complete, confirm that HAproxy is running as shown.

					$ sudo systemctl status haproxy

From the following output, see that HAproxy is up and running.

In addition to that, confirm the version of HAproxy installed as follows:

					$ haproxy -v

The following output confirms that indeed we have installed the latest version of HAproxy, which is currently, s 2.7.4.

Step 3: Configure HAProxy Load Balancer

So far, we have successfully installed HAProxy on our load balancer node.  Some additional configuration is required in order to define the frontend and backend servers and other settings.

So, back up the HA Proxy configuration file before making any modifications, and make a backup copy of the configuration file.

					sudo cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bk

Next,  open the configuration file. 

					$ sudo nano /etc/haproxy/haproxy.cfg

The HAProxy configuration file is made up of four essential sections namely: globaldefaults, frontend, and backend. These appear in the following format.

    # global settings here

    # defaults here

    # a frontend that accepts requests from clients

    # servers that fulfill the requests

Config files

  • Global: This is the topmost section in the configuration file. The settings in this section define process-wide security and performance tunings.
  • defaults: As the name infers, this section contains settings that should work seamlessly without further customization. These settings apply to all of the frontend and backend sections in the configuration file.
  • frontend: This section defines the IP address and ports that a client connects to when the HAProxy serves as a reverse proxy in front of backend servers.
  • backend: This section specifies a group of servers that are assigned to handle requests by the HAProxy load balancer.

Now back to our configuration file, add the following lines of code to specify frontend and backend nodes.

					frontend load_balancer
   stats uri /haproxy?stats
   default_backend web-servers

backend web-servers
    balance roundrobin
    server web1
    server web2


In the above block of code, we have defined the HA Proxy server node which acts as a reverse proxy and sits in front of the two web servers. When a request is made to the HA Proxy load balancer on port 80, it is routed to either of the web servers in a round robin format.This balances the load across the web servers.

Next, add the following ‘listen’ section to enable viewing HA Proxy statistics from a web browser on port 8080. The ‘stats auth‘ parameter specifies the username and password for the login user for viewing statistics on the browser.

					listen stats
   bind *:8080
   stats enable
   stats uri /
   stats refresh 5s
   stats realm Haproxy\ Statistics
   stats auth admin:P@ss123     #Login User and Password for the monitoring

Save the changes made and exit the configuration file. To apply the changes, be sure to restart the HA Proxy service.

					$ sudo systemctl restart haproxy

Great! The configuration of the HA Proxy load balancer is complete. Let’s now go ahead and configure the web servers that will be used for testing the load balancing algorithm.

Step 4: Configure the Web Servers

Now log into each of the web servers and install a web server. For this exercise, we install Apache HTTP web server which is a free and open source web server used by millions of websites.

To install Apache, run the following commands.

					$ sudo apt update
$ sudo apt install apache2 -y

Feel free to enable Apache to start on boot.

					$ sudo systemctl enable apache2

Then confirm that the web service is running as expected.

					$ sudo systemctl status apache2

To demonstrate the round robin load balancing we need to go an extra step and create different sample web pages for the web servers.

For Web server 1

Switch to root user:

					$ sudo su -

And edit the sample welcome page as shown:

					# echo "<p>Greetings! This is web server1: </p>" &gt; /var/www/html/index.html

For Web server 2

Switch to root user:

					$ sudo su -

And edit the sample welcome page as shown:

					# echo "<p>Greetings! This is web server2: </p>" &gt; /var/www/html/index.html


Step 5: Test HA Proxy load balancing

Everything is now configured including the HA Proxy and the two web servers. Let us now put the Ha Proxy load balancer to test and check it distributes traffic evenly across the two web servers.

To test if the configuration is working, launch your browser and browse the IP address of the HA Proxy server. When you first browse the IP address, the webpage of the first web server is displayed as shown:

Now refresh your browser. This time around,  the load balancer channels the web traffic to the second web server which responds with its unique landing page as shown.

This is proof that our HA Proxy load balancer is working as expected by distributing the workload across both web servers. For high traffic applications, there are usually multiple backend web servers configured to handle requests. This ensures that no single node is overwhelmed leading to degraded performance and possible downtime.

Additionally, you can send repetitive HTTP requests with a delay of 1 second to the load balancer using the following while loop.

					while true; do curl; sleep 1; done

This yields the following output that shows alternating responses from the two web servers.

To view the HA Proxy statistics, browse the HA Proxy’s server address on port 8080.


You get a pop up sign in page. Provide your login credentials and click ‘Sign In‘.

Once you are logged in, you are presented with the following HA Proxy page detailing the performance statistics of all the nodes.

Thank you for reading How to Install HAProxy on Ubuntu 20.04 / 22.04. We shall conclude this article now. 

How to Install HAProxy on Ubuntu 20.04 / 22.04 Conclusion

In this guide, we have demonstrated how to Install HAProxy on Ubuntu 20.04 / 22.04. In high traffic websites, HAProxy is a common component in an effort to ensure that all services run smoothly without being overloaded by network traffic or requests.

Avatar for James Kiarie
James Kiarie

Hello everyone! My name is James, a certified Linux Administrator, and a tech enthusiast with over 5 years of experience in penning down high-quality guides on Linux and Cloud technologies. Outside work hours I enjoy working out, swimming, listening to music, and reading fiction novels.

4.5 2 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x