Chapter 3 · Lesson 3 of 22
Domains and HTTPS

HTTPS and Let's Encrypt: get a free certificate

HTTPS is HTTP over TLS; a certificate proves the server owns the domain, and Let's Encrypt issues them for free through an automated check.

By the end you will

Get a free Let's Encrypt certificate for your domain with certbot.

Time
22 min
Level
Beginner
Check
4 questions
You need
  • A domain whose A record points at your server
  • Ports 80 and 443 open

How HTTPS sets up a secure connection

HTTPS starts with a handshake: the server presents a certificate, the browser checks it was issued by an authority it trusts, and both sides agree on keys to encrypt the traffic.
  1. Earlier, the certificate authority checked your domain and issued a certificate for it.
  2. The browser says hello and lists the encryption it supports.
  3. The server answers with its certificate for example.com.
  4. The browser checks that a certificate authority it trusts signed the certificate.
  5. Both sides agree on a shared secret key without sending it in the open.
  6. Requests and responses now travel encrypted, and the browser shows the lock.
Static diagram
  1. Certificate authorityissue certificateYour server
  2. BrowserhelloYour server
  3. Your servercertificateBrowser
  4. Browsercheck signatureCertificate authority
  5. Browseragree on keysEncrypted session
  6. Encrypted sessionencrypted GET /Your server

To get HTTPS for free, point your domain at the server, open ports 80 and 443, and run certbot to request a certificate from Let’s Encrypt. The certificate lasts 90 days and certbot renews it automatically, so you only need to confirm that renewal works.

How it works#

HTTPS is ordinary HTTP carried inside a TLS connection. When the browser connects, the server sends its certificate. The certificate says “this public key belongs to example.com” and is signed by a certificate authority (CA) that the browser already trusts. The browser checks the signature, the name, and the dates. If all is good, both sides agree on keys and everything after that is encrypted. Anyone on the network between them sees only scrambled bytes.

The CA needs proof that you control the domain before it signs. Let’s Encrypt does this with the ACME protocol. With the HTTP-01 challenge, the CA gives your client a token, your client serves it at http://example.com/.well-known/acme-challenge/... on port 80, and the CA fetches it from the internet. If the fetch works, you control that name. The CA then signs the certificate. For wildcards or servers that are not reachable from the internet, the DNS-01 challenge proves control with a DNS TXT record instead.

Certificates last 90 days on purpose, so a stolen key stops being useful soon and renewal is automated. Certbot installs a systemd timer (or cron job) that renews certificates that are close to expiring.

Step 1: Install certbot#

bash
sudo apt install certbot

What you should see: apt installs the package and its dependencies. Check with certbot --version, which prints a version number.

Step 2: Get a certificate#

With nothing listening on port 80:

bash
sudo certbot certonly --standalone -d example.com -d www.example.com

certonly gets the certificate but does not edit any web server config. --standalone makes certbot run a small temporary web server on port 80 for the check. Each -d adds a name to the certificate. Certbot may ask for an email address (for expiry notices) and to agree to the terms. What you should see: a message that the certificate was received, and file paths under /etc/letsencrypt/live/example.com/. Point your web server at fullchain.pem (certificate plus chain) and privkey.pem.

To practice without using real issuance, add --staging to the command. It uses Let’s Encrypt’s staging environment, whose certificates browsers do not trust, and whose limits are more generous for testing.

Step 3: Test renewal#

bash
sudo certbot renew --dry-run

What you should see: a simulated renewal for each certificate that ends with a success message. This is the check that matters most, because failures here are the ones that bite you in 60 days.

Check it worked#

bash
sudo certbot certificates

You should see the certificate name, its domains, an expiry date about 90 days out, and the file paths. Then, after your web server uses the certificate:

bash
curl -I https://example.com

What you should see: an HTTP status line with no certificate error. To check the renewal timer exists, run systemctl list-timers and look for certbot.

