在 Debian 上部署 Node.js 有多种方式,下面按推荐程度从简单到灵活给你介绍几种常见做法。
适合:学习、测试、不追求最新版本
sudo apt update
sudo apt install -y nodejs npm
查看版本:
node -v
npm -v
⚠️ Debian 仓库中的 Node.js 通常较旧(如 18.x 甚至更低)
适合:生产环境、需要指定版本(如 18 / 20 / 22)
sudo apt update
sudo apt install -y curl
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs
验证:
node -v
npm -v
✅ 优点:
适合:开发环境、需要多个 Node 版本
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc
nvm install --lts
node -v
⚠️ NVM 安装的 Node 只对当前用户生效,不太适合系统级服务
cd /var/www
git clone your-project.git
cd your-project
npm install
sudo npm install -g pm2
pm2 start app.js
pm2 save
pm2 startup
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
}
}
如果你能说明:
我可以给你更精确的部署方案。