DEV Community

Cover image for Deploying HAProxy on Ubuntu 22.04
Sanskriti Harmukh for Vultr

Posted on with Aashish Chaurasiya • Originally published at docs.vultr.com

Deploying HAProxy on Ubuntu 22.04

HAProxy is an open-source proxy that load-balances TCP and HTTP traffic across backend servers, routing each request based on health checks and your chosen balancing algorithm. This guide deploys HAProxy on Ubuntu 22.04 in front of two Apache backend servers connected over a private network, configures round-robin balancing with health checks, and enables the live statistics dashboard. By the end, you'll have HAProxy distributing traffic evenly across two backends with real-time monitoring.

Topology: One HAProxy server (public domain haproxy.example.com, private IP 10.128.0.2) plus two backend servers (10.128.0.3, 10.128.0.4) on the same private network.


Install HAProxy

1. Update and install:

$ sudo apt update
$ sudo apt install haproxy -y
Enter fullscreen mode Exit fullscreen mode

2. (Optional) Pull a newer version via PPA if your distro's default is stale:

$ sudo add-apt-repository ppa:vbernat/haproxy-2.8 -y
Enter fullscreen mode Exit fullscreen mode

3. Enable and verify:

$ sudo systemctl enable haproxy
$ sudo systemctl status haproxy
Enter fullscreen mode Exit fullscreen mode
Active: active (running)
Enter fullscreen mode Exit fullscreen mode

Configure HAProxy

The config file /etc/haproxy/haproxy.cfg ships with global (runtime settings) and defaults (timeouts, mode) sections already populated.

1. Back it up:

$ sudo cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.backup
Enter fullscreen mode Exit fullscreen mode

2. Open it:

$ sudo nano /etc/haproxy/haproxy.cfg
Enter fullscreen mode Exit fullscreen mode

3. Append a frontend and backend:

frontend website-frontend
    bind *:80,*:443
    option httpchk GET /healthcheck
    default_backend servers

backend servers
    balance roundrobin
    server server-1 10.128.0.3:80 weight 1 check
    server server-2 10.128.0.4:80 weight 1 check
Enter fullscreen mode Exit fullscreen mode
  • bind — listen on HTTP (80) and HTTPS (443).
  • option httpchk — health-check path/method used against backends.
  • balance roundrobin — distributes requests evenly in turn. Other modes: leastconn, source.
  • server ... weight 1 check — registers a backend with a load weight and enables health checking; HAProxy stops forwarding to a server that fails checks.

4. Append a stats listener:

listen stats
    bind *:8404
    mode http
    stats enable
    stats uri /stats
    stats auth admin:your_password
    stats refresh 10s
Enter fullscreen mode Exit fullscreen mode

This exposes a live dashboard with connection counts, session/request rates, response times, and errors.

5. Restart HAProxy:

$ sudo systemctl restart haproxy
Enter fullscreen mode Exit fullscreen mode

6. Open the firewall:

$ sudo ufw allow 80/tcp
$ sudo ufw allow 8404/tcp
$ sudo ufw reload
Enter fullscreen mode Exit fullscreen mode

7. Confirm both backends are reachable over the private network:

$ ping 10.128.0.3
$ ping 10.128.0.4
Enter fullscreen mode Exit fullscreen mode

Configure the Backend Servers

Repeat on each backend:

1. Install Apache:

$ sudo apt update
$ sudo apt install apache2 -y
$ sudo systemctl enable apache2
Enter fullscreen mode Exit fullscreen mode

2. Replace the default page so you can tell backends apart:

$ cd /var/www/html/
$ sudo mv index.html index.BAK
$ sudo nano index.html
Enter fullscreen mode Exit fullscreen mode

Server 1:

<!DOCTYPE html>
<html>
  <head><title>Server 1</title></head>
  <body><h1>Hello!</h1><p>This content is served by Server 1.</p></body>
</html>
Enter fullscreen mode Exit fullscreen mode

Server 2: same, with Server 2 swapped in.

3. Fix ownership and restart:

$ sudo chown -R www-data:www-data /var/www/html/index.html
$ sudo systemctl restart apache2
Enter fullscreen mode Exit fullscreen mode

4. Identify the private network interface:

$ ip a
Enter fullscreen mode Exit fullscreen mode

Look for the interface carrying the 10.128.0.x-style address (varies by provider/setup) — note its name for the next step.

5. Allow port 80 on that interface:

$ sudo ufw allow in on YOUR_PRIVATE_IFACE to any port 80
$ sudo ufw reload
Enter fullscreen mode Exit fullscreen mode

Test the Load Balancer

1. Visit the HAProxy domain in a browser:

http://haproxy.example.com
Enter fullscreen mode Exit fullscreen mode

Refresh a few times — responses alternate between "Server 1" and "Server 2" thanks to round-robin.

2. Open the stats dashboard:

http://haproxy.example.com:8404/stats
Enter fullscreen mode Exit fullscreen mode

Log in with the stats auth credentials from the config. You'll see live frontend/backend metrics including connections, request rates, response times.


Next Steps

HAProxy is load-balancing traffic across two backends with health checks and live stats. From here you can:

  • Add more backend servers to the servers backend block to scale horizontally
  • Switch balance to leastconn for workloads with long-lived connections
  • Put TLS termination on HAProxy itself with the ssl crt bind option, or keep it behind a dedicated TLS proxy

For the full guide with additional tips, visit the original article on Vultr Docs.

Top comments (0)