How-to

Free HTTPS for nginx with Let's Encrypt

certbot proves you control a domain, fetches a free 90 day certificate from Let's Encrypt and renews it automatically. On nginx the whole setup takes a few minutes.

What do I need before I start?

A domain whose A record points at the public IPv4 of your machine, port 80 reachable from the internet (the HTTP-01 check uses it), and nginx serving a server block with the right server_name. Check DNS from outside with dig +short www.example.com; it must return your machine's address. If you run a firewall, make sure 80 and 443 are open; iptables basics shows a minimal rule set.

How do I install certbot?

The certbot project recommends its snap package, which stays current on its own:

sudo snap install --classic certbot
sudo ln -sf /snap/bin/certbot /usr/local/bin/certbot

Distribution packages work too and fit better if you avoid snaps: sudo apt install certbot python3-certbot-nginx on Ubuntu and Debian, or sudo dnf install certbot python3-certbot-nginx on Rocky Linux and AlmaLinux after enabling EPEL. Use one install method, not both.

Which method should I use?

MethodCommandEdits nginx config?Best for
nginx plugincertbot --nginxYes, adds TLS linesQuick setup on simple sites
Webrootcertbot certonly --webrootNoHand-tuned configs, config in git
DNS-01certbot certonly --dns-...NoWildcards, servers not reachable on 80

How do I use the nginx plugin?

sudo certbot --nginx --agree-tos --redirect -m hostmaster@example.com -d example.com,www.example.com

certbot finds the server block with those names, answers the challenge through nginx, installs the certificate, adds the TLS directives and, with --redirect, a 301 from HTTP to HTTPS. Then it reloads nginx. Open the site in a browser; the padlock should be there.

How do I use webroot and keep my config hand-written?

Serve the challenge folder from the port 80 block, issue the certificate, then write the TLS block yourself:

location ^~ /.well-known/acme-challenge/ { root /var/lib/letsencrypt/webroot; default_type text/plain; }
sudo mkdir -p /var/lib/letsencrypt/webroot
sudo certbot certonly --webroot -w /var/lib/letsencrypt/webroot -d example.com -d www.example.com

Then reference the files in your 443 server block:

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;
ssl_session_cache shared:TLS:10m;

Always use fullchain.pem, not cert.pem; without the intermediate certificate some clients fail to verify the chain.

Does renewal really happen by itself?

Yes. The snap installs a systemd timer and the distribution packages install a timer or cron job that runs twice a day and renews anything within 30 days of expiry. Confirm it once:

systemctl list-timers 'snap.certbot*' 'certbot*'
sudo certbot renew --dry-run

With the webroot method nginx must reload to pick up a renewed file. Add a deploy hook so that happens only when something actually renewed:

echo -e '#!/bin/sh\nsystemctl reload nginx' | sudo tee /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh
sudo chmod 755 /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh

Can certbot issue a wildcard name such as *.example.com?

Wildcards such as *.example.com require the DNS-01 challenge: certbot creates a TXT record under _acme-challenge. With a DNS plugin for your provider and an API token stored in a root-only file, it runs unattended. Without a plugin you can add the record by hand with --manual --preferred-challenges dns, but then renewal is manual too, so automate it before the 90 days are up.

What goes wrong most often?

  • Challenge returns 404. Another server block catches the request, or a redirect to HTTPS runs before the challenge location. Put the ACME location in the port 80 block that matches the name.
  • Timeout during connect. Port 80 is closed in the firewall, or DNS still points somewhere else.
  • Too many certificates already issued. Let's Encrypt has rate limits per domain each week. Test with --staging while experimenting.
  • Old certificate still served. nginx was not reloaded after renewal; add the deploy hook above.

Certificates cost nothing and certbot uses next to no CPU, so even the smallest machine, such as E1, can host many HTTPS sites. For nginx installation see Install nginx on Ubuntu 22.04; for certificates in containers see nginx reverse proxy in Docker; and for how TLS relates to SSH keys see SSH vs SSL/TLS.

Low-Cost Machines for HTTPS Sites

ServerProcessorCores / threadsMemoryStorageNetworkPer monthSmallest order
E1Intel Xeon E3-1240 v34 / 816 GB DDR32x 240 GB SSD2x 1 Gbps$30.52$91.56 (3 months)
E2Intel Xeon E3-1270 v34 / 832 GB DDR31x 480 GB SSD2x 1 Gbps$38.32$114.96 (3 months)
S1Intel Xeon E-2276G6 / 1264 GB DDR41x 1 TB NVMe2x 10 Gbps$53.32$53.32 (1 month)
S2Intel Xeon E-2356G6 / 1264 GB DDR41x 1 TB NVMe2x 10 Gbps$58.39$58.39 (1 month)

Keep Reading

Install nginx on Ubuntu 22.04

Install nginx on Ubuntu 22.04 from the archive or the upstream repository, add a server block and open the firewall in a few minutes.

Open

SSH vs SSL/TLS

Both encrypt traffic, but SSH runs your admin sessions while TLS protects websites and APIs; here is where each fits.

Open

nginx reverse proxy in Docker

Run nginx in a container as the front door for other containers, route by host name, pass real client IPs and add TLS.

Open

curl and certificate errors

When -k is acceptable, why it is risky, and better fixes: trusting a private CA, pinning a key, testing by IP with --resolve and repairing chains.

Open

FAQ

Quick Answers

90 days. certbot renews it automatically when 30 days or less remain.

For the HTTP-01 challenge, yes. The DNS-01 challenge works without port 80 and is required for wildcard names.

fullchain.pem. It includes the intermediate certificate that browsers and API clients need to build the chain.

Run sudo certbot renew --dry-run. It goes through the full process against the staging service.

Yes. Pass several -d options, up to 100 names, or use a wildcard with DNS validation.