
If you're managing multiple subdomains for a web application, configuring SSL certificates individually can quickly become difficult to maintain.
Imagine you're running a SaaS platform with domains such as:
app.example.comapi.example.comadmin.example.comclient1.example.comclient2.example.com
You could configure a separate SSL certificate for each subdomain, but as the number of subdomains increases, certificate management becomes another task you need to handle.
This is where wildcard SSL certificates become useful.
A wildcard certificate allows you to secure multiple subdomains using a single certificate. Instead of requesting a separate certificate for every tenant or application, you can use a certificate for *.example.com.
However, there's one important detail: Let's Encrypt doesn't issue wildcard certificates using the standard HTTP-01 validation method. You need to prove domain ownership through DNS-01 validation.
If your DNS is managed by Cloudflare, you can automate this process using the Cloudflare API and Certbot.
In this guide, we'll configure a wildcard certificate using Let's Encrypt, Certbot, the DNS-01 challenge, and Cloudflare's API. We'll also cover certificate renewal, Nginx configuration, common errors, and security practices that are worth following in a production environment.
1. What Is a Wildcard SSL Certificate?
A wildcard SSL/TLS certificate is a certificate that covers subdomains matching a wildcard name.
For example:
*.example.comThis can cover:
app.example.com
api.example.com
admin.example.com
shop.example.comThe main advantage is that you don't need a separate certificate for every matching subdomain.
This is especially useful for SaaS applications, multi-tenant platforms, staging environments, and systems where new subdomains are created dynamically.
For example, suppose you're building a platform where each business receives its own subdomain:
acme.example.com
brighttech.example.com
demo.example.comA wildcard certificate can secure these subdomains without requiring you to request a new certificate whenever a new tenant is added.
An important limitation
A wildcard certificate for *.example.com does not automatically cover the root domain example.com.
It also doesn't cover nested subdomains such as:
api.dev.example.comFor these cases, you need additional names on the certificate or a separate certificate covering the required hostname.
If you want to secure both the root domain and its first-level subdomains, request a certificate containing both names:
example.com
*.example.comWe'll use this configuration throughout the guide.
2. What Is Let's Encrypt?
Let's Encrypt is a certificate authority that provides free TLS certificates.
It allows developers to enable HTTPS without purchasing a traditional SSL certificate.
Let's Encrypt works with ACME clients, which automate the process of requesting, validating, issuing, and renewing certificates.
One popular ACME client is Certbot.
Certbot can obtain certificates and, when configured with the appropriate plugin, automate domain validation and renewal.
For wildcard certificates, we need DNS-01 validation.
You can learn more in the official Let's Encrypt challenge documentation.
3. What Is the DNS-01 Challenge?
Before issuing a certificate, Let's Encrypt needs to verify that you control the domain.
There are different ways to complete this verification.
The HTTP-01 challenge verifies that a specific file is accessible through your website. This works well for many standard websites, but it cannot be used to issue wildcard certificates.
The DNS-01 challenge works differently.
Instead of placing a file on your server, the ACME client asks you to create a specific TXT record in your domain's DNS.
For example, Let's Encrypt may ask for a record similar to this:
Type: TXT
Name: _acme-challenge.example.com
Value: a-generated-validation-tokenThe value above is illustrative; Certbot generates the actual validation value.
Let's Encrypt checks the TXT record to verify domain control. If the expected value is available, the validation can succeed.
Why is DNS-01 required for wildcard certificates?
A wildcard certificate covers subdomains that may not exist when the certificate is issued.
For example, you might want a certificate for *.example.com before creating any tenant subdomains.
DNS validation proves control of the domain without requiring every subdomain to have a working website.
It also means that the server requesting the certificate doesn't necessarily need to be publicly accessible over HTTP on port 80.
However, the DNS records must be published correctly and be visible to the validating certificate authority.
For more information, see the official Let's Encrypt DNS-01 documentation.
4. Why Use Cloudflare API for DNS Validation?
You can create the required TXT record manually through the Cloudflare dashboard, but that becomes inconvenient when certificates need to be renewed regularly.
Let's Encrypt certificates have a limited validity period, so you should plan for automated renewal rather than relying on manual intervention.
Cloudflare provides an API that allows authorized applications to manage DNS records.
With the Certbot Cloudflare plugin, the process becomes automatic:
Certbot requests a certificate.
The plugin creates the required DNS TXT record through Cloudflare's API.
Certbot waits for DNS propagation.
Let's Encrypt verifies the record.
The certificate is issued.
The plugin removes the temporary TXT record.
The same integration can be used during renewal.
This is useful because you don't have to manually create DNS records every time the certificate needs to be renewed.
The important requirement is that your domain's authoritative DNS must be managed by Cloudflare for this configuration to work as described.
If Cloudflare is only being used as a proxy while another provider hosts your authoritative DNS, you'll need to configure the DNS challenge with the actual DNS provider or delegate the challenge appropriately.
5. Prerequisites Before Starting
Before configuring the certificate, make sure you have the following:
A registered domain, such as
example.com.A Cloudflare account with the domain added.
Cloudflare authoritative nameservers configured for the domain.
A Linux server with administrative access.
Certbot installed or available for installation.
The Certbot Cloudflare DNS plugin.
Permission to create a restricted Cloudflare API token.
You should also know where the certificate will be used.
For example, you might install it on an Nginx server that handles HTTPS traffic for multiple subdomains.
If you're using Docker, Kubernetes, a managed hosting platform, or a control panel such as Plesk, the certificate deployment process may differ. The DNS validation concept remains the same, but you must ensure that the resulting certificate and renewal process integrate correctly with your environment.
In this guide, we'll assume you're using a Linux server and Nginx.
6. Step 1: Create a Cloudflare API Token
The first step is to create an API token that allows Certbot to manage DNS validation records.
You should use a restricted API token instead of your Cloudflare Global API Key.
A Global API Key provides much broader access than Certbot needs. If it is leaked, the consequences can be much more serious.
Create the token
Sign in to your Cloudflare dashboard.
Open the API Tokens section.
Choose the option to create a custom token.
Give it a descriptive name, such as
Let's Encrypt DNS Validation.Configure the required permissions.
Restrict the token to the specific zone you want to manage.
Create the token and copy its value securely.
For the Certbot Cloudflare plugin, the essential permission is:
Zone → DNS → EditRestrict the token to the relevant domain rather than granting access to every zone in your account.
Depending on the current Cloudflare interface and plugin requirements, you may also need read access to the relevant zone so the plugin can identify it. If so, grant only the additional permission required by your setup.
The official Cloudflare API token documentation explains the available permissions.
Important: Cloudflare may show the token value only once. Store it securely and never paste it into a public repository, screenshot, or blog post.
7. Step 2: Install Certbot and the Cloudflare DNS Plugin
Next, install Certbot and the Cloudflare plugin on your server.
The exact installation method depends on your operating system and how Certbot is packaged.
On Debian or Ubuntu systems where the relevant packages are available through APT, you can generally use:
sudo apt update
sudo apt install certbot python3-certbot-dns-cloudflareThis installs Certbot and the DNS plugin from your configured package repositories.
However, package availability and versions vary by distribution. If the plugin package isn't available or your Certbot installation comes from a different packaging method, follow the official Certbot installation guidance for your operating system.
Official resources:
Verify the installation
Run:
certbot --versionYou should see the installed Certbot version.
Then check whether the Cloudflare plugin is available:
sudo certbot pluginsLook for the dns-cloudflare plugin in the output.
If the plugin doesn't appear, confirm that it was installed into the same environment as Certbot. This is particularly important when using virtual environments, Snap installations, or Docker images.
8. Step 3: Create the Cloudflare Credentials File
Certbot needs access to the Cloudflare API token so it can create and remove TXT records.
We'll store the token in a dedicated credentials file.
Create a directory for the credentials:
sudo mkdir -p /etc/letsencryptCreate the file:
sudo nano /etc/letsencrypt/cloudflare.iniAdd the following configuration:
dns_cloudflare_api_token = YOUR_CLOUDFLARE_API_TOKENReplace YOUR_CLOUDFLARE_API_TOKEN with the token you generated in Cloudflare.
Do not include quotation marks around the token unless the plugin documentation specifically requires them.
Save the file and restrict its permissions:
sudo chmod 600 /etc/letsencrypt/cloudflare.iniThe credentials file should be accessible only to the appropriate privileged account.
If the file is owned by another user, make sure its ownership and access permissions match the way Certbot is executed. For a root-run Certbot installation, you can use:
sudo chown root:root /etc/letsencrypt/cloudflare.ini
sudo chmod 600 /etc/letsencrypt/cloudflare.iniWhy are these permissions important?
The API token grants the ability to modify DNS records within the scope of its permissions.
Someone who obtains the token may be able to manipulate DNS records or complete DNS validation for certificates associated with the permitted zone.
Therefore, don't store this file inside your application source code or a publicly accessible directory.
Also avoid committing it to Git.
If a token is accidentally exposed, revoke it in Cloudflare and create a replacement.
9. Step 4: Request a Wildcard Certificate
Now that the Cloudflare plugin and credentials are configured, you can request your certificate.
Run the following command:
sudo certbot certonly \
--dns-cloudflare \
--dns-cloudflare-credentials /etc/letsencrypt/cloudflare.ini \
--dns-cloudflare-propagation-seconds 30 \
-d example.com \
-d '*.example.com'Replace example.com with your actual domain.
Let's break down the command.
certonly
Requests and obtains the certificate without automatically configuring your web server.
This gives you control over how the certificate is installed and used.
--dns-cloudflare
Tells Certbot to use the Cloudflare DNS plugin for domain validation.
--dns-cloudflare-credentials
Specifies the location of the API credentials file.
--dns-cloudflare-propagation-seconds 30
Tells the plugin to wait 30 seconds before asking the ACME server to validate the DNS record.
The value is an example, not a universal requirement. If DNS propagation takes longer, increase the delay based on your environment.
-d example.com
Requests coverage for the root domain.
-d '*.example.com'
Requests wildcard coverage for first-level subdomains.
The quotation marks around the wildcard prevent your shell from interpreting the asterisk as a filename pattern.
What happens after you run the command?
Certbot contacts the ACME service, receives the validation challenge, and uses the Cloudflare API to create the necessary TXT records.
Let's Encrypt checks the DNS records.
If validation succeeds, Certbot downloads the issued certificate and stores it under its certificate directory, usually:
/etc/letsencrypt/live/example.com/The certificate files commonly include:
fullchain.pem
privkey.pemThe exact directory name depends on the certificate lineage created by Certbot.
The fullchain.pem file contains the server certificate and intermediate certificates needed by the TLS configuration.
The privkey.pem file contains the private key. Keep it protected and never expose it publicly.
What if the command fails?
Don't immediately rerun the request several times.
Read the error message first. It may indicate an invalid token, incorrect zone permissions, an unavailable plugin, or a DNS propagation problem.
If you're still troubleshooting, consider using Let's Encrypt's staging environment for testing to avoid unnecessarily consuming production issuance limits.
10. Step 5: Verify the Certificate
After Certbot reports success, verify the certificate before configuring your web server.
You can inspect the certificate details with:
sudo openssl x509 \
-in /etc/letsencrypt/live/example.com/fullchain.pem \
-noout \
-subject \
-issuer \
-dates \
-ext subjectAltNameCheck the output for the expected domain names:
DNS:example.com
DNS:*.example.comThe exact formatting may vary, but both names should appear in the certificate's Subject Alternative Name extension.
You can also inspect the certificate information with:
sudo certbot certificatesThis lists the certificates managed by Certbot, their domains, expiry dates, and relevant paths.
Don't assume that obtaining a certificate automatically makes your website use it. You still need to configure the web server or hosting platform to serve the certificate.
11. Step 6: Configure Nginx to Use the Certificate
Suppose you want Nginx to serve HTTPS traffic for:
example.com
app.example.com
api.example.comYou can configure an HTTPS server block that uses the wildcard certificate.
A simplified Nginx configuration looks like this:
server {
listen 443 ssl;
server_name example.com *.example.com;
ssl_certificate
/etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key
/etc/letsencrypt/live/example.com/privkey.pem;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}This example forwards HTTPS requests to an application listening on port 3000.
Change the upstream address and port to match your application.
If different subdomains serve different applications, you may need separate server blocks or another routing configuration. A wildcard certificate provides TLS coverage; it does not automatically route traffic to the correct application.
You should also configure an HTTP server block on port 80 to redirect requests to HTTPS, if appropriate for your application.
Test the Nginx configuration
Before reloading Nginx, check the configuration:
sudo nginx -tIf the test succeeds, reload Nginx:
sudo systemctl reload nginxIf the configuration test fails, fix the reported issue before reloading.
A note about Cloudflare's proxy
If Cloudflare's proxy is enabled for your DNS records, visitors may connect to Cloudflare first, and Cloudflare will then connect to your origin server according to your SSL/TLS configuration.
The certificate discussed in this guide is installed on the origin server.
Configure Cloudflare's SSL/TLS mode appropriately for your origin certificate and ensure that HTTPS is working between Cloudflare and your server. A valid origin certificate alone does not guarantee that every connection in the full request path is configured correctly.
12. Step 7: Configure Automatic Certificate Renewal
Obtaining a certificate is only part of the job.
You also need to make sure it renews before expiry.
Let's Encrypt certificates are short-lived, so automated renewal is important for production applications.
Certbot typically installs or uses a scheduled renewal mechanism, depending on how it was installed.
First, test the renewal process:
sudo certbot renew --dry-runThis performs a renewal test using the configured renewal settings.
If the test succeeds, Certbot has demonstrated that it can attempt the renewal process without issuing a production replacement certificate.
Why does the credentials file matter during renewal?
Certbot stores the relevant renewal configuration, including the path to the Cloudflare credentials file.
During renewal, the plugin needs that file to create the required DNS TXT records.
If the file has been moved, deleted, or made inaccessible, automatic renewal may fail.
This is another reason to keep the credentials file in a stable, protected location.
Reload Nginx after renewal
A renewed certificate needs to be loaded by the web server before it can be served to clients.
Depending on your installation, Certbot may already have a suitable renewal hook or integration.
If it doesn't, you can configure a deploy hook to reload Nginx after a successful renewal:
sudo certbot renew \
--deploy-hook "systemctl reload nginx"This command attempts renewal and runs the hook after a certificate is successfully renewed.
For a persistent production setup, configure the deploy hook in the appropriate Certbot renewal configuration or scheduled renewal mechanism rather than relying on manually running this command.
Before deploying, verify that Nginx can reload successfully and that the certificate paths remain correct.
13. Common Problems and How to Fix Them
Even when the configuration looks correct, DNS validation can fail.
Here are some common problems worth checking.
Problem 1: Invalid Cloudflare API Token
Certbot may report an authentication error when trying to access Cloudflare.
Possible causes include:
The token was copied incorrectly.
The token has been revoked.
The token lacks the required DNS permission.
The token is restricted to the wrong zone.
The installed plugin or its dependencies are incompatible with the token configuration.
Check the token permissions in Cloudflare and confirm that the token can manage DNS records for the intended domain.
Use a restricted token rather than switching to a Global API Key as a quick fix.
Problem 2: DNS Propagation Takes Too Long
Sometimes Certbot creates the TXT record, but Let's Encrypt cannot find it when validation runs.
This can happen when DNS changes take longer than expected to become visible.
Increase the propagation wait:
--dns-cloudflare-propagation-seconds 60The correct delay depends on the DNS environment.
You can also inspect the TXT record using a DNS lookup tool:
dig TXT _acme-challenge.example.comThe command may return one or more TXT values.
During certificate validation, don't manually delete the challenge record while Certbot is still using it.
Problem 3: The Wildcard Certificate Doesn't Cover the Root Domain
If your certificate includes only:
*.example.comit won't cover:
example.comRequest both names if your application needs both:
-d example.com -d '*.example.com'Problem 4: Nginx Shows the Wrong Certificate
If your browser reports a certificate mismatch, check which certificate Nginx is actually serving.
Possible causes include:
The wrong certificate path is configured.
Another server block matches the hostname.
Nginx hasn't been reloaded after a successful renewal.
Cloudflare is presenting a different edge certificate.
DNS points to a different server.
Inspect the active Nginx configuration and verify the certificate served for the exact hostname.
Problem 5: Automatic Renewal Fails
Run:
sudo certbot renew --dry-runThen inspect the output for authentication, plugin, permissions, or DNS validation errors.
Check that the credentials file still exists and that Certbot can read it.
Also verify that your scheduled renewal mechanism is active and that successful renewal is followed by any required service reload.
A certificate that was successfully issued once is not proof that future renewals will succeed.
14. Security Best Practices for Production
When configuring wildcard certificates, it's worth spending a little extra time on security.
A wildcard certificate can cover many subdomains, so the private key may be used across several services. Protecting that key is particularly important.
Here are some practical recommendations.
Use a restricted Cloudflare API token. Grant only the permissions needed for DNS validation and limit the token to the relevant zone.
Protect the credentials file. Keep it outside your public application directories and restrict its permissions.
Protect the certificate private key. Never commit privkey.pem to Git, place it in a public directory, or expose it through an application endpoint.
Monitor certificate expiry. Configure monitoring or alerts so that renewal failures are discovered before they affect users.
Test renewal regularly. Use Certbot's dry-run option to test the configured renewal process.
Keep software updated. Maintain supported versions of Certbot, the Cloudflare plugin, and your web server.
Separate responsibilities where appropriate. If different applications or teams manage different environments, consider whether separate certificates or restricted credentials would reduce risk.
Review access to production servers. Limit who can read the private key or execute certificate management commands.
Remember that wildcard certificates are convenient, but they also increase the impact of a compromised private key. A certificate covering many subdomains should be handled carefully.
Conclusion
Configuring a wildcard Let's Encrypt certificate with Cloudflare DNS validation is a practical way to simplify SSL management for applications that use multiple subdomains.
The DNS-01 challenge allows Let's Encrypt to verify domain ownership without requiring every subdomain to host a separate validation page. Cloudflare's API automates the creation and removal of the necessary TXT records, while Certbot handles certificate issuance and renewal.
The process involves creating a restricted Cloudflare API token, installing Certbot and its DNS plugin, configuring a protected credentials file, requesting the wildcard certificate, and connecting the resulting certificate to your web server.
But don't stop once the certificate has been issued.
Verify the certificate names, configure your web server correctly, test automatic renewal, and make sure the renewed certificate is loaded after a successful renewal. Protect the API token and private key as carefully as you would other production credentials.
For applications with dynamic subdomains, such as SaaS platforms and multi-tenant systems, this setup can save a significant amount of certificate management work.
My recommendation is to automate certificate issuance and renewal from the beginning rather than treating SSL as a one-time configuration task. A properly configured renewal process, combined with restricted API access and expiry monitoring, makes the setup much easier to maintain over time.
Technical author and software engineer at DevByte. Writing deep-dive tutorials on distributed systems, modern full-stack web architectures, and cloud deployments.


