WIUNIX ACADEMY
HAProxy with Let’s Encrypt and validated certificate renewal
Page 5 of 5. Review the earlier steps and prerequisites before continuing.
YOUR SYSTEM. YOUR STEPS.
Where are you working?
Choose your OS and version. The prerequisites, commands and troubleshooting follow your environment.
USE YOUR OWN VALUES
Configure this tutorial
Set the example values for your environment; related explanations and copy panels update together. Never enter passwords, private keys or tokens. Values stay in this tab and are not sent to the server or share links. Check actual DNS, certificates and network policy before running commands.
Different IP addresses, domains or ports? Open the settings below, change the values and select “Apply to tutorial”, then copy the updated commands.
Set values for my environment7 editable values
LET’S BUILD
Restore the last trusted PEM deliberately
Stop the dedicated timer and wait for an already running renewal service to finish; stopping a timer does not cancel its service. Recovery takes the same deployment lock. Use the previous PEM only when its hostname, key, trust and remaining validity pass and it has not been revoked; this offline check does not prove current CA revocation status.
Preserve the failed PEM in a uniquely named private file, validate a same-filesystem candidate against the active config, then replace and reload. A failed reload remains a failure requiring public TLS/service diagnosis. Do not run this in the HTTP-only stage or roll back a healthy deployment without reason. Keep the timer paused until the cause is fixed and the actual served certificate is checked.
Bash on dedicated edge VM; external checks noted in text
set -eu
sudo systemctl stop academy-certbot.timer
if sudo systemctl is-active --quiet academy-certbot.service; then
echo 'Renewal is running: wait for it to finish, then retry this recovery' >&2; exit 1
fi
sudo python3 - <<'PY'
from pathlib import Path
import os,fcntl,tempfile,subprocess
root=Path('/srv/academy-haproxy-tls');pem=root/'certs/app.example.com.pem';previous=pem.with_suffix('.previous.pem')
os.umask(0o077)
def run(*args):return subprocess.run(args,check=True,capture_output=True).stdout
with (root/'deploy.lock').open('a') as lock:
fcntl.flock(lock,fcntl.LOCK_EX)
for p in (pem,previous,root/'haproxy.cfg'):
if not p.is_file() or p.is_symlink():raise SystemExit('Expected a regular private file')
run('openssl','x509','-in',str(previous),'-checkend','86400','-noout')
run('openssl','verify','-CAfile','/etc/ssl/cert.pem','-untrusted',str(previous),'-purpose','sslserver','-verify_hostname','app.example.com',str(previous))
if run('openssl','x509','-in',str(previous),'-pubkey','-noout')!=run('openssl','pkey','-in',str(previous),'-pubout'):
raise SystemExit('Previous PEM key mismatch')
config=(root/'haproxy.cfg').read_text(encoding='utf-8')
binding='crt '+str(pem)
if config.count(binding)!=1:raise SystemExit('Unexpected certificate binding')
old=pem.read_bytes();replacement=previous.read_bytes()
fd,name=tempfile.mkstemp(prefix='.rollback-',suffix='.pem',dir=pem.parent)
candidate=Path(name)
try:
with os.fdopen(fd,'wb') as f:f.write(replacement);f.flush();os.fsync(f.fileno())
with tempfile.TemporaryDirectory(prefix='rollback-check-',dir=root) as work:
check=Path(work)/'haproxy.cfg'
check.write_text(config.replace(binding,'crt '+str(candidate)),encoding='utf-8')
run('/usr/bin/haproxy','-c','-f',str(check))
fd,name=tempfile.mkstemp(prefix='.failed-',suffix='.pem',dir=pem.parent)
with os.fdopen(fd,'wb') as f:f.write(old);f.flush();os.fsync(f.fileno())
os.replace(candidate,pem)
run('/usr/bin/haproxy','-c','-f',str(root/'haproxy.cfg'))
run('/usr/bin/systemctl','reload','academy-haproxy.service')
finally:candidate.unlink(missing_ok=True)
print('Selected previous PEM installed; verify the actually served certificate')
PY
curl --noproxy '*' --fail --show-error --max-time 15 'https://app.example.com/'
The chosen still-trusted previous PEM validates and is served after reload; timer stays paused until the root cause is repaired.
LET’S BUILD
Recover a failed first TLS activation
Only if the initial TLS reload failed, restore the saved HTTP bootstrap. This intentionally removes HTTPS and preserves HTTP-01 during repair; it is not a way to serve the application securely. After repair repeat the candidate validation/activation, trusted TLS probe and hook probe, then resume the dedicated timer. To end the disposable lab, stop/disable its four named units through normal service management and archive protected files before removing only this root; never remove distro configs or public DNS records used by others.
Bash on dedicated edge VM; external checks noted in text
set -eu
sudo /usr/bin/haproxy -c -f '/srv/academy-haproxy-tls/haproxy.bootstrap.cfg'
sudo install -m 0644 '/srv/academy-haproxy-tls/haproxy.bootstrap.cfg' '/srv/academy-haproxy-tls/haproxy.cfg.next'
sudo mv '/srv/academy-haproxy-tls/haproxy.cfg.next' '/srv/academy-haproxy-tls/haproxy.cfg'
sudo systemctl reload academy-haproxy.service
curl --noproxy '*' --fail --resolve 'app.example.com:80:192.0.2.10' 'http://app.example.com/.well-known/acme-challenge/academy-probe'
Only the bootstrap is loaded and the probe still returns its exact public token body.
LET’S BUILD
Stop only the dedicated disposable exercise
After preserving protected account/lineage, configuration and last trusted PEM backups, stop the four named lab units. This intentionally removes the lab web entry and renewal schedule. Keep evidence for diagnosis; do not purge distribution configuration, remove shared DNS or delete another service’s certificate. Before a later restart, review current expiry, DNS, backend readiness and retained configuration.
Dedicated disposable edge VM
sudo systemctl disable --now academy-certbot.timer
if sudo systemctl is-active --quiet academy-certbot.service; then echo 'Wait for this renewal run to finish before stopping the web services'; exit 1; fi
sudo systemctl disable --now academy-haproxy.service academy-acme-webroot.service
sudo systemctl is-active academy-haproxy.service academy-acme-webroot.service
Only the dedicated web services and renewal timer stop; final is-active is expected to return nonzero and private files remain.
What this guide actually tested
All OS paths are documentation-reviewed, not installations. The isolated fixture evidence is recorded after execution below; public ACME issuance, account registration, DNS changes, renewal with a real CA, systemd boot/reboot, HA and backup restore are not claimed. Reader personalization changes text/copy panels only and does not test your network.
Design resilience after proving the single edge
Keep readiness semantics, renewal alerts and private key backups under a named operator. For redundant edges, design challenge distribution, certificate custody, state and failover separately; copying this VM twice does not create HA. Continue with the existing HAProxy/Keepalived guide only after understanding its network and split-brain limits.
GO TO THE SOURCE
Official references
Each environment states how it was reviewed. A documentation review does not claim a hands-on test of every version.
- HAProxy community configuration manual
- Certbot webroot, renewal and deploy hooks
- Certbot supported installation methods
- HTTP-01 challenge constraints
- Staging environment
- Current rate limits
- IPv6 validation behavior
- Ubuntu release support
- Debian supported releases
- EPEL installation and channels
- EPEL Certbot packages
- Arch HAProxy executable paths
- Arch Certbot scheduler paths
- SELinux HAProxy policy source
- Manjaro package maintenance
KEEP LEARNING