Ubuntu 24.04: Certbot + Cloudflare DNS for Wildcard Certificates with Auto-Renewal

2026-11-12 DevOps 有希 123 views

This guide shows how to install Certbot in a Python virtual environment on Ubuntu 24.04 LTS, prove domain ownership through the Cloudflare DNS API (DNS-01), and issue a Let's Encrypt wildcard certificate for example.com and *.example.com. When everything is in place, cron checks for renewal every day at 02:30 and reloads Nginx only after a certificate is actually renewed.

Why DNS-01 with a wildcard certificate

  • Wildcard support: one certificate covers *.example.com, so new subdomains do not need a new certificate
  • No port 80 required: validation only creates a TXT record, which helps when port 80 is occupied or cannot be opened temporarily
  • Fully automatable: Cloudflare has an API, and Certbot creates and removes the _acme-challenge record for you

DNS-01 proves control of the domain. It does not make the server reachable; you still have to deploy the certificate to your web server.

1. Prerequisites

ItemRequirement
Operating systemUbuntu 24.04 LTS (noble)
DomainManaged by Cloudflare, resolving normally
Accesssudo or root
DNS plugincertbot-dns-cloudflare
Certificate path/etc/letsencrypt/live/example.com/

Replace example.com with your own domain in every command below.

2. Install Certbot in a virtual environment

Keeping Certbot in a Python virtual environment avoids touching the system Python and makes upgrades or rollbacks easier.

sudo apt update
sudo apt install python3-venv -y

python3 -m venv ~/certbot-venv
source ~/certbot-venv/bin/activate

pip install --upgrade pip
pip install certbot certbot-dns-cloudflare

Important: use the certbot-dns-cloudflare plugin for Cloudflare DNS. It calls the Cloudflare API v4 and works with scoped API Tokens.

3. Create a Cloudflare API token

Do not use your Global API Key. In the Cloudflare dashboard, open My Profile → API Tokens → Create Token, use the Edit zone DNS template, and scope it to the zone that hosts your domain. The token needs:

  • Zone → DNS → Edit to create and delete the _acme-challenge TXT record
  • Zone → Zone → Read to look up the zone ID

Store the token in a credentials file:

mkdir -p ~/.secrets/certbot
nano ~/.secrets/certbot/cloudflare.ini

File content:

dns_cloudflare_api_token = your-cloudflare-api-token

Then lock the file down and verify the result:

chmod 600 ~/.secrets/certbot/cloudflare.ini
chmod 700 ~/.secrets ~/.secrets/certbot
ls -l ~/.secrets/certbot/cloudflare.ini
# expected: -rw------- 1 ubuntu ubuntu ... cloudflare.ini

Security note: never commit the credentials file to Git or upload it anywhere public. If it leaks, revoke the token in the Cloudflare dashboard and create a new one.

4. Remove a system-wide Certbot (optional)

If Certbot was previously installed with apt, remove it so two versions do not compete for commands and configuration.

apt list --installed | grep certbot
sudo apt purge -y certbot python3-certbot
sudo apt autoremove -y && sudo apt autoclean -y

When apt purge asks whether to delete /etc/letsencrypt, keep it. Otherwise existing certificates and renewal configuration are removed as well.

Verify:

dpkg -l | grep certbot   # expected: no output

If the virtual environment is active, which certbot still points to ~/certbot-venv/bin/certbot. That is expected.

5. Issue the wildcard certificate

Certbot needs to write to /var/log/letsencrypt/, which a normal user cannot do. Choose one of the following approaches.

sudo /home/ubuntu/certbot-venv/bin/certbot certonly 
  --authenticator dns-cloudflare 
  --dns-cloudflare-credentials /home/ubuntu/.secrets/certbot/cloudflare.ini 
  --dns-cloudflare-propagation-seconds 60 
  -d example.com 
  -d "*.example.com"

Important: under sudo, ~ resolves to /root. Use absolute paths (/home/ubuntu/...) for both the virtual environment and the credentials file.

Option B: no sudo, custom directories

mkdir -p ~/certbot-data/{config,work,logs}

certbot certonly 
  --config-dir ~/certbot-data/config 
  --work-dir ~/certbot-data/work 
  --logs-dir ~/certbot-data/logs 
  --authenticator dns-cloudflare 
  --dns-cloudflare-credentials ~/.secrets/certbot/cloudflare.ini 
  --dns-cloudflare-propagation-seconds 60 
  -d example.com 
  -d "*.example.com"

