Skip to content

Instantly share code, notes, and snippets.

@kjbrum
Last active January 23, 2018 16:28
Show Gist options
  • Save kjbrum/3b81e70b7a1951be9a383e898a0d6f76 to your computer and use it in GitHub Desktop.
Save kjbrum/3b81e70b7a1951be9a383e898a0d6f76 to your computer and use it in GitHub Desktop.
Installing Let's Encrypt with Certbot on DigitalOcean w/ServerPilot

Let's Encrypt on DigitalOcean w/ServerPilot

  • APP_NAME - ServerPilot App Name (serverpilot/apps/example)
  • DOMAIN_NAME - Domain name (example.com)

1. SSH as root into the server

$ ssh root@SERVER_IP_ADDRESS

2. Install Certbot (optional)

Note: This is only necessary when you are setting up a new server, otherwise, skip this step.

$ sudo apt-get update
$ sudo apt-get install software-properties-common
$ sudo add-apt-repository ppa:certbot/certbot
$ sudo apt-get update
$ sudo apt-get install python-certbot-nginx

3. Create new cert(s)

Note: Stopping NGINX will make sites on the server inaccessible.

# Stop NGINX
$ service nginx-sp stop

# Create the certs
$ certbot certonly --standalone -d DOMAIN_NAME.com -d www.DOMAIN_NAME.com

# Start NGINX
$ service nginx-sp start

The first time you run this command the process could take a few minutes, after it's finished installing, follow on screen instructions and you should get a message similar to:

Congratulations! Your certificate and chain have been saved at /etc/letsencrypt/live/DOMAIN_NAME.com/fullchain.pem.

4. Add the SSL config file

$ nano /etc/nginx-sp/vhosts.d/APP_NAME.ssl.conf

Note: The filename should match the serverpilot app name. Another way to know is to see what ever letsencrypt named what the files here /etc/nginx-sp/vhosts.d/APP_NAME.conf.

server {
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  server_name DOMAIN_NAME.com www.DOMAIN_NAME.com;

  ssl on;

  # LetsEncrypt Certs
  ssl_certificate /etc/letsencrypt/live/DOMAIN_NAME.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/DOMAIN_NAME.com/privkey.pem;

  # Verify Chain of Trust of OCSP Repsonse
  # http://nginx.org/en/docs/http/ngx_http_ssl_module.html
  ssl_trusted_certificate /etc/letsencrypt/live/DOMAIN_NAME.com/chain.pem;

  # Secure SSL protocols and ciphers
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_prefer_server_ciphers on;
  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: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_session_timeout 1d;
  ssl_session_cache shared:SSL:50m;

  # OCSP Stapling
  ssl_stapling on;
  ssl_stapling_verify on;
  
  # Strict Transport Security
  # https://www.chromium.org/hsts
  add_header Strict-Transport-Security max-age=15768000;

  root /srv/users/serverpilot/apps/APP_NAME/public;

  access_log /srv/users/serverpilot/log/APP_NAME/APP_NAME_nginx.access.log main;
  error_log /srv/users/serverpilot/log/APP_NAME/APP_NAME_nginx.error.log;

   # Proxy Set
  proxy_set_header Host $host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header X-Forwarded-SSL on;
  proxy_set_header X-Forwarded-Proto $scheme;
  
   # Includes
  include /etc/nginx-sp/vhosts.d/APP_NAME.d/*.nonssl_conf;
  include /etc/nginx-sp/vhosts.d/APP_NAME.d/*.conf;
}

5. Restart NGINX

$ service nginx-sp restart

Success! If everything went right your domain should now have SSL.


Renewing certs

Auto-renew certs with crontab

$ crontab -e
# Certbot renew command every Monday at 5:00 am
0 5 * * 1 certbot renew --pre-hook "service nginx-sp stop" --post-hook "service nginx-sp start" >> /var/log/letsencrypt-renew.log

Manually renewing certs

# Test run
$ certbot renew --dry-run

# Actually renew certs
$ certbot renew

Extras

Adding a new subdomain to certs

$ certbot certonly --standalone -d domain.com -d www.domain.com -d sub.domain.com -d sub2.domain.com --expand

Change Certbot notification email address:

$ certbot register --update-registration --email <email>

More resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment