简体   繁体   中英

How to host a Node.js app on AWS EC2 Ubuntu free tier?

I am really new to AWS EC2. I hosted my Express.js on AWS EC2 using PM2.

Here is the current log of EC2 of my app.

I don't know whether this is working or not.

在此处输入图像描述

My public IPv4 address is (52.90.33.231).

If Nginx is required, please guide me through its steps because I have no prior experience.

I am also adding the inbound rules here.

在此处输入图像描述

http://localhost:5002/questapi

The above url used to give me the following data:

在此处输入图像描述

So 52.90.33.231/questapi is the working url.

I would prefer using Docker to run your application in EC2 instead of using PM2, It will be easy for you to migrate your application to any environment irrespective of application dependencies. PM2 is a good deployment tool but the better answer will be DOCKER.

Regarding NGINX, You can use NGINX or APACHE web servers to enable reverse proxy on your Node services to route your 5002 port to 443/80. There also I would suggest using AWS Application load balancer because it will provide the same and easy for you to install SSL certificate using AWS CERTIFICATE MANAGER.

Steps to Docker Node deployment in Ec2

  1. Install DOCKER in your EC2 machine - Follow this reference URL
  2. Clone your NodeJS codebase in the EC2 machine and add Dockerfile in the root folder in your codebase. Below I will add the Dockerfile sample.
  3. Build docker image using this command in your root folder of the project docker build --no-cache -t <your_application_name>:latest.
  4. Run NodeJs docker image using the given command

sudo docker run --name <your_application_name> -itd -.net="host"
-e 5002:5002
--restart unless-stopped
<your_application_name>:latest;

Now you can start using the application on <your_instance_public_ip>:5002 , Make sure to enable the 5002 port in security group inbound access.

In between, Here I'm adding a reference link to use Aws ALB to hide your EC2 IP address and application port number by using reverse proxying rules.

https://www.clickittech.com/devops/deploy-nodejs-app-to-aws/

DOCKERFILE Sample for NODEJS application

FROM node:14.0
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package.json /usr/src/app/

RUN npm install

# Bundle app source
COPY . .

EXPOSE 5002
CMD [ "node", "server.js" ] # you can add your start command

you should refer to this video (from Brad Traversy) where he deploys the Nodejs application on DigitalOcean droplet using pm2, but for deploying on AWS EC2 you can follow the exact same steps as both use the Ubuntu OS, NGINX and pm2 for configuring the application. NODEJS DEPLOYING TUTORIAL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM