---
title: "Cài đặt và cấu hình Node.js PM2 / NSSM trên Windows Server 2022 VPS"
description: "Hướng dẫn cài đặt Node.js LTS, cấu hình PM2 kết hợp NSSM trên Windows Server 2022 VPS để ứng dụng tự khởi động lại khi reboot và chạy nền 24/7."
canonical: "https://thuecloud.vn/cai-dat-va-cau-hinh-node-js-pm2-nssm-tren-windows-server-2022-vps"
markdown_url: "https://thuecloud.vn/cai-dat-va-cau-hinh-node-js-pm2-nssm-tren-windows-server-2022-vps.md"
locale: "vi"
kind: "article"
published_at: "2026-11-22T16:18:10.859Z"
updated_at: "2026-11-22T16:18:10.011Z"
site: "ThueCloud.Vn"
estimated_tokens: 1875
---
# Cài đặt và cấu hình Node.js PM2 / NSSM trên Windows Server 2022 VPS

> Hướng dẫn cài đặt Node.js LTS, cấu hình PM2 kết hợp NSSM trên Windows Server 2022 VPS để ứng dụng tự khởi động lại khi reboot và chạy nền 24/7.

- Canonical HTML: https://thuecloud.vn/cai-dat-va-cau-hinh-node-js-pm2-nssm-tren-windows-server-2022-vps
- Markdown URL: https://thuecloud.vn/cai-dat-va-cau-hinh-node-js-pm2-nssm-tren-windows-server-2022-vps.md
- Organization: ThueCloud.Vn
- Support Email: support@thuecloud.vn
- Hotline: 03333 499 48
- Published: 2026-11-22
- Updated: 2026-11-22

## Mục lục

- Thiết lập PM2_HOME cấp hệ thống và chạy ứng dụng Node.js
- Đăng ký Windows Service với NSSM để tự khởi động 24/7
- Câu hỏi thường gặp (FAQ)
  - Tại sao chạy pm2 list trong PowerShell không thấy process đang chạy dưới Windows Service?
  - Nên chọn NSSM hay pm2-windows-service?
  - Làm sao cập nhật code mới mà không quên khởi động lại NSSM Service?

