pm2
如果要想在服务端部署node.js 程序,让其持久化运行,就不能单单使用 npm start 命令运行,当然了,这样运行是毫无问题的,但是当关闭 xshell 窗口或者是关闭进程的时候(其实关闭 xshell 窗口相当于默认关闭进程),就无法访问对应的 node.js 服务端程序了。
服务器上的 node 应用需要用 pm2 的 **日志管理、进程管理、负载均衡、性能监控 **
等功能。不管是出于稳定性、性能还是可观测性等目的。但是有 docker 的场景下,pm2 就真的不需要的了。可有可无捏。
安装
1 |
|
启动!
1 |
|
日志管理
1 |
|
进程自动重启
1 |
|
负载均衡
用 -i
指定该进程用多几个 cpu 核心运行
1 |
|
还可以动态调整进程数,通过 <font style="color:rgb(37, 41, 51);">pm2 scale</font>
1 |
|
ecosystem.config.js
执行 <font style="color:rgb(37, 41, 51);">pm2 ecosystem</font>
会创建一个配置文件, 然后 <font style="color:rgb(37, 41, 51);">pm2 start ecosystem.config.js</font>
就可以批量跑一批应用。
1 |
|
forever (不推荐了,已经有更好的 pm2 了)
- 第一步安装 forever
1 |
|
- 第二步运行对应的 js
1 |
|
注意 (你可能会遇到如下错误):
错误信息:
<font style="color:rgb(77, 77, 77);">forever: command not found</font>
原因: 以 Windows 来说,通常这种错误是因为没有配置好环境变量,解决方案也很简单就是配置好环境变量或者是使用绝对路径 **<font style="color:rgb(77, 77, 77);">(全局安装就没有这个问题了)</font>**
解决方式 (Linux 演示,这里我使用绝对路径):
如何找到绝对路径呢?
通过该命令可以获取 node.js 的安装模块, **<font style="color:rgb(77, 77, 77);">npm list -g –depth 0</font>**
├── ali-oss@6.1.0
├── forever@0.15.3
└── npm@6.4.1
再通过关键字搜索 **<font style="color:rgb(77, 77, 77);">find / -name forever</font>**
1 |
|
最后通过 **<font style="color:rgb(77, 77, 77);">/home/youcong/mock-github-api/node_modules/forever/bin/forever start index.js</font>**
即可实现 node.js 服务端程序在 Linux 上持久运行。
forever 常用命令
1 |
|
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!