How To Setup Let’s Encrypt SSL Certificate With Nginx on CentOS 8 / RHEL 8 & CentOS 7 / RHEL 7

0

Let’s Encrypt is a certificate authority that provides free SSL certificates for websites to enable TLS encryption. It was launched in April 2016.

Let’s Encrypt automates the process of certificate creation, validation, signing, implementation, and renewal of certificates for secure websites.

Currently, Let’s encrypt supports automated certification issuance for Apache, Nginx, Plex, and Haproxy.

THIS DOCUMENT IS ALSO AVAILABLE FOR

Prerequisites

You should have a LEMP stack configured on your RHEL / CentOS system.

READ: How To Install LEMP Stack on CentOS 8 / RHEL 8

READ: How To Install LEMP Stack on CentOS 7 / RHEL 7

Install Certbot

To generate a Let’s Encrypt certificate for your domain, you should have terminal access, and Certbot ACME client installed on your system. It handles certificate issuance and updates Nginx configuration to use the created certificate with no downtime.

Certbot is available on EPEL repository only for CentOS 7 / RHEL 7. So, we need to download the Certbot installer manually for CentOS 8 / RHEL 8 from its official site.

### CentOS 8 / RHEL 8 ###

curl -O https://dl.eff.org/certbot-auto

mv certbot-auto /usr/local/bin/certbot-auto

chmod 0755 /usr/local/bin/certbot-auto

### CentOS 7 ###

rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

yum install -y certbot python2-certbot-nginx

### RHEL 7 ###

rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

subscription-manager repos --enable rhel-7-server-optional-rpms

yum install -y certbot python2-certbot-nginx

Create Virtualhost

We will now create a virtual host (server block) configuration file for the domain www.itzgeek.net.

This server block serves the HTTP version of your domain.
vi /etc/nginx/conf.d/www.itzgeek.net.conf

Use the below information.

server {
   server_name www.itzgeek.net;
   root /opt/nginx/www.itzgeek.net;

   location / {
       index index.html index.htm index.php;
   }

   access_log /var/log/nginx/www.itzgeek.net.access.log;
   error_log /var/log/nginx/www.itzgeek.net.error.log;

   location ~ \.php$ {
      include /etc/nginx/fastcgi_params;
      fastcgi_pass 127.0.0.1:9000;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
   }
}

Create a document root to place your HTML files.

mkdir -p /opt/nginx/www.itzgeek.net

Change the permission of the directory.

chown -R nginx:nginx /opt/nginx/www.itzgeek.net

Place the test HTML file in the document root of your web domain.

echo "This is a test site @ www.itzgeek.net" > /opt/nginx/www.itzgeek.net/index.html

Restart the Nginx service.

systemctl restart nginx

Create / Update DNS Record

Access your DNS management tool or Domain registrar and create an A/CNAME record for the domain. Ex: www.itzgeek.net.

Update DNS Record
Update DNS Record

Wait for some time to let the record propagate.

Check the DNS propagation with Nslookup yum install -y bind-utils utility.

Name Resolution
Name Resolution

Install Let’s Encrypt Certificate

Use the certbot command to create and install Let’s Encrypt certificate.

### CentOS 8 / RHEL 8 ###

/usr/local/bin/certbot-auto --nginx

### CentOS 7 / RHEL 7 ###

certbot --nginx

Follow the interactive prompt and generate and install the certificate.

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): [email protected]  << Email Address to receive renewal/security notification

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A  << Access Terms and Conditions

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y  << Subscribe to Newsletter

