# How to install HAProxy on Ubuntu 22.04

HAProxy (High Availability 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](https://www.ionos.co.uk/digitalguide/server/know-how/what-is-open-source/) software that can be used as a [load balancer](https://www.ionos.co.uk/digitalguide/server/know-how/load-balancers-distributing-server-workloads/) or [reverse proxy](https://www.ionos.co.uk/digitalguide/server/know-how/what-is-a-reverse-proxy/). It’s often used to distribute **incident data traffic to several servers** and thus improve the availability and performance of web applications. HAProxy is a proven solution, especially in highly scalable and fail-safe architectures.

Thanks to its high efficiency, 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 connection and source IP hashing. It also offers functions such as SSL termination, 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 information.

HAProxy is used in many large companies and [cloud](https://www.ionos.co.uk/digitalguide/server/know-how/what-is-cloud-storage/) environments. Configuration is carried out via a simple but flexible configuration file that allows detailed customisation to your needs.

## How to install HAProxy on Ubuntu 22.04 step by step

### Step 1: Update the system

Before you start the installation, you should make sure that your [Linux distribution](https://www.ionos.co.uk/digitalguide/server/configuration/linux-distributions/) is up to date. This will ensure that all packages are current and that potential security vulnerabilities have been eliminated. To do this, open a terminal and execute the following commands:

```bash
sudo apt update && sudo apt upgrade -y
```

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 automatically confirmed.

### Step 2: Install HAProxy

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

```bash
sudo apt install haproxy -y
```

This command downloads HAProxy from the official Ubuntu package sources and installs the application. The installation is usually quick as HAProxy is a lightweight program. Once the installation is complete, you can verify that **HAProxy has been successfully installed** by running the following command:

```bash
haproxy -v
```

The output should show the installed version of HAProxy.

[![Image: Screenshot of the current HAProxy version in the terminal](https://www.ionos.co.uk/digitalguide/fileadmin/_processed_/5/a/csm_haproxy-version_ded5693290.webp "Screenshot of the current HAProxy version in the terminal")](https://www.ionos.co.uk/digitalguide/fileadmin/DigitalGuide/Screenshots_2024/haproxy-version.png) After you have executed the command, the currently installed version of HAProxy will be displayed. ### Step 3: Activate and start the HAProxy service

After installation, you must ensure that the HAProxy service is running. First, start HAProxy as an admin with the following command:

```bash
sudo systemctl start haproxy
```

Use this command to check whether the service has been started successfully:

```bash
sudo systemctl status haproxy
```

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

[![Image: Screenshot of the current HAProxy status in the terminal](https://www.ionos.co.uk/digitalguide/fileadmin/_processed_/6/0/csm_haproxy-status_292f97738a.webp "Screenshot of the current HAProxy status in the terminal")](https://www.ionos.co.uk/digitalguide/fileadmin/DigitalGuide/Screenshots_2024/haproxy-status.png) 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 automatically after a restart, activate the service with:

```bash
sudo systemctl enable haproxy
```

### Step 4: Configure HAProxy

HAProxy is configured via the configuration file `/etc/haproxy/haproxy.cfg`. Before making any changes, it’s good practice to create a backup of the original file:

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

Note By replicating 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](https://www.ionos.co.uk/digitalguide/server/tools/linux-editor-effective-code-editing-with-vim/). In our example, we’ll use nano:

```bash
sudo nano /etc/haproxy/haproxy.cfg
```

A **simple load balancing configuration** could look like this:

```none
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](https://www.ionos.co.uk/digitalguide/hosting/technical-matters/what-is-http/) traffic on port 80 is distributed to two [backend](https://www.ionos.co.uk/digitalguide/websites/website-creation/what-is-a-backend/) servers (‘web1’ and ‘web2’). The load is distributed in a round-robin process so that requests are forwarded alternately to the servers.

### Step 5: Restart and test HAProxy

After the configuration change, HAProxy must be restarted for the changes to take effect. This is done with the following terminal command:

```bash
sudo systemctl restart haproxy
```

If errors occur, you can check the HAProxy configuration file for syntax errors using the command below:

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

A correct configuration is confirmed by the output `Configuration file is valid`. You can now test whether HAProxy works as desired by entering the public [IP address](https://www.ionos.co.uk/digitalguide/server/know-how/what-is-an-ip-address/) or the domain name of your server in a [browser](https://www.ionos.co.uk/digitalguide/websites/web-development/what-is-a-browser/).


This is a markdown version of: [https://www.ionos.co.uk/digitalguide/server/configuration/install-haproxy-on-ubuntu-2204/](https://www.ionos.co.uk/digitalguide/server/configuration/install-haproxy-on-ubuntu-2204/) for AI/LLM consumption.