Inteliny
DevOpsLevel: Intermediate120mVerified Production Blueprint

Deploy a MERN Stack Application on AWS EC2

Learn to deploy a MERN Stack application on AWS EC2 using Ubuntu, Nginx, PM2, MongoDB Atlas, SSL, and a custom domain.

Inteliny Engineering

Principal Architect

Prerequisites & System Requirements

Ensure your development workstation or staging server fulfills the following prerequisites before initiating commands:

Node.js v18.0+ runtime environment
MongoDB v6.0+ database cluster
Active AWS or Cloudflare account with DNS access
Linux Ubuntu 22.04 LTS server instance
Basic knowledge of CLI bash & Git workflow

Target System Architecture Diagram

Browser
NGINX
Node / Express
MongoDB / Redis

Interactive Execution Checklist

0/3 Completed

Step-by-Step Implementation Guide

1

Initial Infrastructure & Environment Setup

Provision core Linux environment variables, firewall rules, and Node.js runtime dependencies.

Execute Command Terminal:

bash
sudo apt update && sudo apt upgrade -y
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs nginx git docker.io

Expected Terminal Output:

✔ node v20.11.0 installed
✔ nginx v1.18.0 (Ubuntu) active
✔ docker v24.0.5 running
Common Error & Troubleshooting:

Permission denied error during apt-get update.Ensure you are running commands with 'sudo' or logged in as root user.

2

Configure Environment Secrets & Variables

Create a isolated .env file to store MongoDB connection URI, JWT secrets, and API keys securely.

Execute Command Terminal:

bash
cat << 'EOF' > .env
NODE_ENV=production
PORT=8000
MONGO_URI=mongodb+srv://admin:secret@cluster0.mongodb.net/production
JWT_SECRET=super_secure_enterprise_secret_key_99
EOF

Expected Terminal Output:

✔ .env file created with mode 0600 (owner read/write only)
Common Error & Troubleshooting:

JWT Secret missing during app bootstrap.Verify .env path is loaded at app entry point using dotenv.config().

3

Deploy PM2 Process Manager & Reverse Proxy

Set up PM2 cluster mode for zero-downtime reloads and link NGINX as an SSL reverse proxy.

Execute Command Terminal:

bash
sudo npm install -g pm2
pm2 start server.js -i max --name 'inteliny-api'
pm2 save && pm2 startup

Expected Terminal Output:

[PM2] App [inteliny-api] launched in cluster mode (4 workers)
[PM2] Freeze a system startup script
Common Error & Troubleshooting:

Process restarts repeatedly (Crash Loop).Check application log files with 'pm2 logs inteliny-api --err'.

Production Best Practices & Hardening

Security Hardening

Disable root SSH access, enforce key-based auth, and enable UFW firewall on ports 80/443.

Memory Management

Set Node.js max-old-space-size to 80% of total RAM to avoid Linux OOM-killer crashes.

Frequently Asked Questions

Yes, all NGINX, Docker, and PM2 deployment steps can be packaged into Infrastructure as Code (IaC) playbooks.

Need Help Implementing This?

Partner with Inteliny's principal architects to audit your stack, automate CI/CD, and accelerate deployment.