/ tech

How to add A+ SSL encryption to Ghost using Let's Encrypt

Guide to add a free, self-renewing, A+ SSL certificat from Let's Encrypt with HSTS to your site.

lets-encrypt

Certbot

Certbot takes a lot of load off your shoulders and provides you an easy tool to generate and updates your let's encrypt certifcates. Install in Linux is as easy as:

wget https://dl.eff.org/certbot-auto
chmod a+x ./certbot-auto

To make it work, install the Python package cryptography and it's dependencies.

 sudo apt update
 sudo apt install -y libssl-dev
 sudo pip install cryptography

Next we execute the binary and generate the certificate.

  sudo service nginx stop
  sudo ./certbot-auto certonly \
      --standalone \
      -d ghost.oceanbased.ch \
      --email foo@bar.com \
      --non-interactive \
      --agree-tos

After this, the certifcates are available at /etc/letsencrypt/live/ghost.oceanbased.ch/.

NGiNX

To reach A+ level, we first have to generate a new Diffie-Hellman key.

 cd /etc/ssl/certs
 sudo openssl dhparam -out dhparam.pem 2048

This will take a while - go and grab a cup of coffee ;)

After the process has finished, change your NGiNX config.

Open your config:

 sudo vim /etc/nginc/sites-available/ghost.conf

Example:

server {
    # Enforce HTTPS
    listen 80 default_server;
    server_name ghost.oceanbased.ch;
    return 301 https://ghost.oceanbased.ch$request_uri;
}

server {
    listen 443 default_server ssl;

    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:50m;
    ssl_session_tickets off;

    ssl_dhparam /etc/ssl/certs/dhparam.pem;

    access_log /var/log/nginx/ghost.oceanbased.ch.log;
    error_log  /var/log/nginx/ghost.oceanbased.ch-error.log error;

    server_name ghost.oceanbased.ch;

    charset utf-8;

    ssl_certificate           /etc/letsencrypt/live/ghost.oceanbased.ch/fullchain.pem;
    ssl_certificate_key       /etc/letsencrypt/live/ghost.oceanbased.ch/privkey.pem;
    ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA';
    ssl_prefer_server_ciphers on;

    add_header Strict-Transport-Security max-age=31536000 always;

    ssl_stapling on;
    ssl_stapling_verify on;

    try_files $uri $uri/ ;

    location / {
      proxy_pass  http://127.0.0.1:2368;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto https;
      proxy_set_header X-Forwarded-Port 443;
      proxy_set_header Host $host;
      add_header Strict-Transport-Security max-age=31536000 always;
  }
}

Renewal

Certbot also takes care of the renewal of your certificate. Add the following to your crontab:

 # open the crontab
 crontab -e
 
 # add the following line
 0 3 * * * /path/to/certbot-auto renew --quiet

A-