⚡ PM2 Server¶
Process manager for Node.js applications and Parse Server management
🎯 Overview¶
PM2 Server is a process manager for Node.js applications that manages the Parse Server and other Node.js services across the Appgain platform. It provides process monitoring, automatic restarts, load balancing, and log management for production Node.js applications.
🏗️ Architecture¶
Core Components¶
- Process Manager: Manages Node.js application lifecycle
- Load Balancer: Distributes traffic across multiple instances
- Monitor: Real-time process monitoring and metrics
- Log Manager: Centralized log collection and management
- Cluster Mode: Multi-instance application deployment
Technology Stack¶
- PM2: Process manager for Node.js
- Node.js: Runtime environment
- Parse Server: Backend-as-a-Service applications
- Redis: Session and cache management
- MongoDB: Data persistence
Infrastructure¶
- Server:
ovh-parse-server(Main Parse server) - Port: 1337 (Parse Server), 3000 (PM2 Dashboard)
- Environment: Production, Staging, Development
- Instances: Multiple Parse Server instances
🔧 Configuration¶
Server Details¶
- Server:
ovh-parse-server - Parse Server Port: 1337 (HTTP), 1338 (HTTPS)
- PM2 Dashboard Port: 3000
- Environment: Production, Staging, Development
PM2 Configuration¶
// ecosystem.config.js
module.exports = {
apps: [{
name: 'parse-server',
script: './bin/parse-server',
instances: 4,
exec_mode: 'cluster',
env: {
NODE_ENV: 'production',
PORT: 1337,
PARSE_SERVER_APPLICATION_ID: 'appgain',
PARSE_SERVER_MASTER_KEY: 'your-master-key',
PARSE_SERVER_DATABASE_URI: 'mongodb://localhost:27017/appgain'
},
error_file: './logs/err.log',
out_file: './logs/out.log',
log_file: './logs/combined.log',
time: true
}]
}
Process Management¶
# Start all applications
pm2 start ecosystem.config.js
# Restart specific app
pm2 restart parse-server
# Stop application
pm2 stop parse-server
# Delete application
pm2 delete parse-server
# List all processes
pm2 list
# Monitor processes
pm2 monit
📊 Process Management¶
Application Lifecycle¶
# Start application
pm2 start app.js
# Restart application
pm2 restart app.js
# Reload application (zero-downtime)
pm2 reload app.js
# Stop application
pm2 stop app.js
# Delete application
pm2 delete app.js
Cluster Mode¶
# Start with multiple instances
pm2 start app.js -i 4
# Scale to specific number of instances
pm2 scale app.js 8
# Start with max instances
pm2 start app.js -i max
Environment Management¶
# Start with specific environment
pm2 start ecosystem.config.js --env production
# Set environment variables
pm2 start app.js --env NODE_ENV=production
# Use ecosystem file
pm2 start ecosystem.config.js
🔍 Monitoring & Logs¶
PM2 Commands¶
# List all processes
pm2 list
# Monitor processes
pm2 monit
# Show logs
pm2 logs
# Show specific app logs
pm2 logs parse-server
# Show real-time logs
pm2 logs --lines 100
# Flush logs
pm2 flush
Dashboard¶
Metrics¶
# Show process metrics
pm2 show parse-server
# Show system metrics
pm2 show
# Monitor CPU/Memory
pm2 monit
🔒 Security¶
Access Control¶
- Authentication: PM2 Plus authentication
- Process Isolation: Each app runs in isolated environment
- Environment Variables: Secure credential management
- Log Security: Encrypted log storage
Best Practices¶
- Environment Variables: Use .env files for sensitive data
- Process Naming: Use descriptive process names
- Log Rotation: Implement log rotation policies
- Resource Limits: Set memory and CPU limits
📈 Performance¶
Capabilities¶
- Process Scaling: 1-32 instances per application
- Load Balancing: Automatic traffic distribution
- Zero Downtime: Reload without service interruption
- Memory Management: Automatic memory optimization
Limitations¶
- Memory Per Instance: 512MB default limit
- CPU Usage: 100% per core maximum
- Log Size: 10MB per log file
- Process Count: 100 maximum processes
🔗 Integration Examples¶
Parse Server Management¶
# Start Parse Server with PM2
pm2 start ecosystem.config.js
# Monitor Parse Server
pm2 monit parse-server
# Check Parse Server logs
pm2 logs parse-server --lines 50
Multiple Applications¶
# Start multiple apps
pm2 start ecosystem.config.js
# Scale specific app
pm2 scale parse-server 4
pm2 scale notify-service 2
# Monitor all apps
pm2 monit
Production Deployment¶
# Deploy with PM2
pm2 deploy ecosystem.config.js production
# Update application
pm2 reload parse-server
# Rollback if needed
pm2 reload parse-server --update-env
🛠️ Troubleshooting¶
Common Issues¶
Process Crashes¶
# Check crash logs
pm2 logs parse-server --err
# Restart crashed process
pm2 restart parse-server
# Check memory usage
pm2 show parse-server
High Memory Usage¶
# Check memory usage
pm2 monit
# Restart to free memory
pm2 reload parse-server
# Set memory limit
pm2 start app.js --max-memory-restart 300M
Log Issues¶
Debug Commands¶
# Check PM2 status
pm2 status
# Show detailed info
pm2 show parse-server
# Check system resources
pm2 monit
# View startup logs
pm2 startup
📚 Documentation¶
External Resources¶
- PM2 Documentation: PM2 Official Docs
- Node.js Process Management: Node.js Guide
- Parse Server PM2: Parse Server PM2 Guide
Monitoring Tools¶
- PM2 Plus: Web-based monitoring dashboard
- Prometheus: Metrics collection and alerting
- Grafana: Dashboard visualization
- Custom Analytics: Process performance tracking
Development Tools¶
- PM2 DevTools: Development utilities
- PM2 Logs: Log management and analysis
- PM2 Deploy: Deployment automation
- PM2 Testing: Automated testing integration
🔗 Related Resources¶
- System Architecture? → System Architecture
- Parse Server? → Parse Server
- DevOps Tools? → DevOps Engineer Guide
- API Gateway? → API Gateway
- Learning Resources? → Learning Resources
Ask Chehab GPT