AWS Builder Center

Free HTTPS on EC2 with Nginx and Let's Encrypt

Add free HTTPS to your EC2 website with Nginx and Let's Encrypt. Install Certbot, get a certificate, set up auto-renewal, and fix common errors.

Series: AWS Beginners Guide (9 articles)

  1. …
  2. 8
    Free HTTPS on EC2 with Nginx and Let's Encrypt This article
Software Engineering Undergraduate | AWS | Cloud & DevOps | Full-Stack Developer
Your site loads, your domain points at the server, and then you notice the browser has put a small "Not secure" label next to the address. Visitors see it too. Some will hesitate to type anything into a form, and a few will leave.
The fix is HTTPS, which needs a TLS certificate (people still call it an SSL certificate, and the names are used interchangeably). Let's Encrypt hands them out for free, and a tool called Certbot does the paperwork. On a setup like the one in this series, the whole thing takes about ten minutes. The part worth your attention is renewal, because a certificate that silently expires is worse than none at all.



What HTTPS actually does

HTTP sends everything in plain text. Anyone sitting between your visitor and your server, such as someone on the same café Wi-Fi, can read it.
HTTPS wraps the same traffic in encryption. The certificate does two jobs: it enables that encryption, and it proves the server really belongs to the domain it claims. Browsers check that proof, and the padlock appears when it passes.
Let's Encrypt is a free certificate authority, meaning an organization browsers already trust to vouch for domains. Certbot is the program that talks to it, proves you control your domain, downloads the certificate, and edits your Nginx config to use it.



Before you start

Check these, because nearly every Certbot failure traces back to one of them:
  1. Your domain points at your server. Run  dig +short yourname.com  and confirm it prints your Elastic IP. The Route 53 article covers this.
  1. Nginx answers for that domain. Your site config needs a  server_name  line containing the domain, as set up in the Nginx article.
  1. Port 80 is open in your security group. Certbot proves ownership by placing a file on your site and letting Let's Encrypt fetch it over HTTP.
  1. Port 443 is open too. That's the HTTPS port, and you'll add it in the next step.
The domain requirement is real. This guide covers domain names, so if you're still using a bare IP address, set up Route 53 first.



Step 1: Open port 443

In the EC2 console, open your instance's security group and add one inbound rule:
  • Type: HTTPS
  • Port: 443
  • Source: Anywhere-IPv4 ( 0.0.0.0/0 )
Leave the port 80 rule in place. Certbot needs it for validation and for renewals later, and you'll use it for the redirect from HTTP to HTTPS.



Step 2: Install Certbot

Connect to your instance over SSH. The Certbot project recommends installing through snap on Ubuntu, and EC2's Ubuntu images normally include it already:
sudo apt update
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/local/bin/certbot
If an older Certbot from  apt  is already installed, remove it first with  sudo apt remove -y certbot  so the two don't clash.
Confirm it worked:
certbot --version
On Amazon Linux 2023, snap isn't available. The Certbot docs use a Python virtual environment instead:
sudo dnf install -y python3 augeas-libs
sudo python3 -m venv /opt/certbot/
sudo /opt/certbot/bin/pip install --upgrade pip
sudo /opt/certbot/bin/pip install certbot certbot-nginx
sudo ln -s /opt/certbot/bin/certbot /usr/bin/certbot
Package details shift between releases, so if a command errors out, check the current Certbot instructions for your operating system.



Step 3: Rehearse first

Let's Encrypt limits how many certificates you can request in a week, and failed attempts count toward some of those limits. So do a dry run before the real thing. It goes through the same validation without issuing anything:
sudo certbot certonly --nginx --dry-run \
-d yourname.com -d www.yourname.com
If it ends with "The dry run was successful," everything is lined up. If it fails, the error message tells you what to fix, and the troubleshooting table later in this article decodes the common ones.



Step 4: Get the real certificate

Now run it for real:
sudo certbot --nginx -d yourname.com -d www.yourname.com
Certbot asks a few questions:
  • Email address. Used for important notices about your certificates.
  • Terms of service. Agree to continue.
  • Redirect HTTP to HTTPS? Choose to redirect. Visitors who type  http://  end up on the secure version automatically.
When it finishes, it prints the location of your certificate files and the date they expire. Open  https://yourname.com  in a browser. The padlock should be there.
Both names in the command matter. A certificate only covers the names you listed. If you only include  yourname.com , then  www.yourname.com  shows a certificate warning.



What Certbot changed in your Nginx config

It's worth looking at what a tool did to your files, so open your site config:
sudo cat /etc/nginx/sites-available/mysite
You'll find new lines like these inside the server block:
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/yourname.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourname.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
And a second server block that sends port 80 traffic to HTTPS:
server {
listen 80;
server_name yourname.com www.yourname.com;
return 301 https://$host$request_uri;
}
What each piece does:
  •  listen 443 ssl  makes Nginx accept encrypted connections
  •  ssl_certificate  is the public certificate sent to browsers
  •  ssl_certificate_key  is the private key, which stays on the server and should never be shared or committed to Git
  •  return 301  permanently redirects plain HTTP visitors to HTTPS
