Site logo
Authors
  • avatar Nguyễn Đức Xinh
    Name
    Nguyễn Đức Xinh
    Twitter
Published on
Published on

Deploy Node.js and Express Application to AWS EC2

Node.js and Express are popular and powerful tools widely used in backend development. In this article, we'll walk through the steps to deploy a Node.js application on AWS EC2, leveraging PM2 and Nginx to handle common challenges.

Requirements

Before starting, ensure you have:

  • An AWS account.
  • A ready-to-deploy Node.js application using Express.

Setting up an AWS EC2 Instance

1. Create an EC2 Instance

Follow the guide here: How to Create AWS EC2 . Remember to open ports 443, 80, and 22 for your IP address.

ExpressJS on EC2

2. Connect to the EC2 Instance

Use SSH to connect to your instance:

ssh -i /path/to/key.pem ec2-user@<EC2_PUBLIC_IP>

# Example:
ssh -i ~/.ssh/xinhnd-demo.pem ec2-user@35.77.47.31

Install Node.js on EC2

1. Update the System

For AL2023:

sudo dnf update -y

For Ubuntu:

sudo apt update -y

2. Install Node.js

For AL2023:

sudo dnf install -y nodejs

For Ubuntu:

sudo apt install nodejs npm -y

Verify the installation:

node -v
npm -v

For detailed installation steps, refer to: Node.js and NPM Installation Guide for MacOS, Windows, Linux .

Deploy Node.js Application

Here, we'll use Git to deploy the code to the AWS EC2 server. We'll use a prepared Express app example: https://github.com/ducxinh/express-example-app .

1. Install Git

Run the following command:

For AL2023:

sudo dnf install -y git

For Ubuntu:

sudo apt install -y git

Verify the version:

git --version
# Output: git version 2.43.0

2. Clone the Code onto the Server

cd $HOME
git clone https://github.com/ducxinh/express-example-app.git expressjs-app

3. Install Dependencies

Navigate to the app directory on EC2:

cd $HOME/expressjs-app
npm install

4. Run the Application

npm run start

Access http://<EC2_PUBLIC_IP>:3000 in your browser. If you can't access it, it's because port 3000 isn't open. Open port 3000 using your AWS security group settings. Reload your browser and check the result:

ExpressJS on EC2

While the app is now running, exiting the SSH session will stop the app. To ensure continuous uptime, we use pm2.

Setting up PM2 with Node.js

PM2 is a process manager for Node.js applications. It ensures the app keeps running, even after unexpected errors or server reboots.

1. Install PM2

sudo npm install -g pm2

2. Start the App with PM2

cd $HOME/expressjs-app
pm2 start npm --name "express-demo" -- run start

3. Persist the Process for Reboots

Run the following commands:

pm2 save
pm2 startup

Follow the on-screen instructions to enable the startup script.

sudo env PATH=$PATH:/usr/bin /usr/local/lib/node_modules/pm2/bin/pm2 startup systemd -u ubuntu --hp /home/ubuntu

4. Restart the App

If you make changes or need to restart the app:

pm2 restart express-demo

Reboot your EC2 instance to verify that the app auto-restarts:

sudo reboot

Once rebooted, access http://<EC2_PUBLIC_IP>:3000 to confirm the app is running.

ExpressJS on EC2

Configure Reverse Proxy with Nginx

To make the app accessible on port 80 (HTTP) and enhance scalability, use Nginx as a reverse proxy.

1. Install Nginx

For AL2023:

sudo dnf install nginx -y

For Ubuntu:

sudo apt install -y nginx

2. Enable Nginx

sudo systemctl enable nginx

3. Configure Nginx

Edit the configuration file:

sudo vi /etc/nginx/conf.d/express_js.example.conf

Add the following:

server {
    listen 80;
    server_name 35.77.47.31;

    location / {
        proxy_pass http://127.0.0.1:3000;
        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;
    }
}

Test the configuration and restart Nginx:

sudo nginx -t
sudo systemctl restart nginx

Verify

  1. Access http://<EC2_PUBLIC_IP> to see the Node.js app running through Nginx.

AWS EC2 ExpressJS Nginx

  1. Check application logs:
pm2 logs

Conclusion

With this guide, you have successfully deployed a Node.js and Express app on AWS EC2, integrated with Nginx for optimized access. This is a solid foundation for deploying scalable applications on the cloud.

Further Enhancements

  • Containerization: Use Docker to containerize your app and consider Elastic Beanstalk or ECS for deployment.
  • SSL: Install HTTPS using AWS Certificate Manager or Let’s Encrypt.
  • Scaling: Combine Auto Scaling and Load Balancer to ensure scalability.
  • Monitoring: Leverage CloudWatch for performance monitoring and logging.