Skip to content
masdzub.com
~/articles/certbot — one from the notebook

Certbot: Setup Let's Encrypt SSL Certificates with Automatic Renewal

Install Certbot, obtain free Let's Encrypt certificates, and configure automatic renewal on Linux servers.

masdzub@website:~$ cat ~/articles/certbot-setup-lets-encrypt-automatically.aspx.md
# 1 file · 7 min read · topic: certbot · status: published

Introduction

Certbot requests, installs, and renews Let’s Encrypt TLS certificates without manual intervention. This guide covers installation, certificate issuance for Nginx and Apache, renewal automation via systemd timers, and common failure scenarios with their fixes.

Install Certbot

Use your distro’s package manager. Snap is the officially recommended method and keeps Certbot updated independently of the OS release cycle.

Terminal window
# Remove any OS-packaged certbot first
sudo apt remove certbot 2>/dev/null
sudo dnf remove certbot 2>/dev/null
# Install via snap (Ubuntu, Debian, Fedora, RHEL, Arch)
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot

Verify the installation:

Terminal window
certbot --version

If your server does not support snap (minimal containers, older distros), install via pip in a virtual environment:

Terminal window
sudo python3 -m venv /opt/certbot
sudo /opt/certbot/bin/pip install certbot certbot-nginx
sudo ln -s /opt/certbot/bin/certbot /usr/bin/certbot

The pip route requires you to run pip install --upgrade certbot yourself. Snap handles that automatically.

Obtain a Certificate

Nginx

Terminal window
sudo certbot --nginx -d example.com -d www.example.com

Certbot reads your Nginx server blocks, proves domain ownership over HTTP-01, fetches the certificate, and rewrites the relevant server {} block to add ssl_certificate, ssl_certificate_key, and a 301 redirect from port 80.

Apache

Terminal window
sudo certbot --apache -d example.com -d www.example.com

Same flow. Certbot modifies the VirtualHost and creates an SSL VirtualHost if one does not exist.

Standalone (No Web Server Running)

If nothing listens on port 80, Certbot can spin up its own temporary server:

Terminal window
sudo certbot certonly --standalone -d example.com

This fails if another process already binds port 80. Stop it first or use the webroot method instead.

Webroot (Web Server You Do Not Want Certbot to Touch)

Point Certbot at your existing document root:

Terminal window
sudo certbot certonly --webroot -w /var/www/html -d example.com

Certbot places a challenge file under /var/www/html/.well-known/acme-challenge/, and Let’s Encrypt verifies it over HTTP. Your web server config stays untouched.

Where Certificates Land

Certbot stores everything under /etc/letsencrypt/:

/etc/letsencrypt/
├── live/
│ └── example.com/
│ ├── fullchain.pem # cert + intermediate chain
│ ├── privkey.pem # private key
│ ├── cert.pem # server cert only
│ └── chain.pem # intermediate only
├── archive/ # versioned copies
└── renewal/ # renewal config per domain
└── example.com.conf

Point your web server or application at the live/ symlinks. They always resolve to the latest version after renewal.

Automatic Renewal

Certbot’s snap package installs a systemd timer that runs certbot renew twice daily. Certificates renew only when they are within 30 days of expiry, so the frequent schedule is harmless.

Check the timer status:

Terminal window
sudo systemctl list-timers | grep certbot

You should see snap.certbot.renew.timer (or certbot.timer on pip installs that ship a unit file). If the timer is missing, create one:

/etc/systemd/system/certbot-renew.timer
[Unit]
Description=Certbot renewal timer
[Timer]
OnCalendar=*-*-* 02,14:30:00
RandomizedDelaySec=3600
Persistent=true
[Install]
WantedBy=timers.target
/etc/systemd/system/certbot-renew.service
[Unit]
Description=Certbot renewal
[Service]
Type=oneshot
ExecStart=/usr/bin/certbot renew --quiet
Terminal window
sudo systemctl daemon-reload
sudo systemctl enable --now certbot-renew.timer

Post-Renewal Hooks

Certbot renews the files on disk, but your web server still holds the old certificate in memory until it reloads. Add a deploy hook so the reload happens automatically:

Terminal window
sudo certbot renew --deploy-hook "systemctl reload nginx"

To make the hook permanent for a specific certificate, edit /etc/letsencrypt/renewal/example.com.conf and add under [renewalparams]:

[renewalparams]
renew_hook = systemctl reload nginx

For Apache, replace with systemctl reload apache2 (or httpd on RHEL-family systems).

Dry Run

Test renewal without actually contacting Let’s Encrypt:

Terminal window
sudo certbot renew --dry-run

This exercises the full flow (including hooks) against the Let’s Encrypt staging server. Run it after any config change.

