Ubuntu 24.04: Certbot + Cloudflare DNS for Wildcard Certificates with Auto-Renewal
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-challengerecord 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
| Item | Requirement |
|---|---|
| Operating system | Ubuntu 24.04 LTS (noble) |
| Domain | Managed by Cloudflare, resolving normally |
| Access | sudo or root |
| DNS plugin | certbot-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-cloudflareplugin 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 → Editto create and delete the_acme-challengeTXT recordZone → Zone → Readto 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 purgeasks 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.
Option A: use sudo (recommended)
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
| Prompt | Answer |
|---|---|
Enter email address | your email, used for expiry notices |
Do you agree? (Y)es/(N)o | Y |
Share email with EFF? (Y)es/(N)o | N |
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
| Error | Cause | Fix |
|---|---|---|
Permission denied: '/var/log/letsencrypt/.certbot.lock' | a normal user cannot write to the system directory | run with sudo, or use Option B |
Incorrect TXT record ... found at _acme-challenge | DNS has not propagated, or the apex and wildcard share the same TXT name | raise --dns-cloudflare-propagation-seconds to 120 or 180; if needed, issue the two names separately |
Error determining zone_id: 6003 / HTTP 403 | the token is invalid, expired, or lacks Zone:Read / DNS:Edit | create a new token from the Edit zone DNS template with the correct zone scope |
too many failed authorizations recently | too many failed validations triggered the Let's Encrypt rate limit | check the TXT record first, wait about an hour, then retry |
certbot: command not found | the virtual environment is not activated, or the sudo PATH lacks it | source ~/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 bothcertbotandnginx(check withwhich nginx) - Test before reloading:
nginx -t && nginx -s reloadkeeps a broken configuration from taking the service down - The hook only runs on a real renewal: a not-yet-due certificate and
--dry-runnever 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
600and owned by the right user - the credentials path in the renewal config matches the one used by cron
certbot renew --dry-runsucceeds- both
certbotandnginxin the cron line use absolute paths - Nginx references the certificate under
live/and reloads successfully - the live certificate is verified with
curloropenssl s_client
Appendix: path reference
| Item | Path |
|---|---|
| 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
No comments yet — be the first to say something