HAProxy (High Avail­ab­il­ity Proxy) is a popular software that can be used as a reverse proxy and load balancer. It can be easily installed on Ubuntu 22.04 in just a few steps.

What is HAProxy?

HAProxy is a powerful open-source software that can be used as a load balancer or reverse proxy. It’s often used to dis­trib­ute incident data traffic to several servers and thus improve the avail­ab­il­ity and per­form­ance of web ap­plic­a­tions. HAProxy is a proven solution, es­pe­cially in highly scalable and fail-safe ar­chi­tec­tures.

Thanks to its high ef­fi­ciency, HAProxy can process thousands of requests per second without placing a heavy load on system resources. The software supports various load balancing methods such as round robin, least con­nec­tion and source IP hashing. It also offers functions such as SSL ter­min­a­tion, health checks and sticky sessions in order to optimally control data traffic. Another strength is the ability to forward traffic based on specific rules or header in­form­a­tion.

HAProxy is used in many large companies and cloud en­vir­on­ments. Con­fig­ur­a­tion is carried out via a simple but flexible con­fig­ur­a­tion file that allows detailed cus­tom­isa­tion to your needs.

Dedicated Server
Per­form­ance through in­nov­a­tion
  • En­ter­prise hardware
  • Con­fig­ur­able hardware equipment
  • ISO-certified data centres

How to install HAProxy on Ubuntu 22.04 step by step

Step 1: Update the system

Before you start the in­stall­a­tion, you should make sure that your Linux dis­tri­bu­tion is up to date. This will ensure that all packages are current and that potential security vul­ner­ab­il­it­ies have been elim­in­ated. To do this, open a terminal and execute the following commands:

sudo apt update && sudo apt upgrade -y
bash

This command sequence first updates the package list to determine the latest versions of the installed software. All existing packages are then updated to the latest available versions. The -y parameter ensures that all updates are auto­mat­ic­ally confirmed.

Step 2: Install HAProxy

After the system has been updated, you can install HAProxy with the following command:

sudo apt install haproxy -y
bash

This command downloads HAProxy from the official Ubuntu package sources and installs the ap­plic­a­tion. The in­stall­a­tion is usually quick as HAProxy is a light­weight program. Once the in­stall­a­tion is complete, you can verify that HAProxy has been suc­cess­fully installed by running the following command:

haproxy -v
bash

The output should show the installed version of HAProxy.

Image: Screenshot of the current HAProxy version in the terminal
After you have executed the command, the currently installed version of HAProxy will be displayed.

Step 3: Activate and start the HAProxy service

After in­stall­a­tion, you must ensure that the HAProxy service is running. First, start HAProxy as an admin with the following command:

sudo systemctl start haproxy
bash

Use this command to check whether the service has been started suc­cess­fully:

sudo systemctl status haproxy
bash

If HAProxy is running, the output should look something like this:

Image: Screenshot of the current HAProxy status in the terminal
You can see from the ‘active (running)’ status in the terminal output that HAProxy is working without any problems.

To ensure that HAProxy also starts auto­mat­ic­ally after a restart, activate the service with:

sudo systemctl enable haproxy
bash

Step 4: Configure HAProxy

HAProxy is con­figured via the con­fig­ur­a­tion file /etc/haproxy/haproxy.cfg. Before making any changes, it’s good practice to create a backup of the original file:

sudo cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.backup
bash
Note

By rep­lic­at­ing the original file, you ensure that you can return to a working state at any time. In this way, changes can be made at low risk.

To edit the original file, open it with a text editor of your choice, such as nano or Vim. In our example, we’ll use nano:

sudo nano /etc/haproxy/haproxy.cfg
bash

A simple load balancing con­fig­ur­a­tion could look like this:

frontend http_front
	bind *:80
	default_backend web_servers
backend web_servers
	balance roundrobin
	server web1 192.168.1.10:80 check
	server web2 192.168.1.11:80 check

In the example load balancer, the incoming HTTP traffic on port 80 is dis­trib­uted to two backend servers (‘web1’ and ‘web2’). The load is dis­trib­uted in a round-robin process so that requests are forwarded al­tern­ately to the servers.

Step 5: Restart and test HAProxy

After the con­fig­ur­a­tion change, HAProxy must be restarted for the changes to take effect. This is done with the following terminal command:

sudo systemctl restart haproxy
bash

If errors occur, you can check the HAProxy con­fig­ur­a­tion file for syntax errors using the command below:

haproxy -c -f /etc/haproxy/haproxy.cfg
bash

A correct con­fig­ur­a­tion is confirmed by the output Configuration file is valid. You can now test whether HAProxy works as desired by entering the public IP address or the domain name of your server in a browser.

GPU Servers
Power redefined with RTX PRO 6000 GPUs on dedicated hardware
  • New high-per­form­ance NVIDIA RTX PRO 6000 Blackwell GPUs available
  • Un­par­al­lel per­form­ance for complex AI and data tasks
  • Hosted in secure and reliable data centres
  • Flexible pricing based on your usage
Go to Main Menu