archive2016

Free HTTPS with Let's Encrypt, certbot and nginx

Getting a Let's Encrypt certificate with certbot in webroot mode, configuring nginx for HTTPS, and automatic renewal on Debian Jessie.

I moved my small sites to Let’s Encrypt this month, the smallest one first and the others in the same week. Let’s Encrypt left beta on 12 April 2016, and in May the official client was renamed from letsencrypt to Certbot, now maintained by the EFF (EFF announcement). The server is Debian Jessie with nginx.

Webroot mode

Certbot has to prove that you control the domain. With the webroot plugin it writes a file into a folder, and the Let’s Encrypt server downloads it over HTTP from http://example.com/.well-known/acme-challenge/.... For a server that already runs nginx this is the simplest way.

The standalone mode starts its own web server on port 80, so nginx must be stopped while it runs. Fine for a test, bad for renewals. There is also an nginx plugin that edits your config, but it is still marked experimental, and I prefer to know what is in my config.

Install certbot

Jessie has no certbot package in the main repository yet, so I use the certbot-auto wrapper. It installs its own dependencies and keeps itself updated:

cd /usr/local/bin
sudo wget https://dl.eff.org/certbot-auto
sudo chmod a+x certbot-auto

The first run installs packages with apt and creates a virtualenv, which takes a few minutes.

Prepare nginx

sudo mkdir -p /var/www/letsencrypt

The port 80 server block serves that folder and redirects everything else to HTTPS:

server {
    listen 80;
    server_name example.com www.example.com;

    location /.well-known/acme-challenge/ {
        root /var/www/letsencrypt;
    }

    location / {
        return 301 https://$host$request_uri;
    }
}

Reload with sudo nginx -t && sudo systemctl reload nginx. The challenge must stay reachable on plain HTTP, so check that the redirect does not catch it. If the main server block has a different root, the challenge location needs its own root, as above.

Get the certificate

sudo certbot-auto certonly --webroot \
  -w /var/www/letsencrypt \
  -d example.com -d www.example.com \
  --email admin@example.com --agree-tos

All names after -d must point to this server, or the whole request fails. While you experiment, add --staging, because Let’s Encrypt limits how many certificates you can get per domain per week.

The files end up in /etc/letsencrypt/live/example.com/. nginx needs fullchain.pem (certificate plus intermediate) and privkey.pem. cert.pem alone is missing the intermediate, and some clients will then fail. The files in live/ are symlinks to the newest version, so point nginx at live/, never at archive/.

HTTPS server block

server {
    listen 443 ssl http2;
    server_name example.com www.example.com;

    ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;

    root /var/www/example;
    index index.html;
}

http2 needs nginx 1.9.5 or newer. Jessie’s own nginx is 1.6, which only has spdy. Use the nginx.org repository or jessie-backports, or remove http2 from the line. This is where my first reload failed.

For the cipher list I used the Mozilla SSL Configuration Generator with the “intermediate” profile instead of copying a list from an old blog post. Then I checked the result with the SSL Labs server test.

Renewal

The certificates are valid for 90 days, on purpose, so that people automate renewal. certbot-auto renew checks all certificates and renews those that expire in less than 30 days, with the settings from the first request. I run it twice a day from root’s crontab:

17 3,15 * * * /usr/local/bin/certbot-auto renew --quiet --no-self-upgrade && /bin/systemctl reload nginx

nginx only reads the new certificate after a reload, so the reload is needed. --no-self-upgrade stops the wrapper from updating itself at night; I run certbot-auto by hand now and then to update it. And a random minute is nicer to the servers than 0.

Webroot mode needs port 80 open from the internet for every renewal, so do not close it after the first certificate. Test a renewal against the staging server:

sudo certbot-auto renew --dry-run

Before I call a site done

  • Challenge folder served on port 80
  • fullchain.pem and privkey.pem from live/
  • Cron runs renew and reloads nginx
  • --dry-run passes
  • SSL Labs test done