
Running Your Node.js Application with PM2
In Part 1 , we installed NVM, PM2, configured log rotation, and set up startup scripts. Now let's run the actual application. Prerequisites NVM, Node.js, and PM2 installed (Part 1) A Node.js application with a server.js or app.js entry point Dependencies installed ( npm install or npm ci ) Start the Application Single Instance cd /opt/myapp pm2 start server.js --name myapp That's it. PM2 is managing the process. pm2 status ┌────┬──────┬───────┬───┬─────┬──────────┐ │ id │ name │ mode │ ↺ │ cpu │ memory │ ├────┼──────┼───────┼───┼─────┼──────────┤ │ 0 │ myapp│ fork │ 0 │ 0% │ 45.2 MB │ └────┴──────┴───────┴───┴─────┴──────────┘ Cluster Mode — Use All CPU Cores If your app is stateless (no in-memory sessions), cluster mode spawns one process per CPU core: pm2 start server.js --name myapp -i max ┌────┬──────┬─────────┬───┬─────┬──────────┐ │ id │ name │ mode │ ↺ │ cpu │ memory │ ├────┼──────┼─────────┼───┼─────┼──────────┤ │ 0 │ myapp│ cluster │ 0 │ 0% │ 42.1 MB │ │ 1 │ myapp│ cluster │ 0
Continue reading on Dev.to DevOps
Opens in a new tab




