温馨提示×

Debian下Nodejs如何重启

小樊
38
2026-07-29 13:09:36
栏目: 编程语言

在 Debian 下重启 Node.js 的方式取决于你是如何运行它的。下面按常见场景给你说明。


一、直接前台运行的 Node.js(不推荐生产环境)

如果你是直接在终端运行:

node app.js

那么:

  • 停止Ctrl + C
  • 重启:重新执行
node app.js

⚠️ 这种方式一旦关闭终端,进程就会结束。


二、使用 PM2(✅ 推荐)

1️⃣ 安装 PM2

npm install -g pm2

2️⃣ 启动 Node.js 应用

pm2 start app.js

3️⃣ 重启 Node.js

pm2 restart app

或指定 id / name:

pm2 restart 0

4️⃣ 查看状态

pm2 list

5️⃣ 设置开机自启(Debian)

pm2 startup
pm2 save

生产环境首选方案


三、使用 systemd(Debian 官方推荐)

1️⃣ 创建服务文件

sudo nano /etc/systemd/system/nodeapp.service

示例内容:

[Unit]
Description=Node.js App
After=network.target

[Service]
Type=simple
User=youruser
WorkingDirectory=/var/www/nodeapp
ExecStart=/usr/bin/node /var/www/nodeapp/app.js
Restart=always

[Install]
WantedBy=multi-user.target

2️⃣ 重载 systemd 并启动

sudo systemctl daemon-reload
sudo systemctl start nodeapp

3️⃣ 重启 Node.js

sudo systemctl restart nodeapp

4️⃣ 查看状态

sudo systemctl status nodeapp

5️⃣ 开机自启

sudo systemctl enable nodeapp

四、Docker 方式运行

docker restart 容器名或ID

五、如何确认 Node.js 是否真的重启了?

ps aux | grep node

或:

pgrep -a node

✅ 推荐总结

场景 推荐方式
本地开发 node app.js
生产环境 PM2
系统级服务 systemd
容器化 Docker

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

  • 你是用 PM2 / systemd / 直接运行
  • Debian 版本(如 11 / 12)?
  • Node.js 启动命令或路径?

我可以给你更精确的配置示例。

0