温馨提示×

如何在debian部署nodejs

小樊
48
2026-09-01 16:50:33
栏目: 编程语言

在 Debian 上部署 Node.js 有多种方式,下面给你最常用、最稳妥的几种方案,从“新手友好”到“生产推荐”逐步说明。


一、使用 Debian 官方仓库(最简单,但版本较旧)

适合:测试、学习、不追求最新版本

sudo apt update
sudo apt install -y nodejs npm

查看版本:

node -v
npm -v

⚠️ Debian 仓库中的 Node.js 通常较旧(如 12 / 14),不推荐用于生产。


二、使用 NodeSource 仓库(推荐 ✅)

适合:开发 & 生产环境,版本可控

1. 选择 Node.js 版本(示例:20.x)

curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -

2. 安装

sudo apt install -y nodejs

3. 验证

node -v
npm -v

✅ 优点:

  • 版本新
  • 长期支持(LTS)
  • 官方推荐方式

三、使用 NVM(多版本管理,开发推荐)

适合:前端 / 后端开发,需要切换版本

1. 安装 NVM

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

2. 重新登录或加载环境

source ~/.bashrc

3. 安装 Node.js

nvm install 20
nvm use 20

四、部署 Node.js 项目(生产基础)

1. 上传项目

git clone your_repo.git
cd your_project
npm install

2. 启动服务

node app.js
# 或
npm start

五、生产环境建议(很重要)

✅ 使用 PM2 管理进程

sudo npm install -g pm2
pm2 start app.js
pm2 save
pm2 startup

✅ 使用 Nginx 反向代理

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://localhost:3000;
    }
}

✅ 开启防火墙

sudo ufw allow 80
sudo ufw allow 443

六、总结推荐方案

场景 推荐方式
学习 / 测试 apt 安装
开发 NVM
服务器部署 NodeSource + PM2 + Nginx

如果你愿意,可以告诉我:

  • Debian 版本(11 / 12?)
  • 用途(Web 服务 / 脚本 / API?)
  • 是否需要 HTTPS / Docker

我可以直接给你一套完整部署命令

0