π Deploy Node.js API with PM2 on Windows (Offline + Auto-Restart on Boot)
When deploying a Node.js API on a Windows server without internet access, traditional online installation steps won’t work. This guide walks you through offline deployment with PM2 and configuring it to auto-start on reboot using pm2-windows-startup
.
✅ Prerequisites
-
A development machine with Node.js and internet access.
-
The target Windows server has:
-
Node.js installed
-
No internet access
-
Your Node.js API project copied
-
π¦ Step 1: Prepare Offline PM2 Package
On a machine with internet:
Then zip the folder:
π Step 2: Transfer to Target Server
Copy these to the Windows server:
-
pm2-offline.zip
-
Your Node.js API project
Unzip both:
π ️ Step 3: Start API with PM2
Open Command Prompt or PowerShell and run:
Replace index.js
with your actual entry file.
πΎ Step 4: Save PM2 Process List
This saves the process list for auto-restore.
π Step 5: Auto-Start on Reboot (PM2 on Windows)
Install PM2 Windows startup module:
Then run:
This registers PM2 as a Windows service to auto-start after reboot.
π― After Reboot
-
Your API starts automatically
-
It stays alive forever (auto-restarts on crash)
-
No internet required!
π§ͺ Helpful PM2 Commands (Offline)
π‘ Summary
Task | Command |
---|---|
Start app | pm2 start index.js --name your-api |
Save app list | pm2 save |
Auto-start on reboot | pm2-startup install |
Run offline PM2 commands | node path\to\pm2\bin\pm2 <command> |
π Final Tip
For even smoother startup, you can create a .bat
file that restores the saved PM2 processes on boot, or configure it in Windows Task Scheduler if needed.
Comments
Post a Comment