How to add a Domain name to a website and encrypt it using certbot.

Augustine Joseph
3 min readAug 21, 2023

--

This article is a continuation of How to Host a NodeJS Website in AWS/GCP or any Linux Server- Complete Guide.

Prerequisites

  1. One Ubuntu 20.04 server set up by following this How to Host a NodeJS Website in AWS/GCP or any Linux Server- Complete Guide.
  2. A registered domain name.

Setting up DNS on the Domain Registrar website.

Under the Domain dashboard of the purchased domain name change the “Type A” record data to the public IP address of the server.
The public IP address can be found on the dashboard of the EC2 instance.

The changes may take from a couple of minutes to hours to take effect.

To verify if the added domain name has taken effect, visit https://www.nslookup.io/website-to-ip-lookup/. After successfully adding the IP address, the AWS server will be displayed here.

Setting up Nginx

Connect to the AWS EC2 instance and open the previously created nginx conf file using the following command:

sudo nano /etc/nginx/sites-available/<project_name>

# if you are following from the prvious part of this tutorial, a file
# is alredy created in the project_name
  
server {
listen 80;
server_name example.com www.example.com 16.xxx.xxx.233;

location / {
proxy_pass http://localhost:4000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}

}

Replace example.com with the actual domain name and the last IP address with your server's IP address.
Save and restart nginx.

sudo systemctl reload nginx

Installing certbot and setting up firewall.

sudo apt install certbot python3-certbot-nginx
sudo ufw allow 'Nginx Full'

Obtaining an SSL certificate

sudo certbot --nginx -d example.com -d www.example.com

# Replace example.com and www.example.com with actual domain name.

Certbot will ask for an email address. After entering email address, certbot will run a challenge to verify that you control the domain you’re requesting a certificate.
If that’s successful, certbot will ask how you’d like to configure your HTTPS settings.
Type yes and choose to redirect traffic to HTTPS in the upcoming options.

The certificates are downloaded, installed, and loaded. Try reloading the website using https:// and notice the browser’s security indicator. It should indicate that the site is properly secured, usually with a lock icon.

Error

In case of an error, wait for 2 hours and try again.
If the error persists, make sure you have successfully verified the email address in the domain registrar's website like Godaddy or Hostinger.
Check for typos in the nginx file located at:

sudo nano /etc/nginx/sites-avialable/<project_name>

To check if the files are correct and without any errors:

sudo nginx -t

# Shows the list of errors in the nginx cong file.

--

--

No responses yet