Menjaga agar aplikasi web tetap cepat dan tersedia secara terus-menerus adalah tantangan utama.
Ketika lalu lintas pengguna melonjak, satu server saja sering kali tidak cukup untuk menangani beban, yang berisiko menyebabkan Downtime atau Lambatnya respon server.
HAProxy (High Availability Proxy) hadir sebagai solusi. Ini adalah open-source load balancer andalan yang digunakan oleh perusahaan besar seperti GitHub, Reddit, dan Stack Overflow.
HAProxy bertindak seperti “manajer lalu lintas”, mendistribusikan setiap permintaan pengguna ke beberapa server backend sehingga tidak ada satu server pun yang bekerja sendirian.
Service Local
Install Node Js dan NPM
sudo apt install nodejs -y && sudo apt install npm -y
Cek version Node Js
node -v
Output:
v18.19.1
Cek version NPM
npm -v
Output:
9.2.0
Install PM2
PM2 manager proses untuk aplikasi berbasis Node Js. Fungsi untuk menjaga aplikasi tetap menyala (online) 24/7, merestart otomatis saat crash, dan menyeimbangkan beban (load balancing).
sudo npm install -g pm2
Website Sederhana Framework Express Js
APP 1
- Title : APP 1
- H2 : Website APP 1
- Port: 8181
- Direktori : app-1
APP 2
- Title : APP 2
- H2 : Website APP 2
- Port : 8282
- Direktori : app-2
Buat direktori website app-1
mkdir app-1 cd app-1
Init app-1
npm init -y
Install Framework Express Js
npm install express
Buat file app-1.js
nano app-1.js
Masukan source code website sederhana dibawah.
const express = require('express');
const app = express();
const port = 8181;
app.get('/', (req, res) => {
res.send(`
<!DOCTYPE html>
<html>
<head>
<title>APP 1</title>
<style>
body {
margin: 0;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
font-family: Arial, sans-serif;
}
h1 {
color: white;
font-size: 4rem;
text-shadow: 2px 2px 4px rgba(0,0,0,0.2);
}
</style>
</head>
<body>
<h1>Website APP 1</h1>
</body>
</html>
`);
});
app.listen(port, () => {
console.log(`Server running at http://127.0.0.1:${port}`);
});
Jalankan app-1.js dengan pm2 agar berjalan di background.
sudo pm2 start app-1.js
Untuk APP 2 ulangi seperti langkah-langkah diatas.
Install HAProxy
Jalankan perintah dibawah untuk install package HAProxy.
sudo apt update -y && sudo apt upgrade -y sudo apt install haproxy -y
Aktikan dan jalankan service HAProxy
sudo systemctl enable haproxy sudo systemctl start haproxy
Check HAProxy Version
Setelah terinstall, check version HAProxy.
sudo haproxy -v
Secara default ubuntu 24.04 menggunakan version 2.8.16 LTS.
Output: HAProxy version 2.8.16-0ubuntu0.24.04.2 2026/04/15 - https://haproxy.org/
SSL CloudFlare
Buat direktori certs untuk tempat menyimpan origin certificate dan private key SSL CloudFlare.
sudo mkdir /etc/haproxy/certs
Pastikan domain yang akan digunakan HAProxy sudah dihubungkan dengan Cloudflare dan sudah membuat dns record (A) dan (CNAME) www, dengan Proxy status (Proxied).
Pastikan current encyrption mode: Full Strict. Masuk SSL/TLS -> Overview.
Untuk membuat Origin Certificate dan Private Key, ke menu SSL/TLS -> Origin Server -> +Create Certificate.
- Generate private key and CSR with Cloudflare: Private key type RSA (2048)
- Certificate validity: 1 Years
- Klik Create
Salin Origin Certificate cloudflare, masukan pada file DOMAIN-crt.pem .
nano DOMAIN-crt.pem
Salin Private Key cloudflare, masukan pada file DOMAIN-key.pem
nano DOMAIN-key.pem
Gabungkan isi dari file DOMAIN-crt.pem dengan DOMAIN-key.pem menjadi satu file misalnya; combined.pem .
cat DOMAIN-crt.pem DOMAIN-key.pem > combined.pem
Pindahkan file combined.pem ke folder certs.
sudo mv combined.pem /etc/haproxy/certs
Ubah hak akses combined.pem menjadi 600.
sudo chmod 600 /etc/haproxy/certs/combined.pem
Load Balancer HAProxy
Backup konfigurasi default HAProxy
sudo cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bak
Edit file konfigurasi HAProxy
sudo nano /etc/haproxy/haproxy.cfg
Ganti dengan script dibawah.
global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
stats socket /run/haproxy/admin.sock mode 660 level admin
stats timeout 30s
user haproxy
group haproxy
daemon
defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5000
timeout client 50000
timeout server 50000
errorfile 400 /etc/haproxy/errors/400.http
errorfile 403 /etc/haproxy/errors/403.http
errorfile 408 /etc/haproxy/errors/408.http
errorfile 500 /etc/haproxy/errors/500.http
errorfile 502 /etc/haproxy/errors/502.http
errorfile 503 /etc/haproxy/errors/503.http
errorfile 504 /etc/haproxy/errors/504.http
frontend haproxy-main
bind *:80
bind *:443 ssl crt /etc/haproxy/certs/combined.pem alpn h2,http/1.1 ssl-min-ver TLSv1.2
mode http
option httplog
http-request redirect scheme https unless { ssl_fc }
default_backend local_service
backend local_service
mode http
balance roundrobin
server app1 127.0.0.1:8181 check
server app2 127.0.0.1:8282 check
Cek konfigurasi HAProxy
sudo haproxy -f /etc/haproxy/haproxy.cfg -c
Output: Configuration file is valid
Restart service HAProxy untuk menjalankan ulang dan update konfigurasi.
sudo systemctl restart haproxy
HAProxy Statistic Report
Buka file konfigurasi HAProxy.
sudo nano /etc/haproxy/haproxy.cfg
Tambahkan script dibawah, bagian paling bawah.
listen stats bind *:8443 ssl crt /etc/haproxy/certs/combined.pem alpn h2,http/1.1 ssl-min-ver TLSv1.2 mode http stats enable stats hide-version stats refresh 30s stats realm Haproxy\ Statistics stats uri / stats auth admin:password123
Penjelasan parameter:
- bind *:9000 : HAProxy akan mendengarkan port 9000 untuk memonitor statistik.
- stats enable : Mengaktifkan halaman statistik.
- stats hide-version : Menyembunyikan informasi versi HAProxy dari halaman dasbor demi keamanan.
- stats refresh 30s : Halaman dasbor akan otomatis memuat ulang (refresh) setiap 30 detik.
- stats uri /haproxy-stats : URL untuk mengakses statistik (contoh: http://IP_Public:9000/haproxy-stats).
- stats auth admin:password123 : Autentikasi dasar menggunakan username admin dan password password123.
Validasi configuration HAProxy
sudo haproxy -f /etc/haproxy/haproxy.cfg -c
Restart service HAProxy
sudo systemctl restart haproxy
Akses https://DOMAIN:9000/haproxy-stats untuk melihat dashboard stats haproxy. Akan muncuk from login username dan password isikan dengan admin/password123.

Pengujian Load Balancing HAProxy
Aksess url https://DOMAIN di browser. Lakukan reload dengan menekan keyboard ctrl + r, pastikan berubah dari “Website APP 1” menjadi “Website APP 2“.

Kesimpulan
Kamu telah berhasil membangun infrastruktur web yang tangguh. Dengan Load Balancer HAProxy di Ubuntu 24.04, tidak hanya meningkatkan kapasitas penanganan lalu lintas, tetapi juga menciptakan sistem High Availability (HA).
Jika salah satu server backend mati (down), HAProxy secara otomatis akan mengalihkan semua pengguna ke server yang masih hidup, memastikan layanan tetap online tanpa gangguan .
Untuk kebutuhan production tingkat lanjut, dapat mulai mengeksplorasi fitur seperti HTTPS/SSL termination (mengamankan traffic), Least Connections (mengirim ke server dengan sibuk paling sedikit), atau bahkan menyiapkan Keepalived untuk membuat Load Balancer yang ganda (master-slave) agar HAProxy itu sendiri tidak menjadi single point of failure.

