Introduction

Deploying a Node.js application to AWS EC2 is one of the most straightforward ways to get your app running on a cloud server. In this article I’ll go through the steps to launch an EC2 instance, install Node.js, deploy your app and keep it alive using a process manager like PM2.

Launch an EC2 instance

From the AWS Console, go to EC2 > Instances > Launch instances.

  • Name: give the instance a meaningful name
  • AMI: choose Amazon Linux or Ubuntu Server
  • Instance type: any micro instance is enough for testing and is usually included in the free tier
  • Key pair: create a new key pair (RSA, .pem format) and download it. You’ll need it to connect via SSH later
  • Security group: create a new security group and add the following inbound rules (click Edit near Network Settings):
    • SSH (port 22) from your IP (usually selected by default)
    • Click Add security group rule: Custom TCP on the port your app listens on (e.g. 3000, check your server code for a PORT variable) from 0.0.0.0/0.

Click Launch instance.

Connect via SSH

Once the instance is running, select it in the console and click Connect. Use the SSH client tab for the exact command. It will look like this, write it in your terminal:

chmod 400 your-key.pem
ssh -i "your-key.pem" ubuntu@<your-ec2-ip>

Replace your-key.pem and <your-ec2-ip> with your actual values.

Install Node.js

Once connected, install Node.js using nvm, following the latest up to date istructions:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.6/install.sh | bash
source ~/.bashrc
nvm install --lts

Verify the installation:

node -v
npm -v

You should see your Node.js and npm version.

Deploy your app

Clone your repository and install dependencies. Install git if not already installed on the machine:

git clone https://github.com/your-username/your-repo.git
cd your-repo
npm i

Make sure any environment variables your app needs are set. You can create a .env file or export them directly in the shell.

Keep the app running with PM2

By default, the app stops when you close the SSH session. Use PM2 to run it as a background process and restart it automatically on crash or reboot:

npm install -g pm2
pm2 start index.js --name my-app
pm2 startup
pm2 save

Optionally create a ecosystem.config.js file for deeper configuration.

Run pm2 list to check that the app is running. To view logs use pm2 logs.

Verify it works

Open a browser and navigate to http://<your-ec2-public-ip>:<port> (no https). You should see your app responding if there is a route at /. The easiest route you can make is a /ping route that simply responds with a string.

Conclusion

We’ve seen the minimal steps to get a Node.js app running on an AWS EC2 instance: launch the server, connect via SSH, install Node.js with nvm, clone the app with Git, and use PM2 to keep it alive. This setup is intentionally simple and works well for very small projects or demos. For production workloads, consider adding a reverse proxy (e.g. Nginx), HTTPS via Let’s Encrypt, and a proper CI/CD pipeline for complete automation.