Guide to Automatically Renew Wildcard SSL Certificates Using Certbot and Cloudflare API on Linux VPS
Automatically renew wildcard SSL certificates on a Linux VPS using Certbot and the Cloudflare API to prevent expiration and ensure continuous web security. This guide covers...
Wildcard SSL certificates are an effective security solution for managing multiple subdomains under a single domain, especially when hosting several websites or services on a Linux VPS. This guide provides a step-by-step method to automatically renew wildcard SSL certificates using Certbot in combination with the Cloudflare API. This ensures continuous protection for your web infrastructure without manual intervention, avoiding service disruptions caused by expired certificates.
Prerequisites
To follow this guide, you will need:
- A Linux VPS (Ubuntu 20.04/22.04 or CentOS 8/9) with root or sudo access.
- A domain (e.g., example.com) managed through Cloudflare DNS.
- A Cloudflare API key with DNS editing permissions (typically "Edit DNS").
- A previously issued wildcard SSL certificate from Cloudflare or Certbot.
Wildcard SSL certificates allow you to secure multiple subdomains such as blog.example.com and shop.example.com with a single certificate, simplifying management and reducing costs. Automated renewal is essential to prevent service interruptions due to certificate expiration.
Install and Configure Certbot with Cloudflare DNS Plugin
First, update your system and install Certbot on the Linux VPS:
sudo apt update
sudo apt install certbot -y
To use the Cloudflare DNS plugin, install the additional package:
sudo apt install python3-certbot-dns-cloudflare -y
Next, create a configuration file for your Cloudflare API key. Create the file ~/.secrets/cloudflare.ini with the following content (replace YOUR_CLOUDFLARE_API_KEY and YOUR_EMAIL with your actual values):
[cloudflare]
dns_cloudflare_api_key = YOUR_CLOUDFLARE_API_KEY
dns_cloudflare_email = [email protected]
Ensure the file has read-only permissions for the current user:
chmod 600 ~/.secrets/cloudflare.ini
Renew Wildcard SSL Certificate
To obtain or renew a wildcard SSL certificate, run the following command using the Cloudflare DNS plugin:
sudo certbot certonly --dns-cloudflare --dns-cloudflare-credentials ~/.secrets/cloudflare.ini -d *.example.com
This command will:
- Perform DNS verification via the Cloudflare API.
- Automatically generate a wildcard SSL certificate for
*.example.com. - Store the certificate in
/etc/letsencrypt/live/example.com/.
Let's Encrypt certificates are valid for 90 days, so automated renewal is required. Certbot automatically checks and renews certificates 30 days before expiration.
Set Up Automated Renewal with Cron Job
To ensure automatic renewal, configure a cron job:
sudo crontab -e
Add the following line to run the renewal daily at 02:00:
0 2 * * * /usr/bin/certbot renew --quiet --dns-cloudflare --dns-cloudflare-credentials ~/.secrets/cloudflare.ini
Note: Adjust the path to Certbot if necessary (e.g., /usr/local/bin/certbot on some systems).
Verify and Troubleshoot
To verify that the certificate has been successfully renewed, run:
sudo certbot certificates
This displays a list of issued certificates, their expiration dates, and status. If renewal was successful, the expiration date will be updated.
If issues occur, check the Certbot logs:
sudo tail -f /var/log/letsencrypt/letsencrypt.log
Common errors involve incorrect API keys or DNS permissions. Ensure your API key has "Edit DNS" permissions and is not blocked by Cloudflare Firewall or other security tools.
References
For more information on Certbot and the Cloudflare DNS plugin, refer to the official documentation:
- [Certbot DNS Plugins Documentation](https://certbot.eff.org/docs/using.html#dns-plugins)
- [Cloudflare API Documentation](https://api.cloudflare.com/)
- [Comprehensive VPS Security: Guide to Deploying Coraza WAF with Traefik v3 Reverse Proxy](/en/blog/bao-mat-vps-toan-dien-huong-dan-trien-khai-coraza-waf-ket-hop-traefik-v3-reverse-proxy)
- [Optimizing Docker Infrastructure Security with Pomerium as Zero Trust Ingress Proxy](/en/blog/toi-uu-hoa-bao-mat-ha-tang-docker-trien-khai-pomerium-lam-zero-trust-ingress-proxy)
Related infrastructure & engineering guides: Cloud VPS with L3/L4/L7 Anycast Anti-DDoS · Comprehensive VPS Security: Guide to Deploying Coraza WAF with Traefik v3 Reverse Proxy
Recommended NVMe Cloud VPS Plans to Deploy This Setup
Enterprise RAID 10 NVMe SSDs, CMC & FPT District 9 uplinks with Anycast Anti-DDoS and 99.99% SLA.
VPS-3E
10% VAT included
- CPU: 2 vCPU
- RAM: 4 GB
- SSD: 50 GB NVMe