The files live under  /etc/letsencrypt/live/ . Don't copy them elsewhere, because renewals replace them in place.



Step 5: Make sure renewal works

Here's the part that matters long-term. Let's Encrypt certificates currently last 90 days, and Let's Encrypt has announced plans to shorten that over the next couple of years, so check their site for the current figure. Either way, you're not meant to renew by hand. Certbot installs a timer that checks twice a day and renews any certificate within 30 days of expiring.
Check the timer exists:
systemctl list-timers | grep certbot
On a snap install the timer is named something like  snap.certbot.renew.timer . Then test the renewal process without changing anything:
sudo certbot renew --dry-run
You want to see the word "success" next to your domain. See what you currently have and when it expires:
sudo certbot certificates
If you want an independent check from outside the server, this prints the expiry dates the world sees:
echo | openssl s_client -connect yourname.com:443 -servername yourname.com 2>/dev/null \
| openssl x509 -noout -dates



Why renewals fail months later

Setup works, then three months pass, and the certificate quietly expires. Almost always it's one of these:
  • Port 80 got closed. Someone tidied the security group and removed "unused" rules. Renewals use port 80.
  • The domain no longer points at the server. You moved to a new instance or changed DNS.
  • The Nginx config broke. An edit introduced an error, and Certbot can't reload Nginx.
  • The Elastic IP was released, so validation reaches a different machine entirely.
The fix for all of them is the same habit: run  sudo certbot renew --dry-run  after you change anything about the server, the DNS, or the security group.



Troubleshooting

What you see
Usual cause
 Timeout during connect (likely firewall problem) 
Port 80 isn't open in the security group, so Let's Encrypt can't reach your server
 DNS problem: NXDOMAIN looking up A for yourname.com 
The A record is missing, misspelled, or hasn't spread yet
 Could not automatically find a matching server block 
Your Nginx config has no  server_name  containing that domain
 Invalid response from http://yourname.com/.well-known/acme-challenge/... 
The domain resolves to a different server, or Nginx is returning an error for that path
A rate limit error
You requested too many certificates recently, so wait and use  --dry-run  for rehearsals
Browser warning that the certificate doesn't match
The name isn't on the certificate. Re-run Certbot with every name you use, for example both  yourname.com  and  www 
Padlock shows a warning or "not fully secure"
Mixed content: the page loads images or scripts from  http://  addresses. Change them to  https://  or use relative paths
 nginx -t  fails after editing
A syntax mistake. The error includes a file name and line number



Common mistakes

Skipping the domain. Certbot validates domain names. Pointing it at an IP address, or at a domain that isn't live yet, fails.
Closing port 80 after getting the certificate. The redirect needs it, and so do renewals.
Forgetting  www . Include both names from the start.
Editing files in  /etc/letsencrypt/live/ . Those are managed by Certbot. Change your Nginx config, not the certificate folder.
Never testing renewal. A passing first-time setup says nothing about whether renewal will work in three months.
Enabling HSTS too early. Some guides tell you to add a  Strict-Transport-Security  header immediately. It tells browsers to refuse plain HTTP for a long time, which is painful if you later break your certificate. Add it once everything is stable.



A note on AWS Certificate Manager

If you've browsed AWS, you've seen AWS Certificate Manager (ACM), which also issues free certificates. It's tempting to use it here, but ACM public certificates can't be installed directly on an EC2 instance. They work with AWS services like Application Load Balancers and CloudFront.
So the rule of thumb is simple. A single EC2 instance serving its own traffic uses Certbot. If you later put a load balancer in front of your servers, the certificate moves to the load balancer through ACM, and Certbot's job ends.



Quick reference

sudo snap install --classic certbot # install
sudo certbot certonly --nginx --dry-run -d site.com # rehearse
sudo certbot --nginx -d site.com -d www.site.com # get certificate
sudo certbot certificates # list and check expiry
sudo certbot renew --dry-run # test renewal
  • Ports 80 and 443 both open in the security group
  • Domain points at the Elastic IP before you start
  • List every name you use, including  www 
  • Renewal runs automatically, but test it after any server change



Where this leaves you

If you've followed the series, you now have a full path: launch an instance, connect over SSH, lock it down with a security group, give it a fixed address, point a domain at it, serve a site with Nginx, and encrypt the traffic. That's a complete small deployment.
The next sensible topics are backups through EBS snapshots and a billing alert so the bill never surprises you. For tonight, run  sudo certbot renew --dry-run . If it passes, you won't need to think about certificates again until something moves.


Certbot throwing an error you can't decode? Drop a comment with the exact message and the output of  dig +short yourname.com , and I'll help you trace it.

Series: AWS Beginners Guide (9 articles)

  1. …
  2. 8
    Free HTTPS on EC2 with Nginx and Let's Encrypt This article
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article