在 Debian 上部署 Node.js 常见有三种方式:
1️⃣ 用 apt 直接安装(最简单,但版本可能偏旧)
2️⃣ 用 NodeSource 安装指定版本(推荐)
3️⃣ 用 nvm 管理多版本(适合开发)
下面按推荐程度给你说明。
sudo apt update
sudo apt install -y curl gnupg
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
可替换版本:
18.x/20.x/22.x
sudo apt install -y nodejs
node -v
npm -v
sudo apt update
sudo apt install -y nodejs npm
node -v
⚠️ Debian 官方源版本通常较旧(如 12.x)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc
nvm install 20
nvm use 20
git clone your-project.git
cd your-project
npm install
npm run build # 如有
node app.js
sudo npm install -g pm2
pm2 start app.js
pm2 save
pm2 startup
.env 管理配置如果你告诉我:
我可以给你一套完整部署方案(含 Nginx + HTTPS)