Another “Let’s Encrypt” post for nginx

I’ve replaced the certificate on this site with one issued by Let’s Encrypt and plan to do so for all clients (or enable SSL in the first place) as their domains come up for renewal, or other maintenance work is contracted. The big downside is a 90 day expiry time, which requires a service nginx reload at least that often.

I had no end of issues using the official client as it wouldn’t create the .well-known/acme-challenge files necessary to get the domain to validate (yes, I checked directory permissions.) Instead, Vincent Composieux has some excellent instructions on just using the certonly parameter inside a script.

Rundown, including my changes in case the article disappears:

  • clone letsencrypt repository to /opt/letsencrypt
  • create /usr/local/etc/
# We use a 4096 bit RSA key instead of 2048
rsa-key-size = 4096

email =
domains =,

authenticator = webroot

# This is the webroot directory of your domain in which
# letsencrypt will write a hash in /.well-known/acme-challenge directory.
webroot-path = /var/www/
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:50m;
ssl_dhparam /etc/ssl/dhparams.pem;

if ($myroot = false) {
        set $myroot $realpath_root;

location '/.well-known/acme-challenge' {
    root $myroot/;
    try_files $uri /$1;
  • For each site in /etc/nginx/sites-enabled, update the SSL definition to store the webroot in the $myroot variable, then have the root directive (and ssl.conf) reference it:
server {
        listen 443 ssl;
        # [...]
        set $myroot /var/www/;
        root $myroot;
        include global/ssl.conf;
        # [...]
  • Create the certificate: sudo /opt/letsencrypt/letsencrypt-auto certonly --config /usr/local/etc/
  • Add the certificate paths to each site in sites-enabled:
server {
        # [...]
        include global/ssl.conf;
        ssl_certificate /etc/letsencrypt/live/;
        ssl_certificate_key /etc/letsencrypt/live/;
        # [...]
  • To automatically renew certificates 30 days before expiry, checking each day: ln -snf /usr/local/bin/ /etc/cron.daily/

Some adjustments are obviously necessary for multiple sites but this got me past the point where site validation failed.