Beginner

Free SSL with Let's Encrypt on Nginx (Ubuntu)

Get a certificate with Certbot, renew it automatically and fix a failed challenge

On this page
  1. Check DNS first
  2. Install Nginx and make a site
  3. Get the certificate with Certbot
  4. Test renewal
  5. Put Nginx in front of your app
  6. Certbot troubleshooting
  7. FAQ

Let's Encrypt issues SSL certificates for free, and Certbot sets up Nginx and renewal for you. Before you start, you need a domain that points to your server, with ports 80 and 443 open.

Check DNS first#

Terminal
dig +short YOUR_DOMAIN
curl -4 -s ifconfig.me

Both commands must print the same IP. If they don't, add an A record for the domain and wait for DNS to update. Delete any AAAA record you don't use, because Let's Encrypt may check over IPv6.

Install Nginx and make a site#

Terminal
sudo apt update && sudo apt install -y nginx
sudo mkdir -p /var/www/YOUR_DOMAIN
echo "ok" | sudo tee /var/www/YOUR_DOMAIN/index.html
/etc/nginx/sites-available/YOUR_DOMAIN
server {
    listen 80;
    listen [::]:80;
    server_name YOUR_DOMAIN;
    root /var/www/YOUR_DOMAIN;
    index index.html;
}
Terminal
sudo ln -s /etc/nginx/sites-available/YOUR_DOMAIN /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
sudo ufw allow 80,443/tcp

On XVer, also check that the server's Security Group allows inbound TCP 80 and 443.

Get the certificate with Certbot#

Terminal
sudo snap install --classic certbot
sudo ln -sf /snap/bin/certbot /usr/bin/certbot
sudo certbot --nginx -d YOUR_DOMAIN -m YOUR_EMAIL --agree-tos --redirect

To cover www as well, point its DNS at the server, add -d www.YOUR_DOMAIN, and put that name in server_name too.

Test renewal#

Terminal
sudo certbot renew --dry-run
systemctl list-timers | grep certbot

Keep port 80 open for good: HTTP-01 renewal needs it.

Put Nginx in front of your app#

If your app runs on port 3000, change the 443 block to this

nginx
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;
}

Certbot troubleshooting#

Error Cause Fix
Timeout during connect Port 80 is closed Open it in both ufw and the Security Group
NXDOMAIN The DNS record doesn't exist yet Add the A record and wait
unauthorized with a 404 The domain points to another server, or server_name is wrong Check DNS and Nginx
too many certificates You hit a Let's Encrypt rate limit Wait for the window to pass, and test with --dry-run

FAQ#

How is free SSL different from a paid certificate?

The encryption is the same. A Let's Encrypt certificate only proves you control the domain. Some paid certificates also verify the organisation and come with a contractual warranty.

How long does a Let's Encrypt certificate last?

A standard one lasts 90 days. Certbot renews it automatically before it expires, which is why the dry-run test should pass.

Can I get SSL for an IP address without a domain?

A domain is simpler and far better supported by Certbot. Use a domain or subdomain that points to the server.

Follow the guide on your own server

Create a Cloud Server with Ubuntu, Debian or Windows Server, billed by the hour in Thai baht. Support answers 24/7 in Thai and English.