What is PM2?
PM2 is a production process manager for Node.js applications. It allows you to:- Keep applications alive forever
- Reload applications with zero downtime
- Facilitate common system admin tasks
- Manage application logging, monitoring, and clustering
PM2 Configuration
ProStack comes with a pre-configuredecosystem.config.js
file in the root directory:
Configuration Options Explained
The ecosystem file configures the following options:Option | Value | Description |
---|---|---|
name | ”app” | Name of the application in PM2 |
script | ”npx” | The command to run |
args | ”next start —port 3020” | Arguments passed to the script |
PORT | 3020 | The port on which the application runs |
NODE_ENV | ”production” | Environment setting |
watch | false | Disable file watching (more stable in production) |
max_memory_restart | ”1G” | Restart if memory exceeds 1GB |
autorestart | true | Automatically restart if the app crashes |
restart_delay | 1000 | Wait 1 second before restarting |
log_date_format | ”YYYY-MM-DD HH:mm:ss Z” | Format of log timestamps |
error_file | ”logs/error.log” | Where error logs are stored |
out_file | ”logs/output.log” | Where output logs are stored |
merge_logs | true | Merge logs from all instances |
kill_timeout | 5000 | Wait 5 seconds before force killing |
no_daemon | false | Run PM2 as a daemon |
Installing PM2
Before using PM2, you need to install it globally on your server:Deploying with PM2
To deploy your application with PM2:- Build your Next.js application:
- Start the application with PM2: