Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 30 ยท HTTPS and Modern HTTP

HTTPS, TLS and Let's Encrypt

Understand certificates, chains and the TLS handshake, then enable HTTPS in nginx and Apache with certbot, automatic renewal, HTTP-to-HTTPS redirects and SNI.

HTTPS is HTTP carried inside a TLS connection. TLS gives you three things: encryption (nobody on the path can read the traffic), integrity (nobody can change it unnoticed) and authentication (the browser knows it is talking to the real example.com, not an impostor). Today browsers mark plain HTTP as "not secure", search engines prefer HTTPS, and HTTP/2 and HTTP/3 are used only over TLS in practice. So every public site needs a certificate.

Certificates used to cost money and an afternoon of work. Let's Encrypt, a free certificate authority, changed that: a tool called certbot proves that you control a domain, fetches a certificate and installs it, and a timer renews it before it expires. In this lesson you learn what a certificate really is, how the handshake works, how to enable HTTPS in nginx and Apache, and how renewal and SNI fit in. The real tools need a public server and a domain, so the commands are read-only here, with JavaScript stand-ins for the ideas.

What TLS gives you

Encryption Eavesdroppers see noise.
Integrity Tampering is detected.
Identity The certificate proves the name.
1 / 2

Certificates and chains

A certificate is a signed statement: "this public key belongs to these names, valid between these dates." The signer is a certificate authority (CA). Browsers and operating systems ship with a small list of root CAs they trust (the trust store). Roots rarely sign website certificates directly, because their keys are kept offline. Instead a root signs an intermediate certificate, and the intermediate signs your leaf certificate. The browser follows the signatures from the leaf up to a root it already trusts. That list of linked certificates is the chain.

flowchart LR
  L["Leaf<br/>example.com"] -- signed by --> I["Intermediate CA"]
  I -- signed by --> R["Root CA<br/>(in the trust store)"]

The server sends the leaf and the intermediates; the root is already in the browser.

For the browser to accept a certificate, all of these must hold:

  1. The requested host name matches one of the certificate's names (the Subject Alternative Names). A wildcard *.example.com matches one label, so www.example.com but not a.b.example.com and not example.com itself.
  2. Today's date lies inside each certificate's validity period.
  3. Each certificate was signed by the next one up, and the signers are allowed to be CAs.
  4. The chain ends in a root that is in the trust store.

The most common server mistake is sending only the leaf. Some clients can fetch missing intermediates; many cannot, and you get errors on old devices, command-line tools and API clients while your own browser looks fine. That is why you configure the full chain file, not the bare certificate.

How the handshake works

With TLS 1.3, which every current server and browser supports, the handshake takes one round trip:

  1. The client sends a ClientHello: supported versions and ciphers, a key share, and the server name it wants (SNI).
  2. The server replies with its key share, its certificate chain and a signature proving it holds the private key that matches the leaf.
  3. Both sides derive the same session keys from the key shares and switch to encrypted traffic. The first HTTP request can follow immediately.

The private key never leaves the server and is never sent. The key shares use ephemeral Diffie-Hellman, which gives forward secrecy: stealing the server's key later does not decrypt recorded traffic. Configure TLSv1.2 and TLSv1.3 and nothing older; SSL and TLS 1.0 or 1.1 are broken or deprecated.

Validating a chain, in code

Real signatures use RSA or ECDSA mathematics that does not fit a lesson page. This stand-in replaces them with a toy checksum so that the logic of validation is visible: it follows the links, checks the dates and names, and refuses the same things a browser refuses. The checksum is not secure and exists only for this demo.

JS Build and validate a certificate chain

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Each failing case matches a real-world error: the wrong host name, a missing intermediate (the "works in my browser but not in curl" classic), a modified certificate whose signature no longer fits, and an expired certificate. Real clients report these as messages like unable to get local issuer certificate or certificate has expired.

Getting a certificate with certbot

certbot proves you control the domain with an ACME challenge. In the common HTTP-01 challenge, Let's Encrypt asks your server to publish a token at http://yourdomain/.well-known/acme-challenge/.... The DNS must already point to this server, and port 80 must be reachable, even if you redirect everything else to HTTPS later. For a wildcard certificate you need the DNS-01 challenge, which means adding a TXT record, usually through a DNS provider plugin.

On Debian or Ubuntu, certbot has plugins that edit your web server config for you:

Terminal
sudo apt install certbot python3-certbot-nginx     # or python3-certbot-apache

sudo certbot --nginx  -d example.com -d www.example.com
sudo certbot --apache -d example.com -d www.example.com

Other useful forms:

Terminal
sudo certbot certonly --webroot -w /var/www/example/public -d example.com   # only fetch, edit config yourself
sudo certbot certonly --standalone -d example.com                          # certbot runs its own temporary server
sudo certbot certificates                                                  # list certificates and expiry dates
sudo certbot renew --dry-run                                               # test renewal without changing anything