With Option B, every later renewal must pass the same three directory options, or Certbot will not find the certificate. Option A is the better choice for production.

Interactive prompts

PromptAnswer
Enter email addressyour email, used for expiry notices
Do you agree? (Y)es/(N)oY
Share email with EFF? (Y)es/(N)oN

A successful run ends with:

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

6. Common errors

ErrorCauseFix
Permission denied: '/var/log/letsencrypt/.certbot.lock'a normal user cannot write to the system directoryrun with sudo, or use Option B
Incorrect TXT record ... found at _acme-challengeDNS has not propagated, or the apex and wildcard share the same TXT nameraise --dns-cloudflare-propagation-seconds to 120 or 180; if needed, issue the two names separately
Error determining zone_id: 6003 / HTTP 403the token is invalid, expired, or lacks Zone:Read / DNS:Editcreate a new token from the Edit zone DNS template with the correct zone scope
too many failed authorizations recentlytoo many failed validations triggered the Let's Encrypt rate limitcheck the TXT record first, wait about an hour, then retry
certbot: command not foundthe virtual environment is not activated, or the sudo PATH lacks itsource ~/certbot-venv/bin/activate, or call the absolute path

Troubleshoot in this order: confirm the TXT record exists, confirm it resolves with dig, then retry. Blind retries are the fastest way to hit the rate limit.

7. Configure automatic renewal

Let's Encrypt certificates are valid for 90 days. Certbot renews when less than 30 days remain, so there is no need to force an issue every day.

1. Confirm the credentials path in the renewal config

sudo cat /etc/letsencrypt/renewal/example.com.conf | grep -i credentials

Expected output:

dns_cloudflare_credentials = /home/ubuntu/.secrets/certbot/cloudflare.ini

The file belongs to ubuntu with mode 600, but cron runs as root and can still read it.

2. Dry run (mandatory)

sudo /home/ubuntu/certbot-venv/bin/certbot renew --dry-run

Only Congratulations, all simulated renewals succeeded proves the whole chain works. A dry run does not replace the certificate and does not trigger --post-hook.

3. Add the cron job

sudo crontab -e

Add one line:

30 2 * * * /home/ubuntu/certbot-venv/bin/certbot renew --quiet --post-hook "/usr/sbin/nginx -t && /usr/sbin/nginx -s reload" >> /var/log/certbot-renew.log 2>&1

Details:

  • Use absolute paths: cron has a minimal PATH; write the full path for both certbot and nginx (check with which nginx)
  • Test before reloading: nginx -t && nginx -s reload keeps a broken configuration from taking the service down
  • The hook only runs on a real renewal: a not-yet-due certificate and --dry-run never trigger it
  • With Apache, replace the hook with /usr/bin/systemctl reload apache2

4. Inspect certificates and renewal logs

sudo /home/ubuntu/certbot-venv/bin/certbot certificates
sudo tail -f /var/log/certbot-renew.log

8. Use the certificate in Nginx

ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

Files under live/ are symlinks; the real files live in archive/, and a web server running as root can read them.

Test and reload:

sudo nginx -t && sudo nginx -s reload

Verify the certificate:

curl -I https://example.com
openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null | openssl x509 -noout -subject -dates

9. Wrap up and checklist

You can leave the virtual environment now:

deactivate

cron calls Certbot by absolute path and does not depend on the shell state. Before going live, confirm each item:

  • credentials file mode is 600 and owned by the right user
  • the credentials path in the renewal config matches the one used by cron
  • certbot renew --dry-run succeeds
  • both certbot and nginx in the cron line use absolute paths
  • Nginx references the certificate under live/ and reloads successfully
  • the live certificate is verified with curl or openssl s_client

Appendix: path reference

ItemPath
Virtual environment/home/ubuntu/certbot-venv/
Certbot binary/home/ubuntu/certbot-venv/bin/certbot
DNS credentials/home/ubuntu/.secrets/certbot/cloudflare.ini
Certificate chain/etc/letsencrypt/live/example.com/fullchain.pem
Private key/etc/letsencrypt/live/example.com/privkey.pem
Renewal config/etc/letsencrypt/renewal/example.com.conf
Renewal log/var/log/certbot-renew.log

Automation is only the starting point. Add an expiry monitor that checks the remaining days 14 days before expiry, so a silently failing cron job does not go unnoticed.

Comments

Sign in to join the discussion

No comments yet — be the first to say something