Setting Up and Automating Let's Encrypt Certificates with Certbot and Nginx
Table of Contents
I run my sites on a small Linux server with nginx, and for a long time I renewed my HTTPS certificate by hand with a little shell script. It worked, until I realised that Let’s Encrypt certificates are only valid for 90 days and that I had no idea whether my “automation” would actually survive without me. This post documents how I set things up properly, so I (and anyone else reading) can repeat it without guessing.
I worked through the details with Claude, an AI assistant, and double-checked the important claims against the Let’s Encrypt documentation.
What this post covers #
- Why Let’s Encrypt certificates are short-lived
- Getting your first certificate
- Why my original “stop nginx, renew, start nginx” script was a bad idea
- Setting up renewal that works without downtime
- Checking and changing how often renewal happens
- What changes when certificates drop to 45 days
Throughout this post I use website.com as a stand-in for your own domain. Replace it everywhere with your real domain name.
Assumptions: an Ubuntu/Debian server, nginx already serving your site on port 80, a domain whose DNS already points at the server, and sudo access.
Why only 90 days? #
Let’s Encrypt has issued 90-day certificates since it launched in 2015, and that is deliberate:
- It forces you to automate. Ninety days is short enough that nobody wants to renew manually forever, but long enough that you can do it by hand while getting started.
- It limits damage. If a private key leaks or a certificate is issued by mistake, the problem ends when the certificate expires. Revocation is unreliable in practice, so a short lifetime is a built-in safety net.
Lifetimes are going to get shorter. Let’s Encrypt already offers optional 6-day certificates, and industry rules will cap all public certificates at 47 days from March 15, 2029. Let’s Encrypt plans to reduce its maximum to 45 days by February 2028. If your renewal is automatic, you won’t have to do anything when that happens. That is the whole point of this post.
Step 1: Install certbot #
sudo apt update
sudo apt install certbot python3-certbot-nginx
The python3-certbot-nginx package is the nginx plugin. It lets certbot prove domain ownership through nginx while nginx keeps running, which matters later.
Certbot’s own documentation also describes a snap-based install. Either works. If you use the snap, install the nginx plugin the way its documentation describes.
Step 2: Get your first certificate #
Make sure nginx has a server block for your domain with a matching server_name:
server {
listen 80;
server_name website.com www.website.com;
# ...
}
Then request the certificate:
sudo certbot --nginx -d website.com -d www.website.com
Certbot verifies that you control the domain, downloads the certificate, and edits your nginx config to use it. Open https://website.com to confirm it works.
You can list everything certbot knows about with:
sudo certbot certificates
This shows each certificate name, the domains it covers, its expiry date, and where the files live (under /etc/letsencrypt/live/website.com/).
The script I started with (and why to retire it) #
Before automating, this is what I used to run manually:
sudo service nginx stop
sudo certbot certonly --standalone --preferred-challenges http -d website.com
sudo service nginx start
It works, but it has real problems for automation:
- Downtime.
--standalonemakes certbot start its own tiny web server on port 80, so nginx must be stopped first. Your site is down every time. - It stops nginx even when nothing needs renewing.
certonlycan prompt for input if a certificate already exists, which is a problem in a scheduled job.
Certbot has a much better command built for this: certbot renew. It checks every certificate and only renews the ones that are close to expiry.
Step 3: Check whether renewal is already scheduled #
On Debian/Ubuntu, installing certbot normally sets up a systemd timer for you:
systemctl list-timers | grep certbot
Output looks roughly like this:
Mon 2026-11-30 05:36:57 UTC 15h Sun 2026-11-29 13:43:17 UTC 31min ago certbot.timer certbot.service
If you see certbot.timer, a schedule already exists. But an active timer doesn’t prove renewals succeed. That depends on how the certificate was originally issued.
Step 4: Make sure renewal actually works #
Run a dry run. This simulates renewal against Let’s Encrypt’s staging servers without issuing anything real:
sudo certbot renew --dry-run
Then look at how your certificate is set to renew:
sudo cat /etc/letsencrypt/renewal/website.com.conf
Find the authenticator line under [renewalparams]:
authenticator = nginxorwebroot: renewal works while nginx is running. You’re good.authenticator = standalone: the timer will try to bind to port 80 while nginx is using it, and the renewal will fail. Pick one of the two fixes below.
Option A: keep standalone, but stop nginx only when needed #
Certbot runs scripts from special hook folders during certbot renew, and the pre and post hooks only run when a renewal is actually due:
sudo tee /etc/letsencrypt/renewal-hooks/pre/stop-nginx.sh <<'EOF'
#!/bin/sh
service nginx stop
EOF
sudo tee /etc/letsencrypt/renewal-hooks/post/start-nginx.sh <<'EOF'
#!/bin/sh
service nginx start
EOF
sudo chmod +x /etc/letsencrypt/renewal-hooks/pre/stop-nginx.sh \
/etc/letsencrypt/renewal-hooks/post/start-nginx.sh
This is a small change, but your site still goes down briefly on each renewal.
Option B: switch to the nginx plugin (recommended) #
This gives you zero downtime. Re-issue the certificate using the nginx authenticator:
sudo certbot certonly --nginx --cert-name website.com -d website.com
Certbot notices the existing certificate and asks what to do. Choose the option to renew and replace it. This saves the new method (nginx) into the renewal config.
Because certonly doesn’t install the certificate into nginx for you, add a deploy hook so nginx reloads the new certificate after each 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
Don’t combine this with the stop/start hooks from Option A, since you would be stopping a server that doesn’t need stopping.
Whichever option you choose, finish by running the dry run again:
sudo certbot renew --dry-run
If it ends with “all simulated renewals succeeded”, you’re done.
Step 5: Know how often renewal happens #
There are two separate settings, and mixing them up is easy.
How often certbot checks (the timer) #
systemctl cat certbot.timer
The stock schedule is typically twice a day with a random delay, which is why the run times look irregular. Running the check often is harmless: if no certificate is due, it does nothing.
To change the schedule, don’t edit the packaged file, because updates can overwrite it. Create an override instead:
sudo systemctl edit certbot.timer
Add (for example, once a day around 03:00):
[Timer]
OnCalendar=
OnCalendar=*-*-* 03:00:00
RandomizedDelaySec=1h
The empty OnCalendar= line is required. It clears the original schedule, otherwise both would run. Then apply it:
sudo systemctl daemon-reload
sudo systemctl restart certbot.timer
systemctl list-timers | grep certbot
When a certificate actually gets renewed #
By default, certbot renew renews a certificate only when it has 30 days or fewer left. For a 90-day certificate, that means renewal happens around day 60.
You can change this per certificate in its renewal config:
sudo nano /etc/letsencrypt/renewal/website.com.conf
Under [renewalparams]:
renew_before_expiry = 45 days
Careful with this setting. It means “renew when this many days or fewer remain”, so the value must always be lower than the certificate’s lifetime. If you set it equal to or higher than the lifetime, the certificate is “due” immediately after issuance and certbot will try to renew it on every single run.
| Certificate lifetime | renew_before_expiry | Renews at |
|---|---|---|
| 90 days (today) | 30 days (default) | day 60 |
| 90 days | 45 days | day 45 |
| 45 days (from 2028) | 30 days (default) | day 15 |
| 45 days | 15 days | day 30 |
For most people, the default is fine and you never need to touch this.
What changes with 45-day certificates? #
If you’re on the standard certbot timer, nothing breaks. The default 30-day window still works with a 45-day certificate, though it will renew earlier (around day 15). If you want the “renew about two thirds of the way through” schedule Let’s Encrypt recommends, set renew_before_expiry = 15 days once 45-day certificates actually apply to you.
Two other things worth knowing:
- Don’t hardcode a 60-day interval. A cron job that renews every 60 days will not be enough once certificates last 45 days.
- ARI is the long-term answer. ACME Renewal Information lets Let’s Encrypt tell your client when to renew, so you don’t pick a number at all. Check whether your certbot version supports it.
Verifying everything #
A few commands I now use to check on things:
# All certificates, expiry dates and paths
sudo certbot certificates
# Simulate a renewal end-to-end
sudo certbot renew --dry-run
# What did the last scheduled run do?
journalctl -u certbot.service -n 30
# Full certbot log
sudo tail -50 /var/log/letsencrypt/letsencrypt.log
# The certificate nginx is *actually serving* (this is the one that matters)
echo | openssl s_client -connect website.com:443 2>/dev/null | openssl x509 -noout -dates
That last command is my favourite, because a renewed certificate on disk means nothing if nginx never reloaded it.
Troubleshooting #
- “Address already in use” on port 80: the certificate is on the standalone authenticator while nginx is running. Switch to Option B.
- Challenge fails / timeout: check that DNS points to this server and that port 80 is open in your firewall and cloud provider’s security rules. Let’s Encrypt must reach the server over HTTP.
- Renewal succeeds but the browser shows the old certificate: nginx hasn’t reloaded. Check your deploy hook exists, is executable, and that
systemctl reload nginxworks. - Hitting rate limits: usually caused by repeatedly requesting real certificates while testing. Use
--dry-run, which uses the staging environment.
Summary #
- Install certbot and the nginx plugin.
- Get your first certificate with
certbot --nginx. - Confirm the renewal timer exists with
systemctl list-timers. - Confirm the renewal method is nginx (or use stop/start hooks if standalone).
- Add a deploy hook so nginx reloads after renewal.
- Test with
certbot renew --dry-run. - Leave the default schedule alone unless you have a reason to change it.
Once this is set up, you can forget about it. And with certificate lifetimes shrinking over the next few years, “set up once and automate” is exactly the right habit.