Hướng dẫn cài Python và Node.js trên VPS Windows để chạy bot, web app, script tự động hóa.
==== CÀI PYTHON ====
Bước 1: Tải và cài Python
1. Truy cập https://www.python.org/downloads/windows/
2. Tải bản mới nhất (Python 3.12+) → chọn Windows installer (64-bit)
3. Chạy installer:
- Tích vào Add Python to PATH (quan trọng!)
- Tích vào Install for all users
- Nhấn Install Now
Hoặc cài qua PowerShell (winget):
winget install Python.Python.3.12
Bước 2: Kiểm tra cài thành công
python --version
pip --version
Bước 3: Cài package và chạy script
# Cài package
pip install requests flask django
# Chạy script
python myScript.py
# Tạo virtual environment (khuyến nghị)
python -m venv venv
venv\Scripts\activate
pip install -r requirements.txt
Bước 4: Chạy Python app như service (tự khởi động)
Dùng NSSM (Non-Sucking Service Manager):
# Tải NSSM: https://nssm.cc/download
# Giải nén vào C:\nssm
# Cài service
C:\nssm\win64\nssm.exe install MyPythonApp
# Điền Path: C:\Python312\python.exe
# Arguments: C:\myapp\app.py
# Start → OK
# Hoặc command line:
nssm install MyPythonApp "C:\Python312\python.exe" "C:\myapp\app.py"
nssm start MyPythonApp
Chạy Flask/Django trên VPS Windows:
# Flask
pip install flask
python app.py # chạy trên port 5000
# Django
pip install django
django-admin startproject myproject
cd myproject
python manage.py runserver 0.0.0.0:8000
==== CÀI NODE.JS ====
Bước 1: Tải và cài Node.js
1. Truy cập https://nodejs.org/en/download/
2. Tải bản LTS (Long Term Support) → Windows Installer (.msi) 64-bit
3. Chạy installer → Next → Accept → Next → Next → Install
4. Tích vào Automatically install necessary tools nếu cần build native modules
Hoặc dùng winget:
winget install OpenJS.NodeJS.LTS
Bước 2: Kiểm tra
node --version
npm --version
Bước 3: Chạy app Node.js cơ bản
# Tạo project
mkdir myapp && cd myapp
npm init -y
# Cài Express
npm install express
# Tạo index.js
# const express = require("express")
# const app = express()
# app.get("/", (req, res) => res.send("Hello World"))
# app.listen(3000)
node index.js
Bước 4: Dùng PM2 để quản lý process Node.js
PM2 giúp Node.js tự khởi động lại khi crash và khi reboot Windows:
# Cài PM2
npm install -g pm2
npm install -g pm2-windows-startup
# Khởi động app
pm2 start index.js --name "myapp"
# Tự khởi động khi reboot
pm2-startup install
pm2 save
# Các lệnh PM2 hay dùng
pm2 list # xem tất cả app
pm2 logs myapp # xem log
pm2 restart myapp # restart
pm2 stop myapp # dừng
pm2 delete myapp # xóa
Bước 5: Chạy Bot Telegram trên Windows VPS
# Node.js bot
npm install telegraf dotenv
# Python bot
pip install python-telegram-bot
# Chạy liên tục bằng PM2 (Node.js)
pm2 start bot.js --name "telegram-bot"
pm2 save
Mở firewall nếu app cần truy cập từ ngoài:
# Mở port 3000 (Node.js) hoặc 5000 (Python Flask)
New-NetFirewallRule -DisplayName "Node App" -Direction Inbound -Protocol TCP -LocalPort 3000 -Action Allow
New-NetFirewallRule -DisplayName "Python Flask" -Direction Inbound -Protocol TCP -LocalPort 5000 -Action Allow
Lỗi thường gặp:
- python/node không nhận ra lệnh: Chưa add vào PATH → cài lại, tích vào "Add to PATH"
- npm ERR! EACCES: Chạy PowerShell với quyền Administrator
- Port đã bị dùng:
netstat -ano | findstr :3000→ tìm PID →taskkill /PID xxx /F - Execution Policy error (PowerShell):
Set-ExecutionPolicy RemoteSigned