Nodejs 应用程序如何始终作为 Windows 服务运行?

问题描述 投票:0回答:3

我有 Windows Server 2016 云托管。我在 IIS 上广播我的网站。

我有 2 个应用程序在 Nodejs 上运行。配置应用程序后我退出 Windows。

我的 IIS 站点继续运行,但当我注销或重新启动 Windows 时,我的 Nodejs 应用程序退出。

我已按照以下步骤解决此问题。

  1. 我安装了 pm2 库。全球范围内。
npm install --global pm2@latest 
  1. 我安装了 pm2-windows-service 库。全球范围内。
npm i pm2-windows-service -g
  1. 我安装了 pm2-windows-startup 库。全球范围内。
npm install pm2-windows-startup -g
  1. 我卸载正在运行的服务以避免重复运行应用程序。
pm2 kill
  1. 如果有任何正在运行的服务,我会先将其卸载。
pm2-service-uninstall
  1. 我安装了 pm2-service-install 库。我将其命名为“pm2Service1”。当我输入 services.msc 时,我看到它开始了。
pm2-service-install -n pm2Service1
  1. 我启动启动服务,以便应用程序在 Windows 打开时启动。
pm2-startup install
  1. 我进入应用程序的安装目录并使用 pm2 运行该应用程序。 (如果我不进入目录运行就会报错,无法读取sql/file.sql文件。)
c:
cd C:\webSites\myService1
pm2 start app.js
  1. 我用列表和显示检查应用程序,看看它是否有效。
pm2 list
pm2 show 0

确保其有效后。

  1. 我正在记录正在运行的应用程序。
pm2 save --force

当我执行这些过程并注销窗口时,我可以从外部访问该应用程序。

但是当我重新启动 Windows 时,应用程序无法运行。每次启动时我都必须重复相同的步骤。

当 Windows 重新启动时,“pm2Service1”似乎正在工作。但 pm2 将列表列为空。

node.js windows-services pm2
3个回答
1
投票

我通过使用 NSSM 服务在我的服务器上完成了此操作。安装后,转到 NSSM 文件夹并输入

nssm install PM2

您可以在这里找到完整的教程:

https://lakin-mohapatra.medium.com/register-pm2-as-a-service-in-the-windows-server-747f19e2ff2a


0
投票

最佳实践 AFAIK 是使用 nssm +“windows 批处理文件”来启动你的 app.js,有关更多详细信息,你可以查看 this 链接。


0
投票

pm2-windows-service
已弃用。
我没有使用
pm2-windows-startup
的经验,但我认为它在注册表中添加了一个条目以在用户登录后启动 pm2,这已经太晚了。

这是我的工作示例

© www.soinside.com 2019 - 2024. All rights reserved.