Blogs

Let's Encrypt: Get Your Free SSL Certificate - Part 1


Introduction 🌟

Hey everyone, In this blog, I'll show you how to easily get a free SSL certificate for your domain. We are going to do this through Let's Encrypt.
I'll also be using Nginx but you're free to use any other software you prefer like Apache for example.
There are multiple methods to achieve this, but I'm going to cover the 2 methods I prefer in this blog and the one after .

Let's Encrypt is a non-profit certificate authority run by Internet Security Research Group that provides X.509 certificates for Transport Layer Security encryption at no charge.

Requirements πŸ““

  1. A registered domain name (you can use GoDaddy, AWS Route53, etc.)
  2. A web server (you need admin access on that server).

Preparing a demo server πŸš€

Before we start, let's prepare our web server to serve a hello world html page!
For this demo, I'll be using an AWS EC2 instance with a public IP address and GoDaddy as my domain name registrar.
Steps:

  1. Launch an EC2 instance. (make sure you enable the Auto-assign public IP)
  2. Log into your webserver using ssh.
  3. Run the following commands:
    1. sudo su
    2. yum update -y
    3. yum install nginx
    4. systemctl start nginx

These commands will update your instance, install nginx, and start the server.

You can verify that your server is running by navigating to your instance public ip address (for example http://xx.xx.xx.xx) and you should be able to see the Nginx server default page.

If you're using AWS EC2 like me in this blog, make sure that your assigned security groups and NACLs are not blocking traffic on port 80.

Okay, now we have our web server up and running, let's make our domain name point to our web server.
To do that, simply create an A record with the value of your public IP address.

This might take a while until your DNS propagations happen.. usually within an hour (mine took 10 minutes).

Once you're able to access your web server through your domain, you're ready to request a certificate from Let's Encrypt.

Please note that this method will not work if your website is not online and not available on port 80.

Obtaining a certificate through Let's Encrypt πŸš€ πŸš€

Now, we're ready to request a certificate:

  1. Open the Nginx configs using vim.

    sudo vim /etc/nginx/nginx.conf
  2. Then add the below section to your server block:

    location /.well-known/acme-challenge/ {
    root /var/www/html; # This directory needs to exist
    }

    Your configs file will look something like this

    server {
    listen 80;
    server_name your-domain.com www.your-domain.com;

    location / {
    root /usr/share/nginx/html; # Adjust as necessary index index.html index.htm;
    }

    # This will allow Certbot to validate your domain
    location /.well-known/acme-challenge/ {
    root /var/www/html;
    }
    }
  3. reload nginx service

    sudo systemctl reload nginx
  4. Install certbot-nginx

    • On Amazon Linux: sudo yum install certbot-nginx -y
    • On Ubunutu: sudo apt install certbot python3-certbot-nginx -y
  5. Obtain your certificate using this command

    sudo certbot --nginx -d your-domain.com -d www.your-domain.com
  6. Agree on the terms and conditions and once you're done, you will have your certificates ready to use and stored in /etc/letsencrypt/live/{your-domain}.

  7. Update you nginx server configs to use your new certificate.

    Your new configs should look similar to below:

    server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;

    # Redirect HTTP to HTTPS
    return 301 https://$host$request_uri;
    }

    server {
    listen 443 ssl;
    server_name yourdomain.com www.yourdomain.com;

    ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem; # Path to your SSL certificate
    ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem; # Path to your private key

    # Optional: SSL settings for better security
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers 'HIGH:!aNULL:!MD5';

    location / {
    root /var/www/html; # Path to your web root
    index index.html index.htm index.php;
    }

    # Additional location blocks can go here
    }
  8. Reload your nginx server

    sudo systemctl reload nginx

Summary βœ…

Now, you should be able to access your website using HTTPS.
Again, if you're using AWS EC2 with assigned security groups and NACLs, make sure you allow traffic on port 443.
That's it for this blog; hope you find it helpful!