Security Basics — SSH, TLS and Certificates›11 · Free, automated certificates with ACME

Lesson 11 of 16 · HTTPS and certificates

Free, automated certificates with ACME

Free, automatically renewed certificates from a public CA with the ACME protocol: how the CA checks you control a domain (HTTP-01 and DNS-01 challenges), getting and installing a certificate with certbot for nginx, testing renewal, wildcards through DNS, the staging environment and rate limits, and the servers and tools that do all of it for you.

Practitioner
Key wordsACMELet's EncryptcertbotHTTP-01DNS-01TLS-ALPN-01wildcard certificaterenewalcertbot renewsystemd timerstaging environmentrate limitscert-managerCaddyTraefik

Free and automatic

Let's Encrypt is a free, public certificate authority trusted by all major browsers. It only issues through the ACME protocol, so there is no manual ordering or emailing of CSRs: software on your server requests, proves, installs and renews certificates on its own. Other CAs (for example ZeroSSL and Google Trust Services) also support ACME.

How the CA checks you own the name

Before issuing, the CA sets a challenge that only the domain's controller can complete:

HTTP-01                                       DNS-01
ACME client ── "I want example.com" ──► CA    ACME client ── "I want *.example.com" ──► CA
CA ── "serve token abc123" ──► client         CA ── "publish TXT value xyz789" ──► client
client puts file at                           client creates DNS record
 http://example.com/.well-known/                _acme-challenge.example.com TXT "xyz789"
 acme-challenge/abc123                        CA looks up the TXT record (from several places)
CA fetches it over port 80 (from several      ✔ → certificate issued
 places on the internet) ✔ → certificate
Challenge Needs Good for
HTTP-01 Port 80 reachable from the internet Ordinary public web servers
DNS-01 API access to your DNS provider Wildcards, servers not reachable from the internet, many servers
TLS-ALPN-01 Port 443, special support Some reverse proxies

To prove a house is yours, the office says: "Put this secret word in your front window and we'll walk past and look" (HTTP-01). Or: "Write this word in the town's address book next to your address" (DNS-01). Only the real owner can do either.

Getting a certificate with certbot and nginx

$ sudo apt install certbot python3-certbot-nginx
$ sudo certbot --nginx -d example.com -d www.example.com
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/example.com/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/example.com/privkey.pem
Deploying certificate to VirtualHost /etc/nginx/sites-enabled/example
Successfully deployed certificate for example.com to /etc/nginx/sites-enabled/example

Before running it: DNS for both names must point to this server, and port 80 must be open. The nginx plugin edits your server block to use fullchain.pem and privkey.pem (lesson 7).

Renewal is the whole point

Let's Encrypt certificates are short-lived (90 days by default, and getting shorter, lesson 12). The Ubuntu package installs a systemd timer that tries renewal twice a day and renews when a certificate is within about 30 days of expiry.

$ systemctl list-timers | grep certbot
$ sudo certbot renew --dry-run          # proves the whole renewal path works
$ sudo certbot certificates             # names, expiry dates, paths

Renewal must also reload the service that uses the files. The nginx plugin handles this; for anything else, add a deploy hook:

$ sudo tee /etc/letsencrypt/renewal-hooks/deploy/reload-haproxy.sh <<'EOF'
#!/bin/sh
systemctl reload haproxy
EOF
$ sudo chmod +x /etc/letsencrypt/renewal-hooks/deploy/reload-haproxy.sh

Note that Let's Encrypt no longer sends expiry reminder emails, so your own monitoring matters (lesson 12).

Wildcards with DNS-01

$ sudo certbot certonly --manual --preferred-challenges dns -d '*.example.com' -d example.com

--manual asks you to create the TXT record by hand, which cannot renew automatically. For real use, install the certbot DNS plugin for your provider (Cloudflare, Route 53 and many others) so it creates the record through the provider's API with a narrowly scoped token.

Staging and rate limits

Production Let's Encrypt has rate limits (for example on duplicate certificates and failed validations). While setting things up, add --test-cert (staging) or use --dry-run. Staging certificates are not trusted by browsers, but nothing gets blocked.

Let the platform do it

Many tools have ACME built in, so you never run certbot:

  • Caddy gets and renews certificates automatically for every site it serves.
  • Traefik and many ingress controllers have ACME resolvers.
  • cert-manager does it for Kubernetes (covered in the Kubernetes tracks).
  • Cloud load balancers (for example AWS ACM) issue and renew managed certificates.

Recap

  • ACME automates request, proof, installation and renewal; Let's Encrypt is free and trusted everywhere.
  • HTTP-01 needs port 80; DNS-01 works for wildcards and private servers.
  • Check the renewal timer, run certbot renew --dry-run, and add deploy hooks to reload services.
  • Use staging while experimenting; prefer tools with built-in ACME.

This site is a public version of my personal engineering knowledge hub. It intentionally excludes confidential company information and internal operational details.