Skip to content

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.

NSSMPM2
Runs the app in the backgroundYesYes
Restarts the app if it crashesYesYes
Writes and rotates logsYes, through NSSM settingsYes, through the pm2-logrotate module
Starts the app when the server bootsYes, as a Windows ServiceNot on its own. You set this up with Task Scheduler.
Where it keeps its settingsThe Windows Service registry entryThe .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.

  1. 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.
  2. Decide which Windows account runs PM2. Use a dedicated service account if your IT team provides one. Run every pm2 command on this page as that account.
  3. Install PM2 and its log rotation module:
    Terminal window
    npm install -g pm2
    pm2 install pm2-logrotate
    pm2 set pm2-logrotate:max_size 10M
    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.
  1. 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.js and a .env in your deployment folder, for example C:\apps\my-app\.
  2. Create C:\apps\my-app\ecosystem.config.js, next to server.js. This file tells PM2 how to run your app. It replaces the nssm install and nssm set commands:
    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' },
    }],
    };
    cwd must point at the deployment folder, because the app reads its .env from there. Keep exec_mode: 'fork' and instances: 1. PM2’s cluster mode runs several copies of the app, which a Stadium 8 app doesn’t need.
  3. Start the app and save PM2’s list of running apps:
    Terminal window
    cd C:\apps\my-app
    pm2 start ecosystem.config.js
    pm2 save
    pm2 save writes the list to the account’s .pm2 folder. The startup task in the next step reads it back.
  4. Set up IIS as the reverse proxy, exactly as described in Set up IIS as the reverse proxy. Nothing on the IIS side changes.

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:

Terminal window
$account = "DOMAIN\svc-my-app"
$pm2 = (Get-Command pm2.cmd).Source
$action = New-ScheduledTaskAction -Execute "cmd.exe" -Argument "/c `"$pm2`" resurrect"
$trigger = New-ScheduledTaskTrigger -AtStartup
Register-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.

Open your site in a browser. It should load over HTTPS without a port number.

Terminal window
pm2 list # is the app online, and how often has it restarted?
pm2 logs my-app --lines 50 # recent output and errors

PM2 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.

The steps match the Windows Server update steps, with PM2 commands in place of NSSM ones:

ChangeWhat to run
A new buildpm2 stop my-app, copy the new files, then pm2 start my-app
A server-side value in .envpm2 restart my-app
A new or renamed app in ecosystem.config.jspm2 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.

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.