Let’s Encrypt is a Certificate Authority (CA) that provides an easy way to obtain and install free TLS/SSL certificates, thereby enabling encrypted HTTPS on web servers. This article is a fork of https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-16-04
To follow this tutorial, you will need:
- One Debian 9
- A fully registered domain name. Gandi.net in my case. This tutorial will use
- An A record with
www.example.compointing to your server's public IP address. This A record is mandatory, CNAME don’t working well.
- Nginx installed by
apt-get install nginx
The first step to using Let’s Encrypt to obtain an SSL certificate is to install the Certbot software on your server.
First you’ll have to enable the Stretch backports repository. Uncomment lines in /etc/apt/sources.list file:deb http://deb.debian.org/debian stretch-backports main contrib non-free
deb-src http://deb.debian.org/debian stretch-backports main contrib non-free
Once done then run:$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install python-certbot-nginx -t stretch-backports
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.
Setting up Nginx
Certbot can automatically configure SSL for Nginx, but it needs to be able to find the correct
serverblock 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
vi or your favorite text editor.sudo vi /etc/nginx/sites-available/default
Find the existing
server_name line and replace the underscore,
_, with your domain name:
. . .
server_name example.com www.example.com;
. . .
Save the file and quit.
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 restart nginx
Certbot will now be able to find the correct
server block and update it.
To check:netstat -tulpn | grep :80
Next, we'll update our firewall to allow HTTPS traffic.
Allowing HTTPS Through Iptables
You'll need to adjust the settings to allow for HTTP/HTTPS traffic:iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 443 -j ACCEPT
iptables-save > /etc/iptables/rules.v4
We’re now ready to run Certbot and fetch our certificates.
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
certbot with the
--nginx plugin, using
-d to specify the names we'd like the certificate to be valid for.
If this is your first time running
certbot, you will be prompted to enter an email address and agree to the terms of service. After doing so,
certbot will communicate with the Let's Encrypt server, then run a challenge to verify that you control the domain you're requesting a certificate for.
If that’s successful,
certbot will ask how you'd like to configure your HTTPS settings.
Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access. ------------------------------------------------------------------------------- 1: No redirect - Make no further changes to the webserver configuration. 2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for new sites, or if you're confident your site works on HTTPS. You can undo this change by editing your web server's configuration. ------------------------------------------------------------------------------- Select the appropriate number [1-2] then [enter] (press 'c' to cancel):
Select your choice then hit
ENTER. The configuration will be updated, and Nginx will reload to pick up the new settings.
certbot will wrap up with a message telling you the process was successful and where your certificates are stored:
IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at /etc/letsencrypt/live/example.com/fullchain.pem. Your cert will expire on
Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate Donating to EFF: https://eff.org/donate-le
Your certificates are downloaded, installed, and loaded. Try reloading your website using
https:// and notice your browser's security indicator. It should indicate that the site is properly secured, usually with a green lock icon. If you test your server using the SSL Labs Server Test, it will get an A grade.
Let’s finish by testing the renewal process.
Automating 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
If you see no errors, you’re all set. When necessary, Certbot will renew your certificates and reload Nginx to pick up the changes. If the automated renewal process ever fails, Let’s Encrypt will send a message to the email you specified, warning you when your certificate is about to expire.
Reference page: This article is a fork of https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-16-04
Thanks a lot Mitchell Anicas Software Engineer @ DigitalOcean.