Set up a free SSL certificate with Let’s Encrypt and Certbot on Ubuntu

Free SSL for WordPress with Let’s Encrypt and Certbot

Let’s Encrypt issues free certificates that are valid for 90 days, and Certbot requests, installs and renews them automatically. On an Ubuntu server with Nginx or Apache the whole setup takes a few minutes; after that WordPress only needs to know that its address starts with https://.

Problem: A WordPress site on a VPS runs on plain HTTP, browsers mark it “Not secure”, and a paid certificate with manual renewal is not wanted.

Solution: Install Certbot with the plugin for your web server, request the certificate for the domain and its www variant, let Certbot add the redirect to HTTPS, check that the renewal timer works, and switch the WordPress URLs to https://.

Requirements

The domain (and www) must point to the server, port 80 must be reachable from the internet (Let’s Encrypt checks it over HTTP), and the web server must already serve the site for this domain.

Install and request

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

sudo certbot --nginx -d example.com -d www.example.com
# or: sudo certbot --apache -d example.com -d www.example.com

Certbot asks for an email address for expiry warnings, finds the server block (or the Apache virtual host) for the domain, adds the certificate and offers to redirect HTTP to HTTPS. Accept the redirect.

The Certbot team recommends the snap package (sudo snap install --classic certbot) because it is always the current version; the apt package of Ubuntu 20.04 and 22.04 works too.

What Certbot adds to Nginx

server {
    server_name example.com www.example.com;
    root /var/www/example;

    listen 443 ssl;
    ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    include             /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam         /etc/letsencrypt/ssl-dhparams.pem;
}

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

Renewal

systemctl list-timers | grep certbot
sudo certbot renew --dry-run
sudo certbot certificates

The package installs a systemd timer (or a cron job) that runs certbot renew twice a day; a certificate is renewed when less than 30 days are left. The dry run must succeed, otherwise the site breaks in three months.

Switch WordPress to HTTPS

wp option update home 'https://example.com'
wp option update siteurl 'https://example.com'
wp search-replace 'http://example.com' 'https://example.com' --skip-columns=guid --dry-run

Run the search-replace without --dry-run after checking the numbers; it fixes links and images in the content. Mixed content warnings that remain are covered in mixed content after switching to HTTPS, the redirect via .htaccess in how to force HTTPS.

NOTE: Add an HSTS header (Strict-Transport-Security) only after HTTPS has worked for a while on the domain and all subdomains: browsers remember it for the whole max-age, and a broken certificate then makes the site unreachable instead of showing a warning. Start with a short max-age such as 300 seconds.