# PM2 Configuration This document explains how to use PM2 for managing Deathwatch Roller in production. ## What is PM2? PM2 is a production process manager for Node.js that keeps your application running 24/7. ## Installation ```bash # Install PM2 globally npm install -g pm2 # Initialize PM2 ecosystem file pm2 init ``` ## Application Configuration Create `ecosystem.config.js`: ```javascript module.exports = { apps: [ { name: 'dwroller-frontend', cwd: './frontend', script: 'npm', args: 'start', env: { NODE_ENV: 'production', PORT: 3000 }, env_development: { NODE_ENV: 'development' }, instances: 1, autorestart: true, max_memory_restart: '500M' }, { name: 'dwroller-backend', cwd: './backend', script: 'npm', args: 'start', env: { NODE_ENV: 'production', PORT: 5000 }, env_development: { NODE_ENV: 'development' }, instances: 1, autorestart: true, max_memory_restart: '500M' } ] }; ``` ## Starting the Application ```bash # Start both apps pm2 start ecosystem.config.js # Start only backend pm2 start ecosystem.config.js --only backend # Start only frontend pm2 start ecosystem.config.js --only frontend ``` ## Managing Apps ```bash # View status pm2 list # View logs pm2 logs # Restart app pm2 restart all # Stop app pm2 stop all # Delete app pm2 delete all ``` ## Monitoring ```bash # Enable monitoring pm2 monitortype --interval 10000 # View stats pm2 stats ``` ## Logging ```bash # Rotate logs pm2 rotate logs # Save log pm2 save # Load saved PM2 process list pm2 startup pm2 save ``` ## NPM Scripts Update `package.json` with PM2 scripts: ```json { "scripts": { "start": "npm run build && pm2 start ecosystem.config.js", "pm2:start": "pm2 start ecosystem.config.js", "pm2:stop": "pm2 stop all", "pm2:restart": "pm2 restart all", "pm2:logs": "pm2 logs", "pm2:list": "pm2 list", "pm2:save": "pm2 save", "pm2:startup": "pm2 startup" } } ``` ## Production Build ```bash # Build frontend cd frontend npm run build # Start with PM2 cd ../.. npm run pm2:start ``` ## Environment Variables Store secrets in `.env` or use PM2 env: ```bash # Set environment variable pm2 set ecosystem.config.js:* env:SESSION_SECRET="your-secret" # List all env variables pm2 env ecosystem.config.js ``` ## Backup Strategy ```bash # Create database backup npm run backup # Schedule backup with cron 0 2 * * * /path/to/db/backup.sh ``` ## Scaling For horizontal scaling, configure multiple instances: ```javascript { instances: 'max', exec_mode: 'cluster', watch: false } ``` ## Auto-restart on reboot ```bash pm2 startup pm2 save ```