Let's Encrypt issues free TLS certificates trusted by every browser, and certbot obtains and renews them automatically. It takes five minutes to set up, and then one day the certificate suddenly expires, renewal fails on rate limits, or the site serves an incomplete chain. Here is how it all works and how to make it run for years without attention.
How Let's Encrypt checks your domain
Before issuing a certificate you have to prove you control the domain. There are two main ways:
- HTTP-01. Certbot places a file under
/.well-known/acme-challenge/on the site, and Let's Encrypt's servers fetch it fromhttp://your-domain/. Port 80 must be open to the internet. - DNS-01. Certbot creates a TXT record
_acme-challenge.your-domain. This is the only way to get a wildcard certificate (*.example.com) and the only one that works for a server not reachable from outside.
Validation happens from several locations around the world at once. If the site is closed to foreign addresses (geo-blocking in the firewall or at the hosting level), HTTP-01 fails: use DNS-01 or open at least the /.well-known/acme-challenge/ path to everyone.
Installation and the first certificate
sudo apt install certbot python3-certbot-nginx # Debian, Ubuntu sudo dnf install certbot python3-certbot-nginx # AlmaLinux, Rocky (from EPEL)
Then one of three modes:
# nginx: certbot finds server_name and adds the certificate itself sudo certbot --nginx -d example.com -d www.example.com # webroot: the web server is left alone, the file goes into the site folder sudo certbot certonly --webroot -w /var/www/example -d example.com # standalone: certbot's own temporary web server, port 80 must be free sudo certbot certonly --standalone -d example.com
The --nginx mode is convenient, but it edits your configuration itself. If you keep the configuration in git or build it with a generator, certonly --webroot with your own nginx lines is more reliable.
Ready-made commands for nginx, your own folder, DNS validation via Cloudflare or by hand, with installation for different systems and a CAA record, are built by Let’s Encrypt certificate.
Which files to use
The certificates live in /etc/letsencrypt/live/example.com/:
fullchain.pemis the certificate together with the intermediate. Use this one, notcert.pem: desktop browsers often cope without the intermediate certificate, but Android, curl and mail clients report a chain error.privkey.pemis the private key, readable only by root.
These are links to the latest files in archive/, so the paths don't change after renewal; you only need to reload the web server. What is inside a certificate, its dates and chain are shown by Certificates and CSR.
Automatic renewal
A certificate is valid for 90 days, and Let's Encrypt is gradually shortening that period, so renewal has to run by itself. The certbot package installs a systemd timer or cron job that checks certificates twice a day and renews those with less than 30 days left.
systemctl list-timers | grep certbot # the timer exists and is scheduled sudo certbot renew --dry-run # a test renewal without issuing
The web server sees a renewed certificate only after reloading its configuration. Add a hook that runs after every successful renewal:
sudo tee /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh <<'EOF' #!/bin/sh systemctl reload nginx EOF sudo chmod +x /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh
Wildcard certificates
*.example.com requires DNS validation. You can get the certificate by hand (--manual --preferred-challenges dns), but you can't renew it automatically: you would have to change the TXT record by hand every time. Automatic renewal needs a plugin for your DNS provider, such as python3-certbot-dns-cloudflare, and an API token allowed to change records only in the zone you need.
Rate limits and common errors
- Rate limits. No more than 50 certificates per registered domain per week and no more than 5 identical ones (with the same set of names) per week; failed validations are limited too. Experiment with
--staging: the test server issues untrusted certificates, but its limits are much looser. - Port 80 is closed by the firewall, or something blocks the challenge. A redirect to HTTPS is fine, since Let's Encrypt follows it. What usually breaks validation is a block, basic authentication or a rule returning 404 for
/.well-known/. - An AAAA record points to the wrong place. If the domain has an IPv6 address, Let's Encrypt validates over it. A stale or wrong AAAA record is a frequent cause of “mysterious” failures.
- CAA forbids issuance. If the domain has CAA records, they must include
letsencrypt.org:example.com. CAA 0 issue "letsencrypt.org".
Next: setting up HTTPS
Getting a certificate is half the job: you also need to turn off old TLS versions and enable redirects and HSTS. That is covered in the article on HTTPS with nginx, and ready-made configurations come from the nginx config generator and TLS configuration. For the Russian Ministry of Digital Development certificate, see the article about it.