Edge cases and gotchas#

  • Rate limits exist. Repeated failed attempts can lock you out for a while. Test with --staging first and do not loop retries.
  • Port 80 must be reachable from the internet. A cloud firewall or security group in front of the server blocks it even when ufw allows it. Open it in both places.
  • Standalone renewals can fail later. Once nginx or another service holds port 80, --standalone cannot bind. Switch to the webroot or nginx plugin, or use a renewal hook that stops and starts the service.
  • AAAA records are tested too. If the name has an AAAA record, the CA may connect over IPv6. A wrong or dead AAAA makes validation fail even though IPv4 works.
  • A redirect to HTTPS must not block the challenge. Redirecting /.well-known/acme-challenge/ incorrectly, or a CDN that proxies port 80, can break the check. Tokens are plain HTTP files.
  • The system clock matters. A server with a badly wrong clock sees fresh certificates as not yet valid or already expired. Keep time synced (see the systemd timesyncd or chrony docs).
  • Certificates are per name set. A certificate for example.com does not cover www.example.com unless you list it, and a wildcard needs DNS-01.
  • A proxy that ends TLS changes the picture. Behind a CDN, the visitor’s certificate is the CDN’s, and your origin certificate is a separate matter.

Troubleshooting#

SymptomLikely causeFix
Could not bind TCP port 80 because it is already in use by another process on this systemA web server holds port 80Stop it briefly, or use the webroot or nginx plugin
Timeout during connect (likely firewall problem)Port 80 is not reachable from the internetOpen port 80 in ufw and in the cloud firewall
Validation fails with NXDOMAINThe name does not exist in DNSAdd the record, wait for it to appear in dig
Validation reaches the wrong serverA or AAAA points elsewhereFix the records, check both with dig
Browser shows NET::ERR_CERT_DATE_INVALIDCertificate expired, or clock is wrongRenew, and check the clock with timedatectl
Browser shows NET::ERR_CERT_COMMON_NAME_INVALIDCertificate does not list the name you typedReissue with -d for every name you serve
Renewal dry run failsPort 80 busy, or config changed since issuingRead /var/log/letsencrypt/letsencrypt.log and fix the cause
Rate limit messageToo many recent attemptsWait, and test with --staging
certbot: command not foundCertbot is not installed or not on the pathInstall it with apt, or use the snap method on the certbot site

Real-world scenario#

Dan has a site on a new VPS and wants HTTPS before launch. He checks dig +short example.com A and dig +short www.example.com A: both show 203.0.113.10. He stops nginx for a moment because it holds port 80, and tries a practice run first:

bash
sudo certbot certonly --standalone --staging -d example.com -d www.example.com

The staging run succeeds, so DNS and the firewall are fine. He runs the real command without --staging, points nginx at fullchain.pem and privkey.pem, and starts nginx. Then he runs sudo certbot renew --dry-run and it fails because nginx owns port 80. He switches to the nginx plugin (sudo certbot --nginx) so renewal does not need the port. The proxy side is in nginx reverse proxy with certbot, and the routine for renewals is renew Let’s Encrypt certificates.

Quick reference#

TaskCommand
Installsudo apt install certbot
Practice issueadd --staging
Issue standalonesudo certbot certonly --standalone -d example.com -d www.example.com
List certificatessudo certbot certificates
Test renewalsudo certbot renew --dry-run
Files/etc/letsencrypt/live/example.com/fullchain.pem and privkey.pem

Security and safety notes#

  • Keep privkey.pem private. Never commit it to git or paste it into a chat.
  • Do not delete /etc/letsencrypt: it holds your account and renewal settings.

Next#

Make one name the standard with redirect www to the bare domain.

Try it: Request a certificate

Install certbot, request a certificate for example.com and confirm it.

practice, nothing runs on your machine

Want a real machine? Free browser labs: Killercoda

Check what you learned4 questions
1How long does a Let's Encrypt certificate last?
2Issuing fails with certbot --standalone. Which is a common cause?
3Which command shows the certificate, its domains and the expiry date?
4Your nginx uses port 80. Why can renewal with --standalone fail later even though issuing worked once?

Frequently asked questions

How long does a Let's Encrypt certificate last?

90 days. Tools like certbot and most platforms renew them automatically well before they expire.

Why did issuing fail?

The usual causes are a domain that does not yet point at the server, port 80 blocked by a firewall, or another program already using port 80.

Do I need to buy a certificate to avoid browser warnings?

No. Let's Encrypt certificates are trusted by browsers and are free. Paid certificates mainly add support or validation levels, not stronger encryption.

Can I get a certificate for a server that has no public IP?

Not with the HTTP-01 check, which needs the internet to reach port 80. Use the DNS-01 check, which proves control through a DNS record instead.

Sources

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios