ACME & Let's Encrypt
Public certificates used to be bought, emailed and installed by hand once a year, and they expired on the one weekend nobody was watching. ACME (Automatic Certificate Management Environment, RFC 8555) automated the whole exchange: a client on your server proves to a CA that it controls a domain, gets a certificate, and renews it long before expiry, all without a human. Let's Encrypt made it free and universal; ZeroSSL, Google Trust Services, Buypass and commercial CAs speak ACME too. The sandbox has no internet, so ACME runs as sample sessions; the challenge mechanics and the renewal logic run here.
How a CA checks that you control a domain
The CA gives the client a token; the client must publish a value derived from it (the token plus its account key's thumbprint, the "key authorization") where only the domain's controller could put it:
| Challenge | Proof | Needs | Good for |
|---|---|---|---|
| HTTP-01 | file at http://<domain>/.well-known/acme-challenge/<token> |
port 80 reachable from the internet | single servers, proxies (Caddy and certbot's default) |
| DNS-01 | TXT record _acme-challenge.<domain> |
API access to your DNS provider | wildcards (*.example.com), servers not reachable from the internet, many servers |
| TLS-ALPN-01 | a special certificate on port 443 | port 443, client support | proxies that own port 443 (Caddy, Traefik) |
HTTP-01 against a webroot is easy to rehearse locally. Here Nginx serves the challenge directory, a client writes the token file, and a stand-in for the CA fetches it the way Let's Encrypt's validation servers would (plain HTTP, port 80 in reality):
mkdir -p webroot/.well-known/acme-challenge
cat > nginx.conf <<'EOF'
pid nginx.pid;
error_log stderr;
events {}
http {
access_log off;
server {
listen 127.0.0.1:8080;
server_name shop.example.test;
location /.well-known/acme-challenge/ {
root /home/learner/webroot; # certbot --webroot -w /home/learner/webroot
default_type text/plain;
}
location / { return 301 https://$host$request_uri; }
}
}
EOF
nginx -p "$PWD" -c nginx.conf -e stderr
for i in $(seq 50); do curl -s 127.0.0.1:8080/ > /dev/null && break; sleep 0.1; done
TOKEN=evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ-PCt92wr-oA # chosen by the CA
THUMB=nP1qzpXGymHBrUEepNY9HCsQk7K8KhOypzEY9rB8eDM # the client's account key thumbprint
echo "$TOKEN.$THUMB" > "webroot/.well-known/acme-challenge/$TOKEN" # what the ACME client writes
echo "--- the CA's validation request:"
curl -s -w ' (HTTP %{http_code})\n' --resolve shop.example.test:8080:127.0.0.1 \
"http://shop.example.test:8080/.well-known/acme-challenge/$TOKEN"
echo "--- every other path still redirects to HTTPS:"
curl -s -o /dev/null -w '%{http_code} -> %{redirect_url}\n' --resolve shop.example.test:8080:127.0.0.1 http://shop.example.test:8080/cart
nginx -p "$PWD" -c nginx.conf -e stderr -s quit 2>/dev/null--- the CA's validation request:
evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ-PCt92wr-oA.nP1qzpXGymHBrUEepNY9HCsQk7K8KhOypzEY9rB8eDM
(HTTP 200)
--- every other path still redirects to HTTPS:
301 -> https://shop.example.test/cart
The detail that matters in production: the challenge location must be reachable over plain HTTP, before your HTTP→HTTPS redirect, and through every firewall, CDN and load balancer in front of the server. (Let's Encrypt does follow redirects to HTTPS, but a redirect to a broken HTTPS site fails the challenge.) Most failed issuances are a blocked port 80, DNS pointing elsewhere, or an IPv6 AAAA record for a server that doesn't answer on IPv6: the CA prefers IPv6 when it exists.
certbot
certbot is the EFF's ACME client and the most common on Nginx/Apache servers (sample session; Ubuntu, certbot from the snap or apt):
$ sudo certbot certonly --webroot -w /var/www/acme -d shop.example.com -d www.shop.example.com \
--email ops@example.com --agree-tos --no-eff-email
Requesting a certificate for shop.example.com and www.shop.example.com
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/shop.example.com/fullchain.pem
Key is saved at: /etc/letsencrypt/live/shop.example.com/privkey.pem
This certificate expires on 2026-04-15.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.
$ sudo ls /etc/letsencrypt/live/shop.example.com/
README cert.pem chain.pem fullchain.pem privkey.pem
$ systemctl list-timers 'snap.certbot*' 'certbot*' | head -2
NEXT LEFT LAST PASSED UNIT ACTIVATES
Thu 2026-01-15 21:43:00 UTC 11h Thu 2026-01-15 09:13:02 UTC 47min ago certbot.timer certbot.service
$ sudo certbot renew --dry-run
...
Congratulations, all simulated renewals succeeded:
/etc/letsencrypt/live/shop.example.com/fullchain.pem (success)- Point Nginx at
/etc/letsencrypt/live/<name>/fullchain.pemandprivkey.pem: these are symlinks that certbot updates on renewal. certbot --nginxedits the Nginx config for you;certonly --webrootleaves your config alone, which suits configuration management better.- A running server keeps the old certificate in memory until reloaded. Add a deploy hook, which runs only after a successful renewal:
/etc/letsencrypt/renewal-hooks/deploy/reload-nginxcontaining#!/bin/shandsystemctl reload nginx(executable). - Test against the staging environment (
--test-certor--dry-run) while you experiment. Production has rate limits (per registered domain and per account, check the current numbers in Let's Encrypt's docs), and a broken automation loop can lock you out of issuance for days.
Other clients: acme.sh (pure shell, many DNS provider APIs), lego (Go, single binary), Caddy and Traefik (built in), cert-manager on Kubernetes (Kubernetes).
When to renew
Let's Encrypt certificates have been valid for 90 days, and lifetimes across the industry are getting shorter (the CA/Browser Forum has voted a step-down to 47 days by 2029, and Let's Encrypt has announced shorter certificates too). Short lifetimes only work with automation, and automation only works if renewal starts early enough to survive failures. certbot renews when fewer than 30 days remain (newer clients also ask the CA through ACME Renewal Information, ARI, when to renew). A model of a twice-daily renewal timer, with the CA unreachable for a while:
def run(lifetime=90, renew_before=30, outage=range(0, 0), checks_per_day=2, days=200):
issued, events = 0, []
for step in range(days * checks_per_day):
day = step / checks_per_day
left = issued + lifetime - day
if left <= 0:
return f"EXPIRED on day {day:.0f}: " + "; ".join(events)
if left <= renew_before:
if int(day) in outage:
if not events or not events[-1].startswith("renewal failing"):
events.append(f"renewal failing from day {day:.0f}")
else:
events.append(f"renewed day {day:.0f} ({left:.0f}d left)")
issued = day
return "ok: " + "; ".join(events)
print("normal: ", run())
print("CA down 20 days: ", run(outage=range(60, 80)))
print("CA down 35 days: ", run(outage=range(60, 95)))
print("renew at 5 days left: ", run(renew_before=5, outage=range(85, 95)))normal: ok: renewed day 60 (30d left); renewed day 120 (30d left); renewed day 180 (30d left)
CA down 20 days: ok: renewal failing from day 60; renewed day 80 (10d left); renewed day 140 (30d left)
CA down 35 days: EXPIRED on day 90: renewal failing from day 60
renew at 5 days left: EXPIRED on day 90: renewal failing from day 85
Renewing with 30 days left absorbs weeks of failures; renewing at the last minute turns a short CA or DNS outage into an expired certificate. Which is why the next lesson is about watching expiry dates independently of the renewal job: Renewal & Expiry Monitoring.
Test yourself
Which ACME challenge do you need for a wildcard certificate (*.example.com)?
- HTTP-01
- DNS-01
- TLS-ALPN-01
- Any of them
certbot renewed the certificate successfully, but browsers still see the old one. What is missing?
- A DNS change
- A reload of the web server (e.g. a deploy hook running
systemctl reload nginx) - A new private key
- Port 80
Why does certbot renew 30 days before expiry instead of on the last day?
- Let's Encrypt requires it
- Early renewal leaves weeks to survive and notice failures (CA outages, DNS problems, broken config) before the certificate expires
- To save bandwidth
- Certificates can only be renewed once a month