Deploy a Node.js App on EC2

In previous article you must’ve learned how to create a EC2 instance and connect to it using ssh. Let’s spin up a simple Node.js server.

1. Connect to Your EC2 Instance

ssh -i "your-key.pem" ec2-user@<your-public-ip>

Once connected via SSH to your EC2 instance, you’ll be inside the server, where you can directly write and execute code. You can either use a terminal editor like nano or connect through VSCode Remote SSH for a smoother development experience.


2. Install Node.js

sudo yum update -y
curl -sL https://rpm.nodesource.com/setup_18.x | sudo bash -
sudo yum install -y nodejs git

3. Create a Simple Server

// server.js
const http = require('http');
const server = http.createServer((req, res) => {
  res.end('Hello from EC2!');
});
server.listen(3000);
node server.js

Visit http://<your-ec2-public-ip>:3000 (Make sure port 3000 is open in the security group)


4. Keep Your Server Running with PM2

Install PM2 to keep your app alive even after you close the terminal:

npm install -g pm2
pm2 start server.js
pm2 save
pm2 startup

(Optional) Allocate and associate an Elastic IP to your instance so the public IP doesn’t change.