Facebook Pixel

Installing Node.js and PM2 on AWS EC2 Setting Up the Production Environment

Learn how to install Node.js, npm, PM2, and other essential tools on an AWS EC2 Ubuntu instance for running Node.js applications in production.

Installing Node.js and PM2 on AWS EC2

After launching your EC2 instance, the next step is installing Node.js, PM2, and other tools needed to run your application.

Step 1: Update the System

sudo apt update && sudo apt upgrade -y

This updates all packages to their latest versions. Always do this first on a new instance.

Step 2: Install Node.js 20

Using NodeSource repository for the latest LTS version:

# Add NodeSource repository curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - # Install Node.js sudo apt install -y nodejs

Verify the installation:

node --version # v20.x.x npm --version # 10.x.x

Step 3: Install PM2

PM2 is a process manager for Node.js. It keeps your app running, restarts on crash, and starts on system boot.

sudo npm install -g pm2

Verify:

pm2 --version # 5.x.x

Step 4: Install Nginx

Nginx will serve as a reverse proxy, sitting in front of your Node.js app:

sudo apt install -y nginx

Verify:

nginx --version # nginx version: nginx/1.18.0

Step 5: Install Git

sudo apt install -y git git --version

Step 6: Install Build Tools

Some npm packages require compilation:

sudo apt install -y build-essential

Step 7: Configure PM2 Startup

Make PM2 start on system boot:

pm2 startup systemd

This outputs a command. Copy and run it:

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

Step 8: Clone Your Project

cd /home/ubuntu git clone https://github.com/yourusername/devtinder-backend.git cd devtinder-backend npm install

Step 9: Configure Environment Variables

cp .env.example .env nano .env

Add your production values:

PORT=3000 MONGODB_URI=mongodb+srv://your-atlas-connection-string JWT_SECRET=your_production_secret NODE_ENV=production CLIENT_URL=https://yourdomain.com

Step 10: Start the App with PM2

pm2 start src/server.js --name devtinder-api

Verify it's running:

pm2 status

You should see:

┌────────────────┬────┬───────────┬───────┬───────────┐
│ name           │ id │ mode      │ status│ restarts  │
├────────────────┼────┼───────────┼───────┼───────────┤
│ devtinder-api  │ 0  │ fork      │ online│ 0         │
└────────────────┴────┴───────────┴───────┴───────────┘

Step 11: Save PM2 Process List

pm2 save

This saves the current process list so PM2 knows what to restart on boot.

Step 12: Test the API

curl http://localhost:3000/api/health

Or from your local machine:

curl http://your-ec2-public-ip:3000/api/health

(Only works if port 3000 is open in your security group.)

PM2 Commands Cheat Sheet

CommandPurpose
pm2 start app.js --name myappStart an app
pm2 stop myappStop the app
pm2 restart myappRestart the app
pm2 delete myappRemove from PM2
pm2 statusView all processes
pm2 logs myappView logs
pm2 monitReal-time monitor
pm2 saveSave process list
pm2 startupGenerate startup script

The Takeaway

Setting up a Node.js production environment on EC2 involves: updating the system, installing Node.js 20 via NodeSource, installing PM2 globally for process management, installing Nginx for reverse proxy, installing Git and build tools, configuring PM2 startup for auto-restart on boot, cloning the project, configuring .env, and starting the app with PM2.

Use the NodeSource repository: curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -, then sudo apt install -y nodejs. This installs Node.js 20 LTS and npm.

PM2 is a process manager for Node.js. It keeps your app running, restarts on crash, starts on system boot, and provides log management. Without PM2, the app stops when the SSH session ends or on an unhandled error.

Run pm2 startup systemd, copy and run the outputted command, then run pm2 save to save the current process list. PM2 will automatically restart your apps when the server reboots.

Node.js 20 LTS (via NodeSource), PM2 (sudo npm install -g pm2), Nginx (sudo apt install nginx), Git (sudo apt install git), and build-essential for npm packages that require compilation.

Navigate to your project directory, then run pm2 start src/server.js --name devtinder-api. Verify with pm2 status. Run pm2 save to persist the process list so PM2 restarts it on boot.

Ready to master Node.js completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.