Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 35 ยท Load Balancers and Security

TLS Certificates and Let's Encrypt on DigitalOcean

Add HTTPS to a DigitalOcean app: Let's Encrypt with certbot on a Droplet, free managed certificates on a Load Balancer, DNS challenges, renewal and expiry checks.

HTTPS encrypts traffic between visitors and your server, proves the server owns the domain and is required for modern browser features. On DigitalOcean there are three common places to get it: App Platform handles certificates automatically, a Load Balancer can issue and renew a free certificate for you, and on a plain Droplet you run Let's Encrypt with certbot yourself. This lesson focuses on the last two.

Commands are text only (DigitalOcean is not available on the site); Python stand-ins check renewal dates and choose a validation method.

How Let's Encrypt works

Let's Encrypt is a free, automated certificate authority. Certificates last 90 days, so renewal must be automated. Before issuing, it has to confirm you control the domain (the ACME protocol):

  • HTTP-01: the CA fetches a token from http://example.com/.well-known/acme-challenge/.... It needs port 80 open and a public web server, and cannot issue wildcard certificates.
  • DNS-01: you publish a TXT record _acme-challenge.example.com. It works without a web server and is the only way to get wildcards such as *.example.com. If your DNS is on DigitalOcean, a plugin can create the record with an API token.
Python Stand-in: pick an ACME challenge

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

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

certbot on a Droplet with Nginx

Point your DNS A record at the Droplet and open ports 80 and 443 in the firewall. Then:

Terminal
sudo apt update
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com
sudo certbot renew --dry-run
systemctl list-timers | grep certbot

The nginx plugin edits your server block, installs the certificate and can add an HTTP to HTTPS redirect. Packages normally install a systemd timer that tries renewal twice a day and only renews certificates within about 30 days of expiry, so renew --dry-run is the test you should remember. Certificates and keys live in /etc/letsencrypt/live/example.com/. After renewal Nginx must reload; the plugin installs a hook for that.

For a wildcard certificate with DigitalOcean DNS:

Terminal
sudo apt install -y python3-certbot-dns-digitalocean
printf 'dns_digitalocean_token = YOUR_TOKEN\n' | sudo tee /root/do.ini >/dev/null
sudo chmod 600 /root/do.ini
sudo certbot certonly --dns-digitalocean \
  --dns-digitalocean-credentials /root/do.ini -d "example.com" -d "*.example.com"

Use a token with only the access the plugin needs, keep the file readable by root only and never commit it.

Managed certificates on a Load Balancer

When the domain's DNS is hosted on DigitalOcean, the Load Balancer setup lets you choose Let's Encrypt as the certificate source. DigitalOcean issues and renews it, and you add a forwarding rule from HTTPS 443 to HTTP on the backend port. This removes certbot from every Droplet, which matters when you have many web servers. You can also upload your own certificate (for example a commercial or wildcard one). Add "redirect HTTP to HTTPS" in the balancer's settings.

Compare the options:

Option Who renews Wildcards Best for
certbot on a Droplet A timer you maintain Yes, via DNS-01 One server, full control
Load Balancer certificate DigitalOcean Depends on the setup Several backends, no certs on servers
App Platform DigitalOcean Managed Apps on the platform

Watch expiry

Renewal can silently fail (a firewall change blocks port 80, an API token expired). Check certificates from outside, and alert when fewer than about 14 days remain.

Python Stand-in: days until expiry

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

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Real checks can use openssl s_client -connect example.com:443 -servername example.com 2>/dev/null | openssl x509 -noout -enddate, or the uptime checks in DigitalOcean Monitoring, which can alert on certificate expiry.

Common mistakes

  • Closing port 80 after issuing: HTTP-01 renewals then fail.
  • Forgetting IPv6 (AAAA records or firewall rules) so one protocol shows a different certificate.
  • Putting a certificate on both the Load Balancer and the backends without a plan; choose where TLS ends.
  • Serving mixed content (HTTP images on an HTTPS page).
  • Not testing certbot renew --dry-run until the certificate is about to expire.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How long does a Let's Encrypt certificate last?

    Certificates last 90 days, so renewal must be automated.
  2. 2. Which challenge do you need for a wildcard certificate?

    Wildcard certificates require DNS-01 validation.
  3. 3. Which command tests renewal without issuing a real certificate?

    --dry-run uses the staging environment to prove renewal works.
  4. 4. Why might a Load Balancer certificate be easier with many web servers?

    Terminating TLS at the balancer centralises certificates.

Summary

  • Let's Encrypt is free, uses 90-day certificates and proves domain control with HTTP-01 or DNS-01.
  • On a Droplet, certbot --nginx plus the renewal timer does the work; test with renew --dry-run.
  • A Load Balancer can issue and renew a managed certificate when DNS is on DigitalOcean.
  • Monitor expiry and keep port 80 (or your DNS token) working so renewals keep succeeding.

Next: The doctl Command Line