Following the previously Let’s Encrypt series regarding Apache or Nginx web server with SSL/TLS module, this article we’ll guide you on how to generate and install a SSL/TLS certificate obtained for free from Let’s Encrypt Certificate Authority that we’ll be using to secure Nginx webserver HTTP transactions on CentOS/RHEL 7/6 and Fedora distribution.

If you’re looking to install Let’s Encrypt for Apache on RHEL/CentOS 7/6 and Fedora distributions, follow this guide below:

Setup Let’s Encrypt to Secure Apache on RHEL, CentOS and Fedora

Requirements

A registered domain name with valid A DNS records to point back to server public IP Address. Nginx web server installed with SSL enabled and Virtual Hosts enabled (only for multiple domains or subdomains hosting).

Our Testing Environment Setup

Step 1: Install Nginx Web Server

1. On the first step, in case you don’t have Nginx daemon already installed, issue the below commands with root privileges in order to install Nginx webserver from Epel repositories:

# yum install epel-release # yum install nginx

Step 2: Download or Clone Free Let’s Encrypt SSL Certificate

2. The fastest method of installing Let’s Encrypt client on Linux systems in by cloning the packages from github repositories.

First, install git client on the system with the below command:

# yum install git

3. After git client has been installed, change directory to /opt path and pull Let’s Encrypt software by running the below commands:

# cd /opt # git clone https://github.com/letsencrypt/letsencrypt

Step 3: Generate a Free Let’s Encrypt SSL Certificate for Nginx

4. The process of obtaining a free SSL/TLS Certificate for Nginx will be done manually by using Let’s Encrypt Standalone plugin.

This method requires that port 80 must be free during the time Let’s Encrypt client validates the server’s identity and generates certificates.

So, if Nginx is already running, stop the daemon with the following command and run ss utility to confirm that port 80 is no longer in use in network stack.

# service nginx stop # systemctl stop nginx # ss -tln

5. Now it’s time to obtain a free SSL Certificate from Let’s Encrypt. Move to Let’s Encrypt installation directory, if you’re not already there, and run the letsencrypt-auto command with the certonly --standalone option and -d flag for each domain or subdomain you wish to generate a certificate as suggested in the below example.

# cd /opt # ./letsencrypt-auto certonly --standalone -d your_domain.tld -d www.yourdomain.tld

6. After a series of packages and dependencies being installed on your machine, Let’s Encrypt will prompt you to enter your account which will be used for lost key recovery or urgent notifications.

7. Next you should agree the license terms by pressing Enter key.

8. Finally, if everything went as it should, a congratulation info message will be shown on your bash terminal. The message will also display when the certificate will expire.

Step 4: Install Let’s Encrypt SSL Certificate in Nginx

9. Now that you own a free SSL/TLS Certificate, it’s time to install it in Nginx webserver in order for your domain to use it.

All new SSL certificates are placed in /etc/letsencrypt/live/ under a directory named after your domain name. Use ls command to list the Certificate files issued for your domain and identify them.

# sudo ls /etc/letsencrypt/live/ # sudo ls -al /etc/letsencrypt/live/your_domain.tld

10. To install the certificate files in Nginx and enable SSL, open /etc/nginx/nginx.conf file for editing and add the below statements after the last listen line from server block. Use the below illustration as guide.

# vi /etc/nginx/nginx.conf

Nginx SSL block excerpt:

# SSL configuration listen 443 ssl default_server; ssl_certificate /etc/letsencrypt/live/your_domain.tld/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/your_domain.tld/privkey.pem; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';

Replace the domain name string for SSL certificates to match your own domain.

11. Finally, restart Nginx service and visit your domain via HTTPS Protocol at https://yourdomain . The page should load smooth, without any certificate error.

# systemctl restart nginx # service nginx restart

12. In order to verify the SSL/TLS certificate and its straightness visit the following link:

https://www.ssllabs.com/ssltest/analyze.html

13. In case you get a notification that your server supports a weak DH key exchange and an overall rating of B grade, generate a new Diffie-Hellman cipher in /etc/nginx/ssl/ directory to protect your server against the Logjam attack by running the following commands.

# mkdir /etc/nginx/ssl # cd /etc/nginx/ssl # openssl dhparam -out dhparams.pem 4096

