Install a free SSL certificate in nginx with Certbot

A free certificate for nginx needs four things: a domain that points to your server, ports 80 and 443 open, a server block for that domain, and Certbot. With DNS already in place it takes about fifteen minutes on Ubuntu 24.04 or Debian 12. The sample domain is example.com and the sample address 203.0.113.10.

Updated:

Before you start: DNS and ports

Create two A records at your DNS provider (DNS management is included with any Cloudraft service):

TypeNameValue
Aexample.com (or @)203.0.113.10
Awww203.0.113.10

Remove AAAA records left over from an old host. Let's Encrypt prefers IPv6 when a record exists, so a stale one breaks validation. Check what the world sees:

sudo apt update && sudo apt install bind9-dnsutils
dig +short example.com A
dig +short www.example.com A
dig +short example.com AAAA

The first two must print your address and the third nothing. Then open the ports:

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

If the server sits behind a router in a private network, forward both ports to it.

A server block that answers on port 80

sudo apt install nginx
sudo mkdir -p /var/www/example.com
echo 'It works' | sudo tee /var/www/example.com/index.html
sudo nano /etc/nginx/sites-available/example.com
server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/example.com;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }
}
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
curl -I http://example.com/

Do not go on until curl returns 200. The certificate check uses this very path, so a problem here becomes a Certbot error a minute later.

Get the certificate

Install Certbot one way. The apt package is simple and works on both systems:

sudo apt install certbot python3-certbot-nginx

Certbot's authors recommend the snap, which is newer. Ubuntu Server has snapd; on Debian you have to install it first. If you choose the snap, remove the apt package:

sudo apt remove certbot
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/local/bin/certbot

Make a test run, then request the real certificate:

sudo certbot certonly --nginx -d example.com -d www.example.com --dry-run
sudo certbot certonly --nginx -d example.com -d www.example.com --deploy-hook "systemctl reload nginx"

certonly leaves your configuration alone, so you decide how the site looks. The hook is remembered and runs after every renewal; without it nginx could go on serving the old certificate from memory.

The final configuration: HTTPS and one address

Pick one canonical name, here without www, and send everything else to it in a single hop. Replace the file with:

# all HTTP traffic goes to HTTPS
server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://example.com$request_uri;
}

# https://www goes to the main name
server {
    listen 443 ssl http2;
    server_name www.example.com;
    ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    return 301 https://example.com$request_uri;
}

# the site
server {
    listen 443 ssl http2;
    server_name example.com;
    root /var/www/example.com;
    index index.html;
    ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;

    location / {
        try_files $uri $uri/ =404;
    }
}

listen 443 ssl http2 is the right form for the nginx versions in Ubuntu 24.04 (1.24) and Debian 12 (1.22). From 1.25.1 on, http2 on; is a separate line. Use fullchain.pem, not cert.pem; otherwise some phones will not trust the site.

For an application on a local port, replace the last location and drop root and index:

location / {
    proxy_pass http://127.0.0.1:3000;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
}

The Node.js and Python guides show the process behind that port. Apply the file and check each variant:

sudo nginx -t && sudo systemctl reload nginx
curl -sI http://example.com/ | head -n 3
curl -sI https://www.example.com/ | head -n 3
curl -sI https://example.com/ | head -n 1

Test the renewal, not only the certificate

Let's Encrypt certificates last 90 days at most, and shorter lifetimes have been announced. Both packages install a systemd timer:

systemctl list-timers | grep -i certbot
sudo certbot renew --dry-run
sudo certbot certificates
sudo grep renew_hook /etc/letsencrypt/renewal/example.com.conf

The dry run repeats validation through your new redirect, so it catches mistakes that would otherwise show up months later. Let's Encrypt no longer sends expiry emails; add a check to your monitoring:

echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null | openssl x509 -noout -enddate

Keep port 80 open permanently: renewals and visitors who type the bare domain both need it.

Errors and their causes

Message or symptomCauseFix
DNS problem: NXDOMAINNo A record, or it is too newCheck with dig, wait for the old TTL to pass
Timeout during connectPort 80 closed, wrong IP, missing port forwardingTry curl from mobile internet
unauthorized … 404Another server block or another server answeredLook for a stale AAAA record or a duplicate server_name
too many failed authorizationsRepeated attempts with a broken setupWait an hour, then test with --dry-run
CAA record … prevents issuanceDNS allows a different authority onlyAdd letsencrypt.org to the CAA record
cannot load certificate in nginx -tPath typo, or not issued yetCompare with certbot certificates
Too many redirectsA proxy in front talks HTTP to your HTTPS redirectUse HTTPS between the proxy and the server
Padlock missingImages or scripts still load over http://Fix the URLs in the site or its database

HSTS (Strict-Transport-Security) should wait until everything has run cleanly for a few weeks, because browsers remember it and you cannot take it back quickly. If the domain is moving from another host, read how to migrate a website first: the certificate has to be ready before DNS changes.

SSL and nginx questions

Is a free Let's Encrypt certificate worse than a paid one?

The encryption is the same and all modern browsers trust it. Paid certificates can include a verified company name in the details, which browsers no longer show in the address bar.

How do I get a wildcard certificate for all subdomains?

A wildcard needs the DNS challenge: Certbot must be able to create a TXT record through your DNS provider's API or plugin. For a handful of subdomains it is easier to list them with several -d options.

Can one nginx server hold certificates for many domains?

Yes. Make a separate server block and certificate for each site. Browsers send the host name during the handshake (SNI), so one IP address is enough.

Does restarting nginx for a new certificate interrupt visitors?

A reload does not. The new workers take the new certificate while old workers finish their open requests. A full restart drops connections and is not needed.

Read next

All VPS pages

This page in other languages: Azərbaycanca · Русский · Türkçe

Servers in Baku, prices in manat

Create an account, top up your balance and order the plan you need.