Trên Linux, `pm2 startup` tạo ra systemd service chỉ với một câu lệnh. Nhưng trên [VPS Việt Nam](https://thuecloud.vn/vps-viet-nam) chạy Windows Server 2022, PM2 không có cơ chế tự khởi động sẵn sau khi Windows Update hoặc reboot máy chủ nếu bạn không bọc nó bằng **Windows Service**. Kết hợp **PM2** (quản lý cluster, reload zero-downtime) với **NSSM** (Non-Sucking Service Manager) là giải pháp chuẩn xác nhất để vận hành API Node.js trên Windows VPS. Xem thêm các lưu ý vận hành tại [hỏi đáp kỹ thuật](https://thuecloud.vn/faq).

> **Tóm tắt nhanh (TL;DR):**
> - **Cài đặt chuẩn:** Cài Node.js LTS vào đường dẫn không dấu cách (`C:\Program Files\nodejs`), cài PM2 toàn cục và đặt biến môi trường `PM2_HOME=C:\pm2_home` cho System.
> - **Bọc bằng NSSM:** Dùng NSSM đăng ký service cho `node.exe` hoặc `pm2-windows-service` để tiến trình chạy ở Session 0 không cần đăng nhập RDP.
> - **Biến môi trường System:** Khai báo `PM2_HOME` ở cấp **System Variables** (không phải User Variables) để Service và tài khoản Administrator dùng chung một danh sách process.
> - *Cập nhật: 11/2026*

## Thiết lập PM2_HOME cấp hệ thống và chạy ứng dụng Node.js

Mở PowerShell với quyền Administrator, thiết lập `PM2_HOME` dùng chung và khởi chạy app Node.js:

```powershell
# 1. Tạo thư mục PM2_HOME cố định và gán biến môi trường cấp System
mkdir C:\pm2_home
[Environment]::SetEnvironmentVariable("PM2_HOME", "C:\pm2_home", "Machine")
$env:PM2_HOME = "C:\pm2_home"

# 2. Cài PM2 toàn cục và chạy app mẫu
npm install -g pm2
cd C:\Apps\my-node-api
pm2 start dist\index.js --name "node-api" -i 2
pm2 save
```

## Đăng ký Windows Service với NSSM để tự khởi động 24/7

Thay vì để PM2 tự chạy qua cửa sổ CMD (sẽ tắt khi đóng phiên hoặc reboot), hãy dùng `nssm.exe` đăng ký tiến trình Node.js hoặc PM2 daemon trực tiếp thành Windows Service:

```powershell
# Đăng ký service NodeAPI bằng NSSM chạy trực tiếp binary Node.js (ổn định nhất trên Windows)
C:\Tools\nssm\nssm.exe install NodeAPI "C:\Program Files\nodejs\node.exe"
C:\Tools\nssm\nssm.exe set NodeAPI AppParameters "C:\Apps\my-node-api\dist\index.js"
C:\Tools\nssm\nssm.exe set NodeAPI AppDirectory "C:\Apps\my-node-api"
C:\Tools\nssm\nssm.exe set NodeAPI AppEnvironmentExtra "NODE_ENV=production" "PORT=3000"
C:\Tools\nssm\nssm.exe set NodeAPI AppStdout "C:\Apps\my-node-api\logs\out.log"
C:\Tools\nssm\nssm.exe set NodeAPI AppStderr "C:\Apps\my-node-api\logs\err.log"
C:\Tools\nssm\nssm.exe set NodeAPI Start SERVICE_AUTO_START
C:\Tools\nssm\nssm.exe start NodeAPI
```

| Phương án chạy Node.js trên Windows | Hỗ trợ Cluster đa nhân | Tự bật sau Reboot | Dễ quản lý log & crash |
| :--- | :--- | :--- | :--- |
| Chạy `node index.js` trong CMD | Không | Không | Kém |
| Chỉ dùng PM2 CLI | Có (`-i max`) | Không (mất khi reboot) | Tốt (nhưng cần giữ session) |
| **NSSM bọc trực tiếp Node.js** | Dùng `cluster` module trong code | **Có (100% ổn định)** | **Rất tốt (xoay vòng log tự động)** |

Nếu bạn cần mở rộng hệ thống trên Windows VPS với các tác vụ mạng hoặc điều phối workflow, hãy tham khảo bài [xử lý lỗi Timeout, OOM cho n8n + PostgreSQL](https://thuecloud.vn/n8n-postgresql-production-huong-dan-xu-ly-loi-timeout-oom-va-su-co-ket-noi-thuc-te), [vận hành Rust Reqwest qua Proxy USA Datacenter sạch](https://thuecloud.vn/van-hanh-rust-reqwest-qua-proxy-usa-datacenter-sach-chong-khoa-phien-va-toi-uu-luong), và [cấu hình CORS Header và bảo mật API trên aaPanel](https://thuecloud.vn/aapanel-tren-vps-cach-cau-hinh-cors-header-va-bao-mat-api).

> **Triển khai Node.js ổn định trên Windows Server tại ThueCloud.Vn:**
> Cụm **KVM Enterprise NVMe** tại `vn-hn` giúp Windows Server 2022 khởi động và phản hồi cực nhanh, cam kết **SLA 99,9%**, **VAT 0%** và **hoàn tiền 07 ngày**. Khởi tạo **VPS VN 2** (`78.000đ`/`90.000đ`) hoặc **VPS VN 3** (`139.000đ`/`170.000đ`) tại [bảng giá ThueCloud](https://thuecloud.vn/pricing).

## Câu hỏi thường gặp (FAQ)

### Tại sao chạy pm2 list trong PowerShell không thấy process đang chạy dưới Windows Service?
Vì Windows Service mặc định chạy dưới tài khoản `LocalSystem` (đọc biến môi trường User của System), còn bạn đang mở PowerShell dưới tài khoản `Administrator`. Nếu không đặt biến `PM2_HOME` ở cấp **System (Machine)**, hai tài khoản sẽ đọc hai thư mục `.pm2` khác nhau.

### Nên chọn NSSM hay pm2-windows-service?
NSSM được cộng đồng Windows Server tin dùng hơn vì nó viết bằng C++ thuần, nhẹ, không phụ thuộc phiên bản Node.js cũ và tích hợp sẵn tính năng xoay vòng file log (`AppRotateFiles`).

### Làm sao cập nhật code mới mà không quên khởi động lại NSSM Service?
Bạn có thể tạo một script `deploy.ps1` thực hiện `git pull`, `npm run build`, và kết thúc bằng lệnh `nssm restart NodeAPI`.

## References / Sources

- https://pm2.keymetrics.io/docs/usage/quick-start/
- https://nssm.cc/usage

---

## Navigation & Machine-Readable Endpoints

- [Site Summary (llms.txt)](https://thuecloud.vn/llms.txt)
- [Full Technical Reference (llms-full.txt)](https://thuecloud.vn/llms-full.txt)
- [Pricing Catalog (Markdown)](https://thuecloud.vn/pricing.md)
- [Blog & Technical Guides (Markdown)](https://thuecloud.vn/blog.md)
- [XML Sitemap](https://thuecloud.vn/sitemap.xml)