The files land in /etc/letsencrypt/live/example.com/: fullchain.pem (leaf plus intermediates, the one you want), chain.pem, cert.pem (leaf only) and privkey.pem. Treat privkey.pem as a password: readable by root only. The names in live/ are symbolic links that certbot updates on every renewal, which is why configs point there instead of at dated files.

The nginx server blocks

NGINX
server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://example.com$request_uri;
}

server {
    listen 443 ssl;
    server_name example.com;

    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;

    root /var/www/example/public;
}

The first block is the HTTP to HTTPS redirect: a permanent redirect to the secure URL that keeps the path and query string ($request_uri). The second block terminates TLS. Keep ssl_certificate pointing at fullchain.pem. Prefer the protocol list above and leave the cipher list at nginx's defaults unless you have a reason; recent defaults are sound. After editing, always test and reload: sudo nginx -t && sudo systemctl reload nginx.

The Apache virtual hosts

APACHE
<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    Redirect permanent / https://example.com/
</VirtualHost>

<VirtualHost *:443>
    ServerName example.com
    DocumentRoot /var/www/example/public

    SSLEngine on
    SSLCertificateFile    /etc/letsencrypt/live/example.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
    SSLProtocol           all -SSLv3 -TLSv1 -TLSv1.1
</VirtualHost>

This needs mod_ssl (a2enmod ssl). With Apache 2.4.8 and newer, SSLCertificateFile accepts the full chain file, so the older separate SSLCertificateChainFile line is not needed. The Redirect permanent rule from Redirects and Rewrites appends the requested path automatically.

SNI: many certificates on one address

A server has one IP address and port 443 but may host dozens of sites. The server needs to know which certificate to present before any HTTP request is visible, because the certificate is sent first. Server Name Indication solves this: the client states the host name in the ClientHello, and the server picks the matching certificate. nginx chooses the server block by that name, and Apache chooses the VirtualHost the same way (see Server Blocks and Choosing a Server). Every browser from the last decade supports SNI. If the name matches nothing, the server presents its default certificate, which is why the default server should not be one of your real sites.

Renewal

Let's Encrypt certificates last 90 days, and certbot renews those with fewer than 30 days left. The Debian and Ubuntu packages install a systemd timer (or cron job) that runs certbot renew twice a day. It usually needs a reload to make the web server read the new files, which certbot's plugins arrange through a deploy hook:

Terminal
sudo certbot renew --deploy-hook "systemctl reload nginx"
systemctl list-timers | grep certbot

Lifetimes are expected to keep getting shorter across the industry, so never rely on remembering a date. Rely on automation and on monitoring, such as an alert when a certificate has fewer than 14 days left. The next stand-in selects certificates by SNI, falls back to a default, and flags certificates that renewal should already have replaced.

JS SNI selection and renewal checks

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The last request has a name no certificate covers, so the client receives the default certificate and its browser will show a name mismatch, which is the correct behavior for a host that you do not serve. The wildcard certificate matches one label and has already expired, so the check reports it. In real life certbot renew would have replaced it 30 days earlier, so an expired certificate means that automation is broken.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which file should ssl_certificate point at for a Let's Encrypt certificate?

    Clients must receive the leaf and its intermediates. Sending only the leaf works in some browsers and fails in many other clients.
  2. 2. What problem does SNI solve?

    The client names the host in the ClientHello, before any HTTP request, so the server can choose the matching certificate.
  3. 3. Does the wildcard *.example.com cover example.com and a.b.example.com?

    A wildcard stands for a single label, so it matches www.example.com but neither the bare domain nor deeper names.
  4. 4. For the HTTP-01 challenge, what must be true?

    Let's Encrypt fetches a token over plain HTTP from your domain. Keep port 80 open even if it only redirects.
  5. 5. How often do Debian-style certbot packages try to renew, and when does a certificate actually get replaced?

    Frequent attempts with a 30 day threshold leave plenty of time to retry after a failure.

Summary

  • TLS provides encryption, integrity and identity. A certificate chain links your leaf, an intermediate and a trusted root; always serve the full chain.
  • TLS 1.3 needs one round trip; allow TLS 1.2 and 1.3 only.
  • certbot proves control with an ACME challenge, stores files under /etc/letsencrypt/live/<name>/, and renews automatically after the first run; test with certbot renew --dry-run.
  • nginx uses ssl_certificate and ssl_certificate_key; Apache uses SSLEngine on, SSLCertificateFile and SSLCertificateKeyFile.
  • Redirect port 80 to HTTPS with a permanent redirect, and rely on SNI to share one address between many sites.

Next: make the encrypted connection fast in HTTP/2, HTTP/3, gzip and Brotli.