[Node] AWS EC2 Deployment

·

4 min read

[Node] AWS EC2 Deployment

Launch Instance on EC2

ssh -i <.pem file> ubuntu@<public IP address>

Install Node.js on EC2 Instance

curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash -

sudo apt-get install -y nodejs

Install & Execute MongoDB on EC2 Instance

sudo apt-get install gnupg

curl -fsSL https://pgp.mongodb.com/server-6.0.asc | \
   sudo gpg -o /usr/share/keyrings/mongodb-server-6.0.gpg \
   --dearmor

echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-6.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list

sudo apt-get update

sudo apt-get install -y mongodb-org
sudo service mongod start

Execute Server Program

  1. Copy Github Repo HTTPS and clone it on Bash under Ubuntu

  2. npm install

  3. Inbound rules

  4. Unable to connect through port 3000

    1. It was because the server firewall hasn't allowed for us to connect to port 3000 yet.

    2. Using iptables to set up rules that forward requests coming in on port 80 to port 3000, which is used internally in Linux.

    3. Use this command

       sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000
      

IP Address vs. Port

  1. IP Address

The unique address that each computer has to enable communication is commonly referred to as an IP address. This is the specific address that allows all communicable devices to engage in communication.

It can be easier to understand IP addresses as similar to coordinates that indicate the location of each device.

These addresses are divided into two types: public IP addresses and private IP addresses. Generally, to use the internet, at least one public IP address is required.

  1. Port

All devices with an IP address, which are capable of communication, have channels called ports through which data can flow.

Among the programs running on a device, those that require communication notify the operating system, saying "I will use this channel! Don't let other programs use it to avoid confusion with mixed data!" It is a principle that multiple programs cannot share the same port.

In other words, it means that one port cannot be used by multiple programs simultaneously.

Port-forwarding: iptables

The program that acts as a firewall within a Linux operating system, excluding AWS, is called iptables.

All incoming requests to Linux are processed according to the rules defined by iptables.

When we access a website using a web browser, it is usually set to use port 80 by default.

  1. For example, accessing [http://www.google.com](http://www.google.com) is equivalent to accessing http://www.google.com:80.

This is because it is conventionally agreed that the default port for HTTP protocol is 80.

sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000

PM2 library

pm2 is a process management tool that helps to manage Node.js applications more conveniently. It allows for tasks such as keeping the web server running even after the terminal is closed, and monitoring log information in real-time to better manage the current status of the server in a faster and more efficient manner.

sudo -s

npm install -g pm2

pm2 start app.js

pm2 delete 0

Other commands

pm2 restart app.js. # Restart app.js service
pm2 restart [idNumber]

pm2 list # List current services

pm2 delete [pid number] # Delete service with ID

pm2 log

pm2 logs --lines [number] # Outputs the last [number] lines of logs in sequential order

DNS

DNS stands for Domain Name System. It is a system used to translate human-friendly domain names, such as example.com, into IP addresses, which are numerical addresses used by computers to identify each other on the internet. DNS serves as a critical component of the internet infrastructure, allowing users to access websites, send emails, and perform various online activities using domain names instead of having to remember the numerical IP addresses.