In this example we’ve used a 4096 bit key, which actually takes a long time to generate and puts an extra overhead on your server and on SSL handshake.

In case there’s no explicit need to use a key this long and you’re not to paranoid, you should be safe with a 2048 bit key.

14. After DH key has been generated, open Nginx configuration file and add the below statements after ssl_ciphers line in order to add the DH key and rise the security level of your domain to an A+ grade.

# vi /etc/nginx/nginx.conf

Add following block excerpt to Nginx.conf:

ssl_dhparam /etc/nginx/ssl/dhparams.pem; ssl_session_timeout 30m; ssl_session_cache shared:SSL:10m; ssl_buffer_size 8k; add_header Strict-Transport-Security max-age=31536000;

15. Restart Nginx service to apply changes and retest your SSL certificate by clearing the previous result cache from the link mentioned above.

# systemctl restart nginx # service nginx restart

Step 5: Auto Renew Nginx Free Lets Encrypt SSL Certificates

16. Let’s Encrypt CA releases free SSL/TLS certificates valid for 90 days. Certificates can be manually renewed and applied before expiration using the webroot plugin, without stopping your web server, by issuing the below commands:

# ./letsencrypt-auto certonly -a webroot --agree-tos --renew-by-default --webroot-path=/usr/share/nginx/html/ -d yourdomain.tld -d www.yourdomain.tld # systemctl reload nginx

When running the above command make sure you replace the webroot-path to match your web server document root, specified by Nginx root statement.

17. In order to auto renew the certificate before it expires create this bash script from github erikaheidi in /usr/local/bin/ directory and add the below content (the script it’s slightly modified to reflect Nginx setting).

# vi /usr/local/bin/cert-renew

Add following lines to cert-renew file.

#!/bin/bash webpath='/usr/share/nginx/html/' domain=$1 le_path='/opt/letsencrypt' le_conf='/etc/letsencrypt' exp_limit=30; get_domain_list(){ certdomain=$1 config_file="$le_conf/renewal/$certdomain.conf" if [ ! -f $config_file ] ; then echo "[ERROR] The config file for the certificate $certdomain was not found." exit 1; fi domains=$(grep --only-matching --perl-regex "(?<=domains \= ).*" "${config_file}") last_char=$(echo "${domains}" | awk '{print substr($0,length,1)}') if [ "${last_char}" = "," ]; then domains=$(echo "${domains}" |awk '{print substr($0, 1, length-1)}') fi echo $domains; } if [ -z "$domain" ] ; then echo "[ERROR] you must provide the domain name for the certificate renewal." exit 1; fi cert_file="/etc/letsencrypt/live/$domain/fullchain.pem" if [ ! -f $cert_file ]; then echo "[ERROR] certificate file not found for domain $domain." exit 1; fi exp=$(date -d "`openssl x509 -in $cert_file -text -noout|grep "Not After"|cut -c 25-`" +%s) datenow=$(date -d "now" +%s) days_exp=$(echo \( $exp - $datenow \) / 86400 |bc) echo "Checking expiration date for $domain..." if [ "$days_exp" -gt "$exp_limit" ] ; then echo "The certificate is up to date, no need for renewal ($days_exp days left)." exit 0; else echo "The certificate for $domain is about to expire soon. Starting renewal request..." domain_list=$( get_domain_list $domain ) "$le_path"/letsencrypt-auto certonly -a webroot --agree-tos --renew-by-default --webroot-path=”$webpath” --domains "${domain_list}" echo "Reloading Nginx..." sudo systemctl reload nginx echo "Renewal process finished for domain $domain" exit 0; fi

18. Replace the $webpath variable from the beginning of the script to match your Nginx document root. Make sure the script is executable and the bc calculator is installed on your system by issuing the following commands.

# chmod +x /usr/local/bin/cert-renew # yum install bc

You can test the script against your domain by issuing the following command:

# /usr/local/bin/cert-renew yourdomain.tld



19. Finally, to run the certificate renewal process automatically, add a new cron job to execute the script every week in order update the certificate within 30 days before the expiration date.

# crontab -e

Add the following line at the bottom of the file.

@weekly /usr/local/bin/cert-renew your_domain.tld >> /var/log/your_domain.tld-renew.log 2>&1

That’s all! Now Nginx server can deliver secure web content with a free SSL/TLS Let’s Encrypt certificate on your website.