All posts

Blog

How to Install a Free Let's Encrypt SSL Certificate with Certbot on a VPS

Illustrated cover for the Servers & VPS guide "How to Install a Free Let's Encrypt SSL Certificate with Certbot on a VPS"

On shared hosting, SSL usually just happens: the control panel issues and renews certificates for you. On a VPS, you are the control panel. The good news is that Let's Encrypt, a free and nonprofit certificate authority, combined with the Certbot client, makes HTTPS on your own server a five-minute job.

This guide explains how to install a Let's Encrypt SSL certificate with Certbot on a VPS running Nginx or Apache, how renewal works, and how to fix the errors people hit most often. If you want background on what SSL actually does, start with our explainer on what SSL and HTTPS are.

How Let's Encrypt works in brief

Before issuing a certificate, Let's Encrypt has to confirm you control the domain. It does that with a challenge:

  • HTTP-01: Certbot places a challenge file on your web server, and Let's Encrypt fetches it over port 80 at http://yourdomain/.well-known/acme-challenge/....
  • DNS-01: you (or a Certbot plugin) create a TXT record under _acme-challenge.yourdomain. This is the only method that supports wildcard certificates.

Let's Encrypt certificates are valid for 90 days. The short lifetime is deliberate: it encourages automation, and Certbot renews them automatically well before they expire.

Before you start

Check three things first, because they cause most failures:

  1. DNS points at your server. Your domain's A record (and AAAA record, if you have one) must resolve to this VPS. A stale AAAA record pointing somewhere else is a classic cause of failed validation.
  2. Port 80 is open. Even if you only want HTTPS, the HTTP-01 challenge needs port 80. Allow it in your firewall: sudo ufw allow 'Nginx Full' or sudo ufw allow 'Apache Full'.
  3. A web server block exists for the domain with the right server_name (Nginx) or ServerName (Apache).

Step 1: Install Certbot

The Certbot project recommends installing via snap on many distributions, which keeps Certbot current:

sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot

On Ubuntu and Debian you can also use the distribution packages, which are a little older but work well:

sudo apt install certbot python3-certbot-nginx -y      # for Nginx
sudo apt install certbot python3-certbot-apache -y     # for Apache

Use one method, not both, to avoid confusing duplicate installs. Certbot's website has an instruction picker where you choose your web server and OS.

Step 2: Get a certificate

Nginx

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

Apache

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

Certbot asks for an email address (used for important notices about your certificates), asks you to agree to the terms, and then performs the challenge. If it succeeds, it edits your server configuration to use the new certificate and offers to redirect all HTTP traffic to HTTPS. Accept the redirect unless you have a specific reason not to.

Visit https://example.com and check for the padlock.

Certificate only (no config changes)

If you want to manage the web server configuration yourself, or you are running something other than Nginx or Apache, use the webroot method:

sudo certbot certonly --webroot -w /var/www/example.com -d example.com -d www.example.com

Or, if nothing is listening on port 80, --standalone lets Certbot briefly run its own web server:

sudo certbot certonly --standalone -d example.com

Certificates are stored in /etc/letsencrypt/live/example.com/. Point your software at fullchain.pem (the certificate plus intermediates) and privkey.pem (the private key). Use the live paths, which are symlinks that always point to the current certificate.

Step 3: Confirm automatic renewal

Certbot installs a scheduled task (a systemd timer or a cron entry, depending on how you installed it) that checks twice a day and renews certificates that are close to expiring. Verify it:

systemctl list-timers | grep -i certbot

Then do a dry run against Let's Encrypt's staging environment:

sudo certbot renew --dry-run

If the dry run succeeds, renewal will work. If it fails, fix it now rather than finding out when the certificate expires.

Reloading services after renewal

The Nginx and Apache plugins reload the web server automatically. For other services that use the certificate (a mail server, a Node.js app reading the files directly), add a deploy hook so they pick up the new certificate:

sudo certbot renew --deploy-hook "systemctl reload postfix"

Or drop an executable script into /etc/letsencrypt/renewal-hooks/deploy/.

Wildcard certificates with DNS-01

A wildcard certificate (*.example.com) covers every subdomain. It requires the DNS-01 challenge:

sudo certbot certonly --manual --preferred-challenges dns -d "example.com" -d "*.example.com"

Certbot will ask you to create a TXT record. The catch with --manual is that it cannot renew automatically, because you would need to update the TXT record by hand each time. For hands-off renewal, use a DNS plugin for your DNS provider (Certbot has plugins for several major providers), which creates and removes the records via the provider's API.

Managing certificates

sudo certbot certificates                 # list certificates and expiry dates
sudo certbot --nginx -d example.com -d www.example.com -d shop.example.com   # add a name (Certbot offers to expand)
sudo certbot delete --cert-name example.com   # remove a certificate you no longer use

Troubleshooting common errors

  • "Timeout during connect" or "Connection refused": port 80 is blocked by a firewall, or nothing is listening on it.
  • "Unauthorized" / 404 on the challenge file: DNS points at another server, an AAAA record is wrong, or a redirect rule is interfering with /.well-known/acme-challenge/.
  • "Too many certificates already issued": Let's Encrypt has rate limits. Use --dry-run or the --staging flag while testing so you do not burn through them.
  • Behind a CDN or proxy: the HTTP challenge may be intercepted. Temporarily bypass the proxy for validation, or use DNS-01.
  • Mixed content warnings after switching: the certificate is fine, but your pages load some resources over http://. Update those URLs.

Hardening tips

  • Keep the HTTP-to-HTTPS redirect in place.
  • Once everything works over HTTPS, consider adding an HSTS header so browsers always use HTTPS. Start with a short max-age and increase it once you are confident, because HSTS is hard to undo.
  • Check your configuration with a public TLS testing tool such as SSL Labs' server test.

Wrap-up

Installing a Let's Encrypt certificate with Certbot on a VPS is one of the highest-value, lowest-effort tasks in server administration. Make sure DNS and port 80 are right, run Certbot with your web server's plugin, confirm renewal with a dry run, and add deploy hooks for any other services. After that, your certificates renew themselves and HTTPS is one less thing to think about.