Which names would you like to activate HTTPS for?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: www.itzgeek.net
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel): 1  << Choose domain name to generate certificate
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for www.itzgeek.net
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/conf.d/www.itzgeek.net.conf

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): 2  << Redirect traffic from HTTP to HTTPS
Redirecting all traffic on port 80 to ssl in /etc/nginx/conf.d/www.itzgeek.net.conf

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://www.itzgeek.net

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=www.itzgeek.net
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/www.itzgeek.net/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/www.itzgeek.net/privkey.pem
   Your cert will expire on 2019-11-02. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot-auto
   again with the "certonly" option. To non-interactively renew *all*
   of your certificates, run "certbot-auto renew"
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should make a
   secure backup of this folder now. This configuration directory will
   also contain certificates and private keys obtained by Certbot so
   making regular backups of this folder is ideal.
 - 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

Redirect non-www HTTP requests to www HTTPS with Nginx (optional)

We will now configure Nginx server to redirect the traffic comes for non-www HTTP site to the WW HTTPS site, I.e., http://itzgeek.net >> https://www.itzgeek.net.

Here, we will use the same configuration file that we created for the HTTP version of the site to do the non-www HTTP site to WW HTTPS redirection.

vi /etc/nginx/conf.d/www.itzgeek.net.conf

Add the below information at the end of the file.

# Redirect NON-WWW HTTP to WWW HTTPS

server {
    if ($host = itzgeek.net) {
        return 301 https://www.itzgeek.net$request_uri;
    }


   server_name itzgeek.net;
    listen 80;
    return 404;

}

Restart the Nginx service.

systemctl restart nginx

Firewall

Configure the firewall to allow HTTPS requests.

firewall-cmd --permanent --add-port=443/tcp

firewall-cmd --reload

Verify Let’s Encrypt Certificate

Verify the Let’s Encrypt certificate details by going to the HTTPS version of your website.

http://your-http-web-site

OR

https://your-https-web-site
Let's Encrypt Certificate
Let’s Encrypt Certificate

Test SSL Certificate

Check your SSL certificate for any issues and its security ratings by going to the below URL.

https://www.ssllabs.com/ssltest/analyze.html?d=www.itzgeek.net
Test SSL Certificate
Test SSL Certificate

Renew Let’s Encrypt Certificate

Let’s Encrypt certificate comes with a validity of 90 days, and it needs to be renewed before they expire.

By default, both CentOS 7 / RHEL 7 are configured with cron scheduler entry which runs twice a day to renew Let’s Encrypt certificate.

Unfortunately, for CentOS 8 / RHEL 8, we need to configure the cron scheduler manually.

echo "0 0,12 * * * root python -c 'import random; import time; time.sleep(random.random() * 3600)' && /usr/local/bin/certbot-auto renew" | sudo tee -a /etc/crontab > /dev/null

You can also simulate the certificate renewal process with the below command to ensure the renewal goes smooth.

### CentOS 8 / RHEL 8 ###

/usr/local/bin/certbot-auto renew --dry-run

### CentOS 7 / RHEL 7 ###

certbot renew --dry-run

Output:

Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/www.itzgeek.net.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cert not due for renewal, but simulating renewal for dry run
Plugins selected: Authenticator nginx, Installer nginx
Renewing an existing certificate
Performing the following challenges:
http-01 challenge for www.itzgeek.net
Waiting for verification...
Cleaning up challenges

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
new certificate deployed with reload of nginx server; fullchain is
/etc/letsencrypt/live/www.itzgeek.net/fullchain.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
** DRY RUN: simulating 'certbot renew' close to cert expiry
**          (The test certificates below have not been saved.)

Congratulations, all renewals succeeded. The following certs have been renewed:
  /etc/letsencrypt/live/www.itzgeek.net/fullchain.pem (success)
** DRY RUN: simulating 'certbot renew' close to cert expiry
**          (The test certificates above have not been saved.)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should make a
   secure backup of this folder now. This configuration directory will
   also contain certificates and private keys obtained by Certbot so
   making regular backups of this folder is ideal.

If the output does not report any issue, the certificate renewal will work as expected.

Conclusion

That’s All. I hope you learned how to setup Let’s Encrypt SSL Certificate with Nginx on CentOS 8 / RHEL 8 & CentOS 7 / RHEL 7. Share your feedback in the comments section.

You might also like