Rate Limits

Let’s Encrypt enforces production rate limits. The ones you will hit first:

LimitValue
Certificates per registered domain per week50
Duplicate certificates per week5
Failed authorizations per hostname per hour5

During testing, use the staging environment to avoid burning through limits:

Terminal window
sudo certbot certonly --nginx --staging -d example.com

Staging certificates are not trusted by browsers, but the ACME flow is identical. Remove --staging for production.

Wildcard Certificates

Wildcard certs (*.example.com) require DNS-01 validation. Certbot needs a plugin for your DNS provider. Example with Cloudflare:

Terminal window
sudo snap install certbot-dns-cloudflare
# Create credentials file
sudo mkdir -p /etc/letsencrypt/cloudflare
cat <<EOF | sudo tee /etc/letsencrypt/cloudflare/credentials.ini
dns_cloudflare_api_token = YOUR_API_TOKEN_HERE
EOF
sudo chmod 600 /etc/letsencrypt/cloudflare/credentials.ini
sudo certbot certonly \
--dns-cloudflare \
--dns-cloudflare-credentials /etc/letsencrypt/cloudflare/credentials.ini \
-d "*.example.com" \
-d example.com

The Cloudflare API token needs Zone:DNS:Edit permission for the target zone. Scoped tokens are safer than global API keys.

DNS plugins exist for Route 53, DigitalOcean, Google Cloud DNS, Linode, and others. Install the matching certbot-dns-* snap or pip package.

Troubleshooting

Challenge Failed: Connection Refused

Challenge failed for domain example.com
Fetching http://example.com/.well-known/acme-challenge/xxxx: Connection refused

Root cause: Let’s Encrypt cannot reach port 80 on your server. Check that port 80 is open in your firewall and that your DNS A/AAAA record points to the correct IP.

Terminal window
# Check firewall
sudo ufw status # Ubuntu
sudo firewall-cmd --list-all # RHEL/Fedora
# Check DNS resolution
dig +short example.com
curl -I http://example.com/.well-known/acme-challenge/test

Too Many Certificates Already Issued

Error creating new order :: too many certificates (5) already issued for this
exact set of domains in the last 168 hours

Wait a week, or add/remove a SAN to change the “exact set” and issue a new certificate. Use --staging for testing.

Permission Denied on Renewal

Could not open file: /etc/letsencrypt/archive/example.com/privkey3.pem -
Permission denied

The renewal runs as root via the systemd timer. If you ran certbot as a different user during setup, ownership may be mixed. Fix it:

Terminal window
sudo chown -R root:root /etc/letsencrypt
sudo chmod -R 600 /etc/letsencrypt/archive/
sudo chmod -R 700 /etc/letsencrypt/live/ /etc/letsencrypt/archive/

Nginx Config Test Fails After Renewal

nginx: [emerg] cannot load certificate "/etc/letsencrypt/live/example.com/fullchain.pem"

The symlink in live/ is broken, usually because someone manually deleted files in archive/. Reissue:

Terminal window
sudo certbot delete --cert-name example.com
sudo certbot --nginx -d example.com -d www.example.com

DNS-01 Challenge Timeout (Wildcard)

Waiting for verification...
Challenge failed for domain *.example.com
dns-01 challenge :: DNS problem: NXDOMAIN looking up TXT for _acme-challenge.example.com

The TXT record was not created or has not propagated. Increase the propagation wait:

Terminal window
sudo certbot certonly \
--dns-cloudflare \
--dns-cloudflare-credentials /etc/letsencrypt/cloudflare/credentials.ini \
--dns-cloudflare-propagation-seconds 60 \
-d "*.example.com"

Verify the record manually:

Terminal window
dig TXT _acme-challenge.example.com +short

Revoking and Deleting Certificates

If a private key is compromised:

Terminal window
sudo certbot revoke --cert-name example.com --reason keycompromise
sudo certbot delete --cert-name example.com

Revocation notifies Let’s Encrypt immediately. delete removes the local files and renewal config.

Checking Certificate Expiry

Quick check from the command line:

Terminal window
sudo certbot certificates

From a remote machine:

Terminal window
echo | openssl s_client -servername example.com -connect example.com:443 2>/dev/null \
| openssl x509 -noout -dates

Both show notBefore and notAfter. If notAfter is less than 30 days out and the timer is active, renewal should happen automatically on the next run. If it does not, run certbot renew --dry-run and read the error output.

found this useful? share it →
Written by

Dzubayyan Ahmad

System Administrator, SRE & Technical Support Engineer specialising in web hosting infrastructure — Linux servers, automation, and monitoring. Writing these notes since 2015 so future-me can find the fix again.