HAProxy load balancer on Ubuntu 24.04
You build a load balancer by installing HAProxy on a small Ubuntu 24.04 server, defining a frontend that listens on ports 80 and 443 and a backend that lists your two app servers with health checks, then adding a Let's Encrypt certificate as one combined pem file. Check the config with haproxy -c and reload with systemctl reload haproxy, which swaps the config without dropping connections.
Networking and security guide, updated 2026-12-03
What does the setup look like?
Three servers in one city, joined by a WireGuard mesh:
| Role | Public IPv4 | Tunnel address | Listens on |
|---|---|---|---|
| lb (HAProxy) | 203.0.113.10 | 10.20.0.10 | 80, 443 public |
| app1 | 198.51.100.20 | 10.20.0.11 | 8080 on wg0 only |
| app2 | 198.51.100.21 | 10.20.0.12 | 8080 on wg0 only |
The site is www.example.com and its DNS A record points at the balancer. Each app server runs the same application on port 8080 and answers GET /health with status 200. Commands run as root on the balancer unless noted. Concepts behind each choice are in Load balancing explained.
How do you install HAProxy?
apt update
apt install -y haproxy
haproxy -v
systemctl status haproxy --no-pagerUbuntu 24.04 ships the 2.8 long term support branch, which has everything this guide uses. The package creates /etc/haproxy/haproxy.cfg with a global and defaults section, runs HAProxy in a chroot under /var/lib/haproxy, and adds an rsyslog rule that writes logs to /var/log/haproxy.log. Keep the existing global and defaults blocks and add yours below them.
Open the public ports: ufw allow 80,443/tcp. On the app servers, allow the app port only on the tunnel: ufw allow in on wg0 to any port 8080 proto tcp.
How do you write the frontend and backend?
Start with plain HTTP so the certificate can be issued. Append to /etc/haproxy/haproxy.cfg:
frontend web
bind :80
bind :::80
option forwardfor
acl acme path_beg /.well-known/acme-challenge/
use_backend acme if acme
default_backend app
backend app
balance leastconn
option httpchk
http-check send meth GET uri /health ver HTTP/1.1 hdr Host www.example.com
http-check expect status 200
default-server inter 3s fall 3 rise 2
server app1 10.20.0.11:8080 check
server app2 10.20.0.12:8080 check
backend acme
server certbot 127.0.0.1:8888option forwardfor adds X-Forwarded-For with the visitor address. default-server applies the check timing to every server line: a probe every 3 seconds, down after 3 failures, up after 2 successes. The acme backend forwards certificate validation requests to certbot, which you will run on port 8888.
How do you check and reload the config safely?
Always validate first. A reload with a broken file is refused, but a restart would stop the service:
haproxy -c -f /etc/haproxy/haproxy.cfg
systemctl reload haproxyThe check prints Configuration file is valid on success, or the line number and reason on failure. The packaged service runs HAProxy in master-worker mode: a reload starts new workers with the new config, and old workers finish their open connections before exiting. Visitors do not notice.
A useful habit is to chain the two so a typo never reaches the reload: haproxy -c -f /etc/haproxy/haproxy.cfg && systemctl reload haproxy. Test the result from your laptop with curl -I http://www.example.com and check that both backends serve requests by stopping the app on one of them for a moment.
How do you add TLS with a Let's Encrypt certificate?
Install certbot and request the certificate through the acme backend:
apt install -y certbot
certbot certonly --standalone --http-01-port 8888 -d www.example.com -d example.comLet's Encrypt calls http://www.example.com/.well-known/acme-challenge/... on port 80; HAProxy forwards it to certbot on 8888. Certbot writes fullchain.pem and privkey.pem under /etc/letsencrypt/live/www.example.com/.
HAProxy wants one file holding the chain followed by the key. Create it with a deploy hook, which also runs after every renewal:
mkdir -p /etc/haproxy/certs
cat > /etc/letsencrypt/renewal-hooks/deploy/haproxy.sh <<'EOF'
#!/bin/sh
d=/etc/letsencrypt/live/www.example.com
cat "$d/fullchain.pem" "$d/privkey.pem" > /etc/haproxy/certs/www.example.com.pem
chmod 600 /etc/haproxy/certs/www.example.com.pem
systemctl reload haproxy
EOF
chmod +x /etc/letsencrypt/renewal-hooks/deploy/haproxy.sh
RENEWED_LINEAGE=/etc/letsencrypt/live/www.example.com /etc/letsencrypt/renewal-hooks/deploy/haproxy.shThe last line runs the hook once by hand to create the first file.
How do you switch the frontend to HTTPS?
Now that the pem exists, extend the frontend:
frontend web
bind :80
bind :::80
bind :443 ssl crt /etc/haproxy/certs/ alpn h2,http/1.1
bind :::443 ssl crt /etc/haproxy/certs/ alpn h2,http/1.1
option forwardfor
acl acme path_beg /.well-known/acme-challenge/
http-request redirect scheme https code 301 unless { ssl_fc } || acme
http-request set-header X-Forwarded-Proto https if { ssl_fc }
use_backend acme if acme
default_backend appPointing crt at a directory loads every pem in it and picks the right one by the name the client asks for, so more sites later need no config change. Plain HTTP is redirected to HTTPS, except validation requests, which must stay reachable on port 80. Validate, reload, then test with curl -I https://www.example.com and certbot renew --dry-run. The app should read X-Forwarded-Proto to build correct links. Background on chains and renewals is in TLS certificates explained.
How do you enable a stats page without exposing it?
HAProxy has a built-in status page showing each backend, its health, sessions and errors. Bind it to localhost only:
frontend stats
bind 127.0.0.1:8404
stats enable
stats uri /stats
stats refresh 10sReach it through an SSH tunnel from your computer:
ssh -L 8404:127.0.0.1:8404 root@203.0.113.10Then open http://localhost:8404/stats in your browser. Green rows are healthy servers; a red row with L7STS means the health check got a wrong status code, L4TOUT means the connection timed out, usually a firewall or a stopped tunnel. Because the page is not public, you do not need a password on it, though stats auth adds one if you prefer.
How do you read the logs?
The default global section sends logs to /dev/log, and the packaged rsyslog rule writes them to /var/log/haproxy.log. Watch it live:
tail -f /var/log/haproxy.logWith option httplog (set in the packaged defaults), each line shows the client address, the frontend, the backend and server chosen, timers, the status code and the termination state. Timers such as 0/0/1/24/25 show queue, connect and response times in milliseconds, which tells you whether slowness is in the balancer or the app. Server state changes also appear: Server app/app1 is DOWN, reason: Layer7 wrong status.
If the log file stays empty, check that rsyslog is installed and running with systemctl status rsyslog, then restart HAProxy once. Logs rotate daily through logrotate.
What is the Nginx alternative?
If you already know Nginx, it can do the same job for HTTP. In /etc/nginx/conf.d/lb.conf:
upstream app {
least_conn;
server 10.20.0.11:8080 max_fails=3 fail_timeout=10s;
server 10.20.0.12:8080 max_fails=3 fail_timeout=10s;
}
server {
listen 443 ssl;
http2 on;
server_name www.example.com;
ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;
location / {
proxy_pass http://app;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}Nginx reads the separate certificate and key files directly, so no combined pem is needed, and certbot's nginx plugin can manage everything. The main difference: open source Nginx uses passive health checks, so a dead backend is noticed only when real requests fail. HAProxy probes actively and has the stats page. Basic Nginx TLS setup is in Nginx with HTTPS.
Questions about this guide
Do the backends need public ports?
No. In this layout they listen on the tunnel address only, and their firewall allows the app port on wg0. Only SSH stays open on their public side.
Why does HAProxy fail to start after I add the 443 bind?
The pem file or directory in crt is missing or empty, or the file holds the key before the chain in a broken format. Run haproxy -c -f /etc/haproxy/haproxy.cfg to see the exact message.
How do I take a backend out for maintenance?
Use the runtime API: add stats socket /run/haproxy/admin.sock mode 660 level admin to global (the package already includes it), then echo "set server app/app1 state drain" | socat stdio /run/haproxy/admin.sock. Set it back to ready afterwards.
Does this work on Debian 13 or Rocky Linux?
Yes. Debian uses the same package and paths. On Rocky and Alma, install with dnf install haproxy, open ports with firewalld, and allow HAProxy to connect to backends with setsebool -P haproxy_connect_any 1 when SELinux blocks it.
Can HAProxy balance non-HTTP services?
Yes. Use mode tcp in a separate frontend and backend for TCP services such as game servers or database read replicas. Health checks then use check with a TCP connect.
How much traffic does the balancer use?
All responses pass through it, so its outgoing traffic is roughly what your site sends to visitors. Check the allowance per plan and region on the pricing page.
More in Networking and security
- IPv4 and IPv6 addresses
- Add IPv6 addresses
- Static network setup
- Reverse DNS (PTR)
- Networking terms
- How the VPS network works
- How DNS works
- Private networking
- Private link with WireGuard
- Failover and floating IPs
- Firewall rules explained
- Load balancing explained
- TLS certificates explained
- SSH hardening
- Firewall setup
- Point a domain