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
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.
Type your own values and the commands on this page follow
Check DNS first#
dig +short YOUR_DOMAIN
curl -4 -s ifconfig.meBoth 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#
sudo apt update && sudo apt install -y nginx
sudo mkdir -p /var/www/YOUR_DOMAIN
echo "ok" | sudo tee /var/www/YOUR_DOMAIN/index.htmlserver {
listen 80;
listen [::]:80;
server_name YOUR_DOMAIN;
root /var/www/YOUR_DOMAIN;
index index.html;
}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/tcpOn XVer, also check that the server's Security Group allows inbound TCP 80 and 443.
Get the certificate with
Certbot#
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 --redirectTo 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#
sudo certbot renew --dry-run
systemctl list-timers | grep certbotKeep 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
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 |
too many certificates |
You hit a |
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.