Skip to content

PM2 Setup

Amit Das edited this page Apr 28, 2026 · 1 revision

⚡ PM2 Setup

This guide explains how to run SecureAuth using PM2.

PM2 is a production process manager for Node.js applications that helps keep SecureAuth running continuously.


🚀 Why Use PM2?

Benefits of PM2:

  • Automatic restarts
  • Crash recovery
  • Process monitoring
  • Background execution
  • Startup on server reboot

📋 Requirements

Before starting:

  • Node.js installed
  • npm installed
  • SecureAuth project configured

📦 Install PM2

Install PM2 globally.

npm install -g pm2

🔍 Verify Installation

pm2 -v

📂 Navigate to Project

cd SecureAuth

🔐 Configure Environment Variables

Ensure .env is properly configured.

Example:

# ===============================================
# SecureAuth - ENVIRONMENT CONFIGURATION
# ===============================================

APP_NAME="SecureAuth"
APP_URL="https://example.com"

# TEXTSNAP CONFIG
TEXTSNAP_INSTANCE_ID="YOUR_INSTANCE_ID"
TEXTSNAP_ACCESS_TOKEN="YOUR_ACCESS_TOKEN"

# FIREBASE CONFIG
FIREBASE_DATABASE_URL="YOUR_FIREBASE_DATABASE_URL"
FIREBASE_DATABASE_SECRET="YOUR_FIREBASE_DATABASE_SECRET"

# SECURITY CONFIG
JWT_SECRET="YOUR_SECRET_KEY"
ENCRYPTION_KEY="YOUR_ENCRYPTION_KEY"

🏗 Build Project

Before starting production server:

npm run build

▶️ Start Application with PM2

Run SecureAuth using PM2.

pm2 start npm --name secureauth -- start

📊 View Running Processes

pm2 list

📜 View Logs

View application logs.

pm2 logs secureauth

🔄 Restart Application

pm2 restart secureauth

⏹ Stop Application

pm2 stop secureauth

❌ Delete Application

pm2 delete secureauth

💾 Save PM2 Processes

Save active processes.

pm2 save

🔁 Enable Auto Startup

Enable PM2 startup on server reboot.

pm2 startup

Run the command PM2 provides after execution.


📊 Monitor Application

Open PM2 monitoring dashboard.

pm2 monit

📁 PM2 Ecosystem Configuration (Optional)

Create ecosystem config file.

Example:

module.exports = {

  apps: [

    {

      name: "secureauth",

      script: "npm",

      args: "start",

      cwd: "/path/to/SecureAuth",

      env: {

        NODE_ENV: "production"

      }

    }

  ]

}

▶️ Start Using Ecosystem File

pm2 start ecosystem.config.cjs

🔄 Reload Without Downtime

pm2 reload secureauth

📦 Recommended Production Setup

Recommended architecture:

Client
  ↓
Nginx Reverse Proxy
  ↓
PM2 Managed SecureAuth Process
  ↓
Authentication & API Handling

🔒 Security Recommendations

For production:

  • Use HTTPS
  • Restrict server access
  • Use strong environment secrets
  • Monitor logs regularly

⚠️ Common Issues


App Not Starting

Check logs:

pm2 logs secureauth

Port Already In Use

Check port usage:

lsof -i :3000

App Crashing Repeatedly

Restart app:

pm2 restart secureauth

PM2 Not Found

Reinstall globally:

npm install -g pm2

🔍 Verify Application

Test locally:

curl http://127.0.0.1:3000

📱 Recommended with Nginx

PM2 should be combined with Nginx for:

  • Reverse proxy support
  • HTTPS handling
  • Better security

✅ Next Steps

Continue with:


Secure • Fast • Modern ⚡

Clone this wiki locally