在 Debian 上部署 Node.js 服务,通常分为 安装 Node.js → 编写/上传项目 → 运行服务 → 设置开机自启/反向代理 几个步骤。下面给你一套常用且稳妥的方案。
sudo apt update
sudo apt upgrade -y
sudo apt install -y curl gnupg2 ca-certificates
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs
node -v
npm -v
cd /var/www
sudo mkdir myapp
sudo chown $USER:$USER myapp
cd myapp
上传项目(git / scp / ftp 均可):
git clone your_repo.git .
npm install
npm run build # 如有构建步骤
// app.js
const http = require('http');
const server = http.createServer((req, res) => {
res.end('Hello Debian Node.js');
});
server.listen(3000, () => console.log('running on 3000'));
运行:
node app.js
sudo npm install -g pm2
pm2 start app.js --name myapp
pm2 list
pm2 logs
pm2 restart myapp
pm2 stop myapp
pm2 startup
pm2 save
sudo apt install -y nginx
sudo nano /etc/nginx/sites-available/myapp
内容示例:
server {
listen 80;
server_name your_domain.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
启用配置:
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
sudo ufw allow 80
sudo ufw allow 443
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d your_domain.com
✅ Node.js
✅ PM2
✅ Nginx
✅ 域名 + HTTPS
如果你告诉我:
我可以直接给你定制部署命令。