Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ntamvl/b9ee6aee6e71760a31a2fc80b3e2fd14 to your computer and use it in GitHub Desktop.
Save ntamvl/b9ee6aee6e71760a31a2fc80b3e2fd14 to your computer and use it in GitHub Desktop.
How To Secure Nginx with Let's Encrypt on Ubuntu 16.04

How To Secure Nginx with Let's Encrypt on Ubuntu 16.04

Step 1 — Installing Certbot

First, add the repository.

sudo add-apt-repository ppa:certbot/certbot

You'll need to press ENTER to accept. Then, update the package list to pick up the new repository's package information.

sudo apt-get update

And finally, install Certbot's Nginx package with apt-get.

sudo apt-get install python-certbot-nginx

Certbot is now ready to use, but in order for it to configure SSL for Nginx, we need to verify some of Nginx's configuration.

Step 2 — Setting up Nginx

Certbot can automatically configure SSL for Nginx, but it needs to be able to find the correct server block in your config. It does this by looking for a server_name directive that matches the domain you're requesting a certificate for.

If you're starting out with a fresh Nginx install, you can update the default config file. Open it with nano or your favorite text editor.

sudo nano /etc/nginx/sites-available/default

Find the existing server_name line and replace the underscore, _, with your domain name:

/etc/nginx/sites-available/default
. . .
server_name example.com www.example.com;
. . .

Save the file and quit your editor.

Then, verify the syntax of your configuration edits.

sudo nginx -t

If you get any errors, reopen the file and check for typos, then test it again.

Once your configuration's syntax is correct, reload Nginx to load the new configuration.

sudo systemctl reload nginx

Certbot will now be able to find the correct server block and update it. Next, we'll update our firewall to allow HTTPS traffic.

Step 3 — Allowing HTTPS Through the Firewall

To additionally let in HTTPS traffic, we can allow the Nginx Full profile and then delete the redundant Nginx HTTP profile allowance:

sudo ufw allow 'Nginx Full'
sudo ufw delete allow 'Nginx HTTP'

Your status should look like this now:

sudo ufw status

Step 4 — Obtaining an SSL Certificate

Certbot provides a variety of ways to obtain SSL certificates, through various plugins. The Nginx plugin will take care of reconfiguring Nginx and reloading the config whenever necessary:

sudo certbot --nginx -d example.com -d www.example.com

This runs certbot with the --nginx plugin, using -d to specify the names we'd like the certificate to be valid for.

Step 5 — Verifying Certbot Auto-Renewal

Let's Encrypt's certificates are only valid for ninety days. This is to encourage users to automate their certificate renewal process. The certbot package we installed takes care of this for us by running ‘certbot renew’ twice a day via a systemd timer. On non-systemd distributions this functionality is provided by a script placed in /etc/cron.d. This task runs twice a day and will renew any certificate that's within thirty days of expiration.

To test the renewal process, you can do a dry run with certbot:

sudo certbot renew --dry-run

OR 

sudo certbot renew --force-renewal

source:

https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-16-04

https://github.com/certbot/certbot

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