Let’s Encrypt SSL Certificate Auto Renewal for NGINX on Centos

Let’s Encrypt is a CA that went into beta mode on December 3, 2015, providing free X.509 cryptographic certificates for TLS encryption. The process of issuing certificates is fully automated.

Training

Install packages

$ sudo yum -y install git bc

Clone the letsencrypt GitHub repository to the / opt / letsencrypt directory

$ sudo git clone https://github.com/letsencrypt/letsencrypt /opt/letsencrypt

We get an ssl certificate

Go to the directory

$ cd /opt/letsencrypt

Run the script

$ ./letsencrypt-auto certonly -a webroot --webroot-path=/var/www/example.com/public_html -d example.com -d www.example.com
  • example.com, www.example.com is our domain
  • webroot-path = / var / www / example.com / public_html – directory where the site is located

Run letsencrypt-auto application without sudo

If the script has completed successfully, we will receive a message:

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/www.example.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/www.example.com/privkey.pem
   Your cert will expire on 2020-10-26. To obtain a new or tweaked
   version of this certificate in the future, simply run
   letsencrypt-auto again. To non-interactively renew *all* of your
   certificates, run "letsencrypt-auto renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

As a result, in the /etc/letsencrypt/live/www.example.com/ directory, we got the following files

  • cert.pem – certificate
  • chain.pem – Let’s Encrypt certificate chain
  • fullchain.pem – the combined cert.pem and chain.pem certificates
  • privkey.pem – private key

Generate a Diffie-Hellman key and save it to the / etc / ssl / certs / directory

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

Configuring Nginx

As a result, our config will look like this

$ sudo nano /etc/nginx/sites-available/example.com.conf

server {
    # redirect
    server_name example.com www.example.com
    listen 80;
    return 301 https://www.example.com$request_uri;
}

server {
        listen 443 ssl;

        server_name example.com www.example.com;

        # Let's Encrypt certs
        ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; 
        ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
		ssl_trusted_certificate /etc/letsencrypt/live/www.itdraft.ru/chain.pem;

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;

        ssl_dhparam /etc/ssl/certs/dhparam.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: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;

        ssl_stapling on;
        ssl_stapling_verify on;
        add_header Strict-Transport-Security max-age=15768000;

        location ~ /.well-known {
                allow all;
        }

        # The rest of your server block
        root /usr/share/nginx/html;
        index index.html index.htm;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        }
}

Checking and reloading Nginx

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

$ sudo systemctl reload nginx

Ssl certificate auto-renewal

For auto-renewal use the command

$ /opt/letsencrypt/letsencrypt-auto renew

Because we recently renewed the certificate, then in response we will see the message

Requesting to rerun /opt/letsencrypt/letsencrypt-auto with root privileges...
Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/www.example.com.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cert not yet due for renewal

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

The following certs are not due for renewal yet:
  /etc/letsencrypt/live/www.example.com/fullchain.pem expires on 2020-10-26 (skipped)
No renewals were attempted.

Add a command for auto-renewal to crontab

$ sudo crontab -e
## Обновление SSL сертификата по понедельникам и четвергам в 2:15
15  2  * * 1,4  /opt/letsencrypt/letsencrypt-auto renew && systemctl restart nginx

Letsencrypt script update

Downloading changes for the letsencrypt GitHub repository

$ cd /opt/letsencrypt
$ sudo git pull

Sidebar