Update: Using Free Let’s Encrypt SSL/TLS Certificates with NGINX - NGINX

Original: https://www.nginx.com/blog/using-free-ssltls-certificates-from-lets-encrypt-with-nginx/

Editor – This is an update to a previous blog post about using Let’s Encrypt certificates with NGINX. This blog covers the newly added NGINX support in certbot.

Also see our blog post from nginx.conf 2015, in which Peter Eckersley and Yan Zhu of the Electronic Frontier Foundation introduce the then‑new Let’s Encrypt certificate authority.

It’s well known that SSL/TLS encryption of your website leads to higher search rankings and better security for your users. However, there are a number of barriers that have prevented website owners from adopting SSL.

Two of the biggest barriers have been the cost and the manual processes involved in getting a certificate. But now, with Let’s Encrypt, they are no longer a concern. Let’s Encrypt makes SSL/TLS encryption freely available to everyone.

Let’s Encrypt is a free, automated, and open certificate authority (CA). Yes, that’s right: SSL/TLS certificates for free. Certificates issued by Let’s Encrypt are trusted by most browsers today, including older browsers such as Internet Explorer on Windows XP SP3. In addition, Let’s Encrypt fully automates both issuing and renewing of certificates.

In this blog post, we cover how to use the Let’s Encrypt client to generate certificates and how to automatically configure NGINX to use them.

How Let’s Encrypt Works

Before issuing a certificate, Let’s Encrypt validates ownership of your domain. The Let’s Encrypt client, running on your host, creates a temporary file (a token) with the required information in it. The Let’s Encrypt validation server then makes an HTTP request to retrieve the file and validates the token, which verifies that the DNS record for your domain resolves to the server running the Let’s Encrypt client.

Prerequisites

Before starting with Let’s Encrypt, you need to:

Now you can easily set up Let’s Encrypt with NGINX and NGINX Plus.

Note: In this blog post, we’re running Let’s Encrypt with open source NGINX on Ubuntu 16.04 (Xenial).

1. Download the Let’s Encrypt Client

First, download the Let’s Encrypt client, certbot:

  1. Create the certbot repository:

    $ add-apt-repository ppa:certbot/certbot
  2. Install certbot:

    $ apt-get update
    $ apt-get install python-certbot-nginx

The Let’s Encrypt client is now ready to use.

2. Set Up NGINX

certbot can automatically configure SSL/TLS for NGINX by finding the server block in the NGINX configuration that contains the server_name directive matching the domain name you’re requesting a certificate for. In our example, the domain is www.example.com.

  1. Assuming you’re starting with a fresh NGINX install, use a text editor to create a configuration file named www.example.com.conf in the /etc/nginx/conf.d directory.

  2. Specify your domain name (and variants, if any) with the server_name directive:

    server {
        listen 80 default_server;
        listen [::]:80 default_server;
        root /var/www/html;
        server_name example.com www.example.com;
    }
  3. Save the file, then run this command to verify the syntax of your configuration and restart NGINX:

    $ nginx -t && nginx -s reload

3. Obtain the SSL/TLS Certificate

The NGINX plug‑in for certbot takes care of reconfiguring NGINX and reloading its configuration whenever necessary.

  1. Run the following command to generate certificates with the NGINX plug‑in:

    $ sudo certbot --nginx -d example.com -d www.example.com
  2. Respond to prompts from certbot to configure your HTTPS settings, which involves entering your email address and agreeing to the Let’s Encrypt terms of service.

  3. When certificate generation completes, NGINX reloads with the new settings. certbot generates a message indicating that certificate generation was successful and specifying the location of the certificate on your server.

    Congratulations! You have successfully enabled https://example.com and https://www.example.com 
    
    -------------------------------------------------------------------------------------
    IMPORTANT NOTES: 
    
    Congratulations! Your certificate and chain have been saved at: 
    /etc/letsencrypt/live/example.com/fullchain.pem 
    Your key file has been saved at: 
    /etc/letsencrypt/live/example.com//privkey.pem
    Your cert will expire on 2017-12-12.

    Note: Let’s Encrypt certificates expire after 90 days (on 2017-12-12 in the example). For information about automatically renenwing certificates, see Automatic Renewal of Let’s Encrypt Certificates.

If you look at the NGINX configuration, you’ll note that certbot has modified it:

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    root /var/www/html;
    server_name  example.com www.example.com;

    listen 443 ssl; # managed by Certbot

    # RSA certificate
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot

    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot

    # Redirect non-https traffic to https
    if ($scheme != "https") {
        return 301 https://$host$request_uri;
    } # managed by Certbot
}

4. Automatic Renewal of Let’s Encrypt Certificates

Let’s Encrypt certificates expire after 90 days. We encourage you to automatically renew your certificates. Here we add a cron job to an existing crontab file to do this.

  1. Open the crontab file.

    $ crontab -e
  2. Add the certbot command to run daily. In this example, we run the command every day at noon. The command checks to see if the certificate on the server will expire within the next 30 days, and renews it if so. The --quiet directive tells certbot not to generate output.

    0 12 * * * /usr/bin/certbot renew --quiet
  3. Save and close the file. All installed certificates will be automatically renewed and reloaded.

  4. Summary

    We’ve installed the Let’s Encrypt agent to generate SSL/TLS certificates for a registered domain name. We’ve configured NGINX to use the certificates and set up automatic certificate renewals. With Let’s Encrypt certificates for NGINX and NGINX Plus, you can have a simple, secure website up and running within minutes.

    To try out Let’s Encrypt with NGINX Plus yourself, start your free 30-day trial today or contact us.

Retrieved by Nick Shadrin from nginx.com website.