How to Set Up a Free SSL Certificate for a Subdomain on Nginx
A free SSL certificate subdomain Nginx setup takes about fifteen minutes once your DNS record is in place. You do not need to buy a certificate, and you do not need to change how your main domain works. This guide walks through the whole job on Ubuntu 24.04 with Nginx and Certbot: point the subdomain at your server, add a server block, run Certbot, and turn on auto-renewal.
Free SSL Certificate Subdomain Nginx: What You Need Before You Start
Before you run any command, make sure you have four things ready. First, a server running Nginx on Ubuntu 24.04, with sudo access. Second, a domain you control, with access to its DNS settings. Third, the subdomain you want to secure, for example app.example.com. Fourth, Nginx already serving at least one site, so you know the web server works.
Let’s Encrypt issues free certificates through an automated process, and Certbot is the tool that talks to it on your behalf. If you already have a certificate on your main domain, the subdomain still needs its own. A certificate covers only the exact names listed on it, so example.com does not cover app.example.com. If you have worked through a guide to installing a free Let’s Encrypt certificate on Nginx before, the steps here will feel familiar. The difference is that every step targets the subdomain instead of the root domain.
Point the Subdomain DNS Record at Your VPS First
Add an A record for the subdomain at your DNS provider and point it at your server’s public IPv4 address.
Type: A
Name: app
Value: 203.0.113.10
TTL: 300
DNS changes take time to spread. Check the record from your server before you move on:
dig +short app.example.com
curl -I http://app.example.com
If dig returns your server IP, the record is live. If it returns nothing, wait a few minutes and check again. Do not run Certbot until the subdomain resolves, because the HTTP-01 challenge will fail and Let’s Encrypt will count the failed attempt against its rate limit. The same DNS-first rule applies when you point a new domain to your VPS and set up free HTTPS.
Set Up the Nginx Server Block for the Subdomain
Certbot needs a working Nginx server block for the subdomain so it can place its challenge file where Let’s Encrypt can fetch it. Create a new file for the subdomain, separate from your main domain config.
sudo nano /etc/nginx/sites-available/app.example.com
Add a plain HTTP server block. At this stage there is no SSL yet, so the block listens on port 80 only:
server {
listen 80;
server_name app.example.com;
root /var/www/app.example.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
Create the web root, add a small test page, then enable the site and reload Nginx:
sudo mkdir -p /var/www/app.example.com
echo "Subdomain is up" | sudo tee /var/www/app.example.com/index.html
sudo ln -s /etc/nginx/sites-available/app.example.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
Open http://app.example.com in a browser. You should see the test page. If not, check the server name and the DNS record again before running Certbot. This follows the same pattern as Nginx server blocks on Ubuntu 24.04, just with a different server name and root.
Install Certbot and Get the Certificate
Install Certbot and its Nginx plugin from the Ubuntu repositories. The plugin edits your Nginx config for you, which saves manual work and avoids typos in the SSL lines.
sudo apt update
sudo apt install certbot python3-certbot-nginx -y
Now ask Certbot for a certificate for the subdomain. The --nginx flag tells it to use the Nginx plugin, and -d names the domain to cover:
sudo certbot --nginx -d app.example.com
Certbot asks for an email address, which Let’s Encrypt uses for expiry warnings, and asks you to agree to the terms. Then it runs the HTTP-01 challenge: it places a temporary file under /var/www/app.example.com/.well-known/acme-challenge/, and Let’s Encrypt fetches it over HTTP to confirm you control the subdomain. If the fetch works, Certbot saves the certificate files and rewrites your server block to listen on port 443 with SSL. It also adds a redirect from HTTP to HTTPS, unless you tell it not to.
The certificate files live under /etc/letsencrypt/live/app.example.com/:
/etc/letsencrypt/live/app.example.com/fullchain.pem
/etc/letsencrypt/live/app.example.com/privkey.pem
Your server block now includes lines like these, added by Certbot:
server {
listen 443 ssl;
server_name app.example.com;
ssl_certificate /etc/letsencrypt/live/app.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/app.example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
root /var/www/app.example.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
Check the Certificate and Test HTTPS
Open https://app.example.com in a browser and look for the padlock. Then verify from the command line, which shows you the dates and the issuer without browser caching getting in the way:
echo | openssl s_client -connect app.example.com:443 -servername app.example.com 2>/dev/null | openssl x509 -noout -dates -issuer -subject
The output should show Issuer: C = US, O = Let's Encrypt, a subject matching your subdomain, and a notAfter date about 90 days out. You can also run a quick curl check:
curl -I https://app.example.com
A HTTP/2 200 response means the site serves over HTTPS. If curl reports a certificate error, check that the server name in your Nginx config matches the subdomain exactly. The names must match, letter for letter.
Set Up Auto-Renewal So the Certificate Never Expires
A free SSL certificate subdomain Nginx setup only stays healthy if renewal works without you. Certbot installs a systemd timer or a cron job when you install it, and that timer tries to renew certificates twice a day. Check that the timer exists and is active:
sudo systemctl status certbot.timer
sudo certbot renew --dry-run
The dry run simulates a renewal without changing your live certificate. If it passes, the timer will handle real renewals on its own. A guide to renewing Let’s Encrypt certificates automatically covers the timer and the logs.
Certbot only renews certificates within 30 days of expiry, so a dry run on a fresh certificate will report that renewal is not due yet. That is normal. If you ever change the web root or move the site, run the dry run again, since renewal depends on the same HTTP-01 challenge path.
Single-Subdomain Certificate vs Wildcard Certificate
A single-subdomain certificate covers one exact name, like app.example.com. A wildcard certificate covers every subdomain at the same level, like *.example.com. The trade-off: a wildcard needs DNS validation, so Certbot must create a TXT record through your DNS provider’s API.
| Feature | Single-subdomain certificate | Wildcard certificate |
|---|---|---|
| Names covered | One exact subdomain | All subdomains at one level |
| Validation method | HTTP-01 challenge | DNS-01 challenge |
| DNS API access needed | No | Yes |
| Setup effort | Low, one command | Higher, API credentials needed |
| Best for | One or two subdomains | Many subdomains, or new ones added often |
Pick the single-subdomain certificate when you have one or two subdomains and want the simplest setup. Pick the wildcard when you run many subdomains, or when you add new ones often and do not want to run Certbot each time. A full walkthrough of the DNS method is in the guide on setting up Let’s Encrypt wildcard certificates with DNS validation. Note that *.example.com does not cover example.com itself, so request both names if you need both.
Fix the Most Common Errors
Most failures in this process come from three causes, and each one has a clear fix.
DNS has not spread yet
Wait ten minutes, check with dig +short, and try again. Lower the TTL before your next change so the wait is shorter.
Port 80 is blocked
The HTTP-01 challenge needs port 80 open to the internet. Check UFW with sudo ufw status, and check your VPS provider’s cloud firewall too, since either one can block the port.
Nginx fails to reload after Certbot edits the config
Run sudo nginx -t and read the error. The usual cause is a duplicate listen 443 ssl line from running Certbot twice, or two server blocks claiming the same name. Remove the duplicate, test again, and reload.
Let’s Encrypt allows five failed validations per hostname per hour, so if you hit that limit, use the hour to check DNS, the firewall, and the server block before trying again.
Once the certificate is live and the renewal timer is active, the job is done. Your subdomain serves over HTTPS, the certificate renews itself, and you can repeat the same steps for the next subdomain in a few minutes. Keep the server block file per subdomain, keep port 80 open for renewals, and run the dry run again any time you change the site.