Windows Server With PM2
Tested on Windows 11 with Stadium 8 v1.5.1.
Use this page if your server doesn’t allow NSSM, the tool the Windows Server guide uses to run your app as a service. PM2 does the same job. It’s a process manager for Node.js: it keeps your app running, restarts it if it crashes and manages its logs.
Only one part of the Windows Server guide changes. You still build the app, copy it to the server and set up IIS as described there. PM2 replaces the NSSM steps.
If your environment allows containers, consider Docker first. It’s the recommended deployment approach.
How PM2 differs from NSSM
Section titled “How PM2 differs from NSSM”| NSSM | PM2 | |
|---|---|---|
| Runs the app in the background | Yes | Yes |
| Restarts the app if it crashes | Yes | Yes |
| Writes and rotates logs | Yes, through NSSM settings | Yes, through the pm2-logrotate module |
| Starts the app when the server boots | Yes, as a Windows Service | Not on its own. You set this up with Task Scheduler. |
| Where it keeps its settings | The Windows Service registry entry | The .pm2 folder in the profile of the account that runs it |
The last two rows are the ones to plan for. PM2’s built-in pm2 startup command only supports Linux and macOS, so on Windows you add a startup task yourself. That task must run as the same account that runs PM2, because PM2 keeps its saved list of apps in that account’s profile.
One-time server setup
Section titled “One-time server setup”- Follow steps 1 to 3 of the one-time server setup on the Windows Server page: install Node.js 22 or newer, IIS, URL Rewrite and ARR. Skip the NSSM step.
- Decide which Windows account runs PM2. Use a dedicated service account if your IT team provides one. Run every
pm2command on this page as that account. - Install PM2 and its log rotation module:
Both commands download packages from the npm registry. If the server has no internet access, ask your IT team for a proxy setting or an offline copy of the packages.
Terminal window npm install -g pm2pm2 install pm2-logrotatepm2 set pm2-logrotate:max_size 10M
Deploy
Section titled “Deploy”- Build your project and copy it to the server, as described in Build your project and steps 1 and 2 of Deploy on the Windows Server page. You end up with
server.jsand a.envin your deployment folder, for exampleC:\apps\my-app\. - Create
C:\apps\my-app\ecosystem.config.js, next toserver.js. This file tells PM2 how to run your app. It replaces thenssm installandnssm setcommands:module.exports = {apps: [{name: 'my-app',script: 'server.js',cwd: 'C:\\apps\\my-app',exec_mode: 'fork',instances: 1,env: { NODE_ENV: 'production', PORT: 3000, HOSTNAME: '127.0.0.1' },}],};cwdmust point at the deployment folder, because the app reads its.envfrom there. Keepexec_mode: 'fork'andinstances: 1. PM2’s cluster mode runs several copies of the app, which a Stadium 8 app doesn’t need. - Start the app and save PM2’s list of running apps:
Terminal window cd C:\apps\my-apppm2 start ecosystem.config.jspm2 savepm2 savewrites the list to the account’s.pm2folder. The startup task in the next step reads it back. - Set up IIS as the reverse proxy, exactly as described in Set up IIS as the reverse proxy. Nothing on the IIS side changes.
Start the app when the server boots
Section titled “Start the app when the server boots”Add a Task Scheduler task that runs pm2 resurrect at startup. pm2 resurrect starts PM2 and every app in the list you saved with pm2 save.
Run this in PowerShell as an administrator. Replace the account with the one that runs PM2:
$account = "DOMAIN\svc-my-app"$pm2 = (Get-Command pm2.cmd).Source$action = New-ScheduledTaskAction -Execute "cmd.exe" -Argument "/c `"$pm2`" resurrect"$trigger = New-ScheduledTaskTrigger -AtStartupRegister-ScheduledTask -TaskName "PM2 Resurrect" -Action $action -Trigger $trigger -User $account -Password (Read-Host "Password for $account")Find pm2.cmd while signed in as the PM2 account. npm installs global commands into that account’s profile, so the path differs per account.
Restart the server once to confirm the app comes back on its own.
Verify
Section titled “Verify”Open your site in a browser. It should load over HTTPS without a port number.
pm2 list # is the app online, and how often has it restarted?pm2 logs my-app --lines 50 # recent output and errorsPM2 writes logs to C:\Users\<account>\.pm2\logs\. A restart count that keeps climbing in pm2 list means the app is crashing and PM2 is restarting it. Check the logs for the cause.
Updating the app and its settings
Section titled “Updating the app and its settings”The steps match the Windows Server update steps, with PM2 commands in place of NSSM ones:
| Change | What to run |
|---|---|
| A new build | pm2 stop my-app, copy the new files, then pm2 start my-app |
A server-side value in .env | pm2 restart my-app |
A new or renamed app in ecosystem.config.js | pm2 start ecosystem.config.js, then pm2 save |
Run pm2 save again whenever you add or remove an app. Otherwise the startup task brings back the old list.
Hosting multiple apps on one server
Section titled “Hosting multiple apps on one server”Add one entry per app to apps in ecosystem.config.js, each with its own name, cwd and PORT. Then set up one IIS site per app, as described in Hosting multiple apps on one server. One PM2 install and one startup task cover every app.