Sub topic 8
Obtaining your first certificate
This chapter takes you from a prepared server to a valid certificate on disk. It assumes you have a Linux server running nginx, a domain whose A (and AAAA, if used) records point at that server, and ports 80 and 443 open in the firewall. It also assumes you can use sudo and have read the earlier chapters on ACME and domain validation. You will install Certbot, test against the staging environment, request the production certificate, and confirm the result. Wiring the certificate into nginx comes in a later chapter.
What happens when you run Certbot
Certbot is an ACME client. When you ask for a certificate, it goes through these steps:
- It creates or reuses an ACME account, which is a key pair registered with Let's Encrypt.
- It generates a private key and a certificate signing request (CSR) for the names you listed.
- It asks Let's Encrypt for a challenge for each name.
- It publishes the challenge response, which for HTTP-01 is a file under
/.well-known/acme-challenge/on port 80. - Let's Encrypt fetches that URL. If the content matches, the name is validated.
- Certbot submits the CSR, downloads the signed certificate and chain, and stores them under
/etc/letsencrypt/.
Install Certbot
On Debian or Ubuntu, the packaged client and its nginx plugin are enough for this chapter.
sudo apt update
sudo apt install certbot python3-certbot-nginx
certbot --versionThe distribution package may lag behind the newest Certbot release. That is acceptable for HTTP-01 with nginx. If you need a newer client, the Certbot project documents a snap-based install, but do not mix the two installations on one host.
Choose an authenticator
An authenticator is the method Certbot uses to answer the challenge. The three common choices differ in what they need from your server.
| Option | How it answers HTTP-01 | Requirement |
|---|---|---|
| `--nginx` | Temporarily edits nginx configuration, reloads it, then reverts | A server block with a matching `server_name` |
| `--webroot -w /path` | Writes the token file into a directory nginx already serves | A known document root, and nginx must serve `/.well-known/` from it |
| `--standalone` | Runs its own temporary web server on port 80 | Port 80 must be free, so nginx must be stopped |
This chapter uses certonly --nginx. The certonly subcommand obtains the certificate and stops there. Without it, certbot --nginx would also rewrite your nginx configuration. Letting a tool edit a production configuration is convenient on day one and confusing on day ninety, so we write the TLS block by hand in the next chapter.
Test against staging first
Let's Encrypt runs a separate staging environment. It performs the same validation, but its certificates come from an untrusted CA and its rate limits are far more generous. Use it to find DNS, firewall, and configuration mistakes without burning production quota.
sudo certbot certonly --nginx --staging \
-d example.com -d www.example.comOn the first run Certbot prompts for an email address, asks you to accept the terms of service, and offers to share your email with the EFF. The email receives expiry warnings, so use one you read. For scripted installs, supply these answers as flags:
sudo certbot certonly --nginx --staging \
--agree-tos -m [email protected] --no-eff-email \
-d example.com -d www.example.comA successful run prints the paths of the certificate and key and the expiry date. The staging certificate is not trusted by browsers. That is expected, and it proves only that validation works.
Request the production certificate
Once staging succeeds, repeat the command without --staging:
sudo certbot certonly --nginx \
-d example.com -d www.example.comCertbot names a certificate (its "lineage") after the first -d value, so the staging and production certificates would both be called example.com. Certbot will then ask whether to keep, renew, or replace the existing one, and non-interactive runs may report that the certificate is not yet due for renewal. The clean fix is to delete the staging lineage first:
sudo certbot delete --cert-name example.comThen run the production command above. The alternative is --force-renewal, which also works but counts toward your duplicate-certificate limit in the same way.
Check the result
sudo ls -l /etc/letsencrypt/live/example.com/
sudo certbot certificatesThe first command lists four entries: fullchain.pem, privkey.pem, cert.pem and chain.pem. They are symlinks into /etc/letsencrypt/archive/example.com/. Point services at the live/ paths, never at archive/, because Certbot updates the symlinks at each renewal. The second command shows, for each certificate, the lineage name, the domains it covers, the expiry date, and the file paths. Confirm that both names appear and that the expiry is about 90 days away. Let's Encrypt has announced shorter lifetimes, so rely on automation and not on a number you memorised.
Rate limits and failed validations
These limits matter most when you retry in a hurry. The figures below were current at the time of writing, so check the Let's Encrypt documentation for exact values.
- A duplicate certificate, meaning one for exactly the same set of names, can be issued only a few times per week.
- Repeated validation failures for the same name are limited per hour.
- New orders per account and new certificates per registered domain also have ceilings.
Staging has its own, higher limits. The habit that follows is to change one thing, test on staging, and go to production only after staging passes.
Common failures at this stage
When validation fails, Certbot prints the URL Let's Encrypt tried and the error it received. Read that message first, because it names the cause. The most frequent ones are these:
- Timeout or connection refused. Port 80 is blocked by a cloud security group,
ufw, ornftables, or nginx is not listening on the public address. Check withsudo ss -tlnp | grep ':80'and test from outside the server. - 404 or unexpected content. The name resolves to a different server, often because an AAAA record still points at an old host. Let's Encrypt prefers IPv6 when a record exists, so a stale AAAA record fails validation even if IPv4 is correct.
- NXDOMAIN or wrong IP. DNS has not propagated, or a name you listed with
-dhas no record. Every name in the request must validate, or the whole request fails. - Could not find a matching server block. With
--nginx, aserver_namefor each requested name must exist. Add it or switch to--webroot. - Redirect loops. A rule that forces HTTPS before the certificate exists can break the challenge. Plain redirects to HTTPS are acceptable once a valid certificate is in place, but the challenge path must stay reachable.
Exercise
You ran the staging command, then ran the production command without deleting anything. Certbot reports that the certificate is not due for renewal and does not issue a trusted one. Why does this happen, and what are two ways to fix it?
Show answer
Both runs use the lineage name example.com (the first -d value), so Certbot sees an existing, unexpired certificate and declines to replace it. Fix it by deleting the staging lineage with "sudo certbot delete --cert-name example.com" and rerunning the production command, or by adding --force-renewal to the production command. Deleting is cleaner because force-renewal consumes a production issuance and leaves the same lineage to reason about.
Exercise
Validation fails with a timeout, although example.com loads fine from your laptop. Name two things to check on the server and one thing to check in DNS.
Show answer
On the server, confirm that nginx listens on port 80 with "sudo ss -tlnp | grep ':80'", and confirm the firewall or cloud security group allows inbound port 80 from the whole internet. In DNS, look for a stale AAAA record that points at a different host, because Let's Encrypt tries IPv6 first when that record exists.