Centos 7 Nginx Letsencrypt Https And Https


Centos 7 Nginx Letsencrypt Https And Https

Overview

Many web servers require secure communications through the HTTPS protocol, and the most common way to do this is with the help of a TLS certificate. TLS (Transport Layer Security) is an internet protocol used to provide secure communications over the web. Let’s Encrypt is a free, open-source Certificate Authority that provides free digital certificates to organisations and individuals alike. It’s the perfect choice for setting up an HTTPS server on a Centos 7 machine using Nginx.

Prerequisites

To follow along with this guide, you’ll need the following:

  • A Centos 7 machine with Nginx installed.
  • Access to the root user account.
  • An active domain name.
  • The ability to edit Nginx configuration files.

Once you have all of these pieces in place, you’ll be ready to set up your secure HTTPS server.

Installing Certbot

Certbot is the official client for the Let’s Encrypt Certificate Authority. We need it to automatically generate and renew TLS certificates for us. To install it, we first need to add the Let’s Encrypt repository to our system’s package list. This can be done by running the following command:

sudo yum-config-manager –add-repo https://dl.fedoraproject.org/pub/epel/7/x86_64/

Next, we’ll install the EPEL package:

sudo yum install epel-release

Now we can finally install Certbot:

sudo yum install certbot

Generating A Certificate

Now that Certbot is installed, we can generate our certificate. To do this, we need to run the following command:

sudo certbot certonly --webroot -w /var/www/example.com -d example.com -d www.example.com

In the command above, we are telling certbot to generate a certificate for our domain example.com and its subdomain www.example.com. We also need to provide Certbot with a web root directory, which should be set to the directory where the site is hosted. For example, here we are setting it to /var/www/example.com.

Once you have run the command, Certbot will generate the certificate and save it in /etc/letsencrypt/live. It will also create a cron job that will renew the certificate automatically every 90 days. You can view the cron job by running the following command:

sudo crontab -e

Configuring Nginx

Now that we have the certificate, we need to configure Nginx to use it. To do this, we need to edit our Nginx configuration file. We can open it with the following command:

sudo nano /etc/nginx/nginx.conf

In the configuration file, we need to add the following:

server {
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
server_name example.com www.example.com;
root /var/www/example.com;
location / {
index index.html;
}
}

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

When you’re done, save the file and exit. Finally, we need to restart Nginx for the changes to take effect. We can do that with the following command:

sudo systemctl restart nginx

Testing The Configuration

Now that we have configured our Nginx server to use Let’s Encrypt certificates, we need to test it to make sure it’s working correctly. To do this, we can use the following command:

curl -I -L https://example.com

This command will show us the response from the server, including the protocol, the status code, and the TLS certificate being used. It should look something like this:

HTTP/2 200
Connection: keep-alive
Last-Modified: Wed, 13 Feb 2019 13:59:17 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 280
Server: nginx
Strict-Transport-Security: max-age=63072000; includeSubDomains
Accept-Ranges: bytes
Vary: Accept-Encoding
Date: Wed, 13 Feb 2019 14:14:14 GMT
Content-Security-Policy: upgrade-insecure-requests
TLSv1.2
TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256

Conclusion

In this guide, we have shown you how to set up a secure HTTPS server on a Centos 7 machine with Nginx and Let’s Encrypt. We have installed Certbot, generated the certificate, and configured Nginx to use it. We have also tested it to make sure everything is working properly. If you have any questions or feedback, feel free to leave a comment below.

Thank You for Reading This Article

Thank you for taking the time to read this article. If you enjoyed it, please check out our other articles. We are always looking to improve, so feel free to leave us your feedback.

FAQs

Q: What is Let’s Encrypt?
A: Let’s Encrypt is a free, open-source Certificate Authority that provides free digital certificates to organisations and individuals alike.

Q: How can I set up an HTTPS server on Centos 7?
A: To set up an HTTPS server on Centos 7, you will need to install Certbot, generate the certificate, configure Nginx to use it, and test it to make sure everything is working properly.

Leave a Reply

Your email address will not be published. Required fields are marked *