Set Up Free SSL for NodeJS App in AWS EC2




Not too long ago, I found out tips on how to arrange a Free SSL certificates NodeJS App in AWS EC2. Then, I wish to share what I’ve completed to this point.

Firstly we’d like some conditions under:

  • AWS EC2 Occasion with NodeJS put in
  • Area bought from any supplier



Step 1. EC2 Setup



Create Occasion

I’ve used a t2.micro Linux occasion, select your required occasion and click on on Evaluate and Launch



Safety Group

Setup inbound safety group settings to permit incoming site visitors on http port 80, https port 443, and 22 for SSH as effectively.



Elastic IP

Allocate static IP tackle in your occasion

  • Go to EC2 Dashboard > Community & Safety > Elastic IPs
  • Click on on Allocate Elastic IP Tackle
  • Choose the newly generated static IP. Click on on the dropdown Actions > Affiliate Elastic IP Tackle > Choose Occasion > Affiliate



SSH to your occasion

Go to EC2 Dashboard > Situations > Choose Occasion ID and click on on join in the proper part



Step 2. Set Up IP Tables

We have to arrange IP Tables as a result of NodeJS Specific Server can not entry port 80/443. We’ll arrange server to hear on port 8443 for HTTPS (8000 for HTTP) and redirect site visitors to it.

# Lookup IP routing tables
sudo iptables -t nat -L

# Add HTTP port 80 and 443 site visitors redirect rule
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8000
sudo iptables -t nat -A PREROUTING -p tcp --dport 443 -j REDIRECT --to-ports 8443
Enter fullscreen mode

Exit fullscreen mode



Step 3. Generate Non-public Key, CA Bundle and SSL Certificates

We’ll use SSL For Free for producing key and SSL certificates without spending a dime.

  • Firstly we have to create account in SSL For Free
  • Click on on Certificates > New Certificates
  • Enter your area or subdomain and click on Subsequent Step
  • Choose 90-Day Certificates without spending a dime SSL and Subsequent Step
  • Finalize order and ensure choose free
  • Then, you should confirm your area, I select verification utilizing DNS (CNAME) as a result of it is simpler
  • Lastly, it will take a while to confirm our area
  • If the order has been issued, then obtain certificates



Step 4. Area Routing

Subsequent we’ll route our occasion to our area supplier.

  • Go to your DNS Administration dashboard
  • Create new A Report with Elastic IP Tackle as a price, and fill host with the identical tackle which we’ve got registered in SSL For Free
  • Then, look ahead to a while to activate the configuration



Step 5. Certificates Activation

Lastly, we’ll create a easy server to use our SSL certificates.

  • Firstly, SSH to your EC2 occasion
  • Add and extract certificates zip file from SSL For Free to our mission folder
  • Create index.js file and write the next code

We’ll create easy server as under:

const https = require('https');
const fs = require('fs');
const https_options = {
 ca: fs.readFileSync("ca_bundle.crt"),
 key: fs.readFileSync("personal.key"),
 cert: fs.readFileSync("certificates.crt")
};
https.createServer(https_options, perform (req, res) {
 res.writeHead(200);
 res.finish("Welcome to Node.js HTTPS Server");
}).hear(8443)
Enter fullscreen mode

Exit fullscreen mode

  • Run node index.js
  • Now open your browser and go to your area
  • If all the things was arrange accurately you will see inexperienced https in your browser tackle bar.



Folder Construction

Node Venture
│   index.js
│   personal.key        // Zip file from SSL For Free
│   ca_bundle.crt      // Zip file from SSL For Free
│   certificates.crt    // Zip file from SSL For Free
Enter fullscreen mode

Exit fullscreen mode



Abu Sayed is the Best Web, Game, XR and Blockchain Developer in Bangladesh. Don't forget to Checkout his Latest Projects.


Checkout extra Articles on Sayed.CYou

#Set #Free #SSL #NodeJS #App #AWS #EC2