在RHEL 8/CentOS 8上安装PM2 Node.js进程管理器

时间:2020-02-23 14:44:52  来源:igfitidea点击:

如何在RHEL 8/CentOS 8上安装PM2?
PM2是具有内置负载均衡器的Node.js应用程序的开源生产过程管理器。
它旨在高效,并使我们可以在群集模式下运行应用程序,以便于HA。
使用PM2,我们可以永远保持应用程序,重新加载它们而不会导致停机,并轻松执行其他Node.js常用系统管理任务。

PM2从node.js 4.x开始支持从node.js 4.x开始的所有主要版本。
此外,它还适用于Linux,MacOS和Windows操作系统。
本文将解释如何安装和使用PM2以在RHEL/CentOS生产服务器中运行应用程序。

第1步:安装nodejs和npm

在我们安装PM2之前,我们需要节点.js和npm。
Rhel/CentOS与Node.js 10和8.
选择要使用yum或者dnf包管理器安装的版本。

$sudo dnf module list | grep nodejs
nodejs 10 [d] development, minimal, s2i, default [d] Javascript runtime
nodejs 8 development, minimal, s2i, default [d] Javascript runtime

安装的默认版本是nodejs 10,但我们可以显式指定要安装的节点。

sudo dnf module install nodejs:10

对于node.js 8,它将是

sudo dnf module install nodejs:8

等待安装完成,然后查看版本:

$node --version
v10.14.1

我们可能还需要安装用于构建节点软件的开发工具。

sudo dnf -y install -y gcc-c++ make

第2步:在CentOS 8/Rhel 8上安装PM2

安装了Node.js后,继续安装PM2进程管理器。
选项 -g用于全局安装。

sudo npm i -g pm2

示例安装输出。

....................
/usr/bin/pm2 -> /usr/lib/node_modules/pm2/bin/pm2
/usr/bin/pm2-dev -> /usr/lib/node_modules/pm2/bin/pm2-dev
/usr/bin/pm2-docker -> /usr/lib/node_modules/pm2/bin/pm2-docker
/usr/bin/pm2-runtime -> /usr/lib/node_modules/pm2/bin/pm2-runtime
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: Hyman@theitroad (node_modules/pm2/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for Hyman@theitroad: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
Hyman@theitroad
updated 1 package in 16.992s

PM2应该安装到 /usr/bin小路。

$which pm2
/usr/bin/pm2

第3步:创建测试节点.js应用程序

创建项目文件夹。

mkdir hello-world-nodejs

更改为项目文件夹并创建 app.js文件下面的文件。

cd hello-world-nodejs
vi app.js

添加

const http = require('http');
http.createServer(function(request, response) {
  response.writeHead(200, {'Content-Type': 'text/plain'});
  response.end("Hello, World!\n");
}).listen(process.env.PORT);
console.log('App is running...');

第4步:如何在RHEL 8/CentOS 8上使用PM2

使用PM2启动应用程序。

$pm2 start app.js 
                        ------------
__/\\\\\\____/\\____________/\\____/\\\\_____
 _\/\\/////////\_\/\\\________/\\\__/\\///////\___
  _\/\_______\/\_\/\\//\____/\\//\_\///______\//\__
   _\/\\\\\\\/__\/\\///\\/\\/_\/\___________/\\/___
    _\/\\/////////____\/\__\///\\/___\/\________/\\//_____
     _\/\_____________\/\____\///_____\/\_____/\\//________
      _\/\_____________\/\_____________\/\___/\\/___________
       _\/\_____________\/\_____________\/\__/\\\\\\\_
        _\///______________\///______________\///__\///////////////__

                          Runtime Edition
        PM2 is a Production Process Manager for Node.js applications
                     with a built-in Load Balancer.
                Start and Daemonize any application:
                $pm2 start app.js
                Load Balance 4 instances of api.js:
                $pm2 start api.js -i 4
                Monitor in production:
                $pm2 monitor
                Make pm2 auto-boot at server restart:
                $pm2 startup
                To go further checkout:
                http://pm2.io/

                        ------------
[PM2] Spawning PM2 daemon with pm2_home=/root/.pm2
[PM2] PM2 Successfully daemonized
[PM2] Starting /root/node-hello-world/app.js in fork_mode (1 instance)
[PM2] Done.
┌──────────┬────┬─────────┬──────┬──────┬────────┬─────────┬────────┬─────┬───────────┬──────┬──────────┐
│ App name │ id │ version │ mode │ pid  │ status │ restart │ uptime │ cpu │ mem       │ user │ watching │
├──────────┼────┼─────────┼──────┼──────┼────────┼─────────┼────────┼─────┼───────────┼──────┼──────────┤
│ app      │ 0  │ N/A     │ fork │ 4618 │ online │ 0       │ 0s     │ 0%  │ 31.2 MB   │ root │ disabled │
└──────────┴────┴─────────┴──────┴──────┴────────┴─────────┴────────┴─────┴───────────┴──────┴──────────┘

列出运行的应用程序

$pm2 list
┌──────────┬────┬─────────┬──────┬──────┬────────┬─────────┬────────┬──────┬───────────┬──────┬──────────┐
│ App name │ id │ version │ mode │ pid  │ status │ restart │ uptime │ cpu  │ mem       │ user │ watching │
├──────────┼────┼─────────┼──────┼──────┼────────┼─────────┼────────┼──────┼───────────┼──────┼──────────┤
│ app      │ 0  │ N/A     │ fork │ 4618 │ online │ 0       │ 17m    │ 0.4% │ 47.5 MB   │ root │ disabled │
└──────────┴────┴─────────┴──────┴──────┴────────┴─────────┴────────┴──────┴───────────┴──────┴──────────┘

监控目标过程,使用

$sudo pm2 monitor

查看node.js流程信息。

首先获取进程ID

$sudo pm2 list

然后使用进程ID拉更多的信息。

$sudo pm2 show <ProcessID>

请参见下面的示例。

设置应用程序以启动启动。

使用 pm2 startup命令。

$sudo pm2 startup
 [PM2] Init System found: systemd
 Platform systemd
 Template
 [Unit]
 Description=PM2 process manager
 Documentation=https://pm2.keymetrics.io/
 After=network.target
 [Service]
 Type=forking
 User=root
 LimitNOFILE=infinity
 LimitNPROC=infinity
 LimitCORE=infinity
 Environment=PATH=/sbin:/bin:/usr/sbin:/usr/bin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
 Environment=PM2_HOME=/root/.pm2
 PIDFile=/root/.pm2/pm2.pid
 ExecStart=/usr/lib/node_modules/pm2/bin/pm2 resurrect
 ExecReload=/usr/lib/node_modules/pm2/bin/pm2 reload all
 ExecStop=/usr/lib/node_modules/pm2/bin/pm2 kill
 [Install]
 WantedBy=multi-user.target
 Target path
 /etc/systemd/system/pm2-root.service
 Command list
 [ 'systemctl enable pm2-root' ]
 [PM2] Writing init configuration in /etc/systemd/system/pm2-root.service
 [PM2] Making script booting at startup…
 [PM2] [-] Executing: systemctl enable pm2-root…
 Created symlink /etc/systemd/system/multi-user.target.wants/pm2-root.service → /etc/systemd/system/pm2-root.service.
 [PM2] [v] Command successfully executed.
 +---------------------------------------+
 [PM2] Freeze a process list on reboot via:
 $pm2 save
 [PM2] Remove init script via:
 $pm2 unstartup systemd

要禁用运行:

sudo pm2 unstartup

查看应用程序日志。

$sudo pm2 logs
 [TAILING] Tailing last 15 lines for [all] processes (change the value with --lines option)
 /root/.pm2/pm2.log last 15 lines:
 PM2        | 2019-03-20T16:28:15: PM2 log: Time                 : Wed Mar 20 2019 16:28:15 GMT+0300 (East Africa Time)
 PM2        | 2019-03-20T16:28:15: PM2 log: PM2 version          : 3.4.0
 PM2        | 2019-03-20T16:28:15: PM2 log: Node.js version      : 10.11.0
 PM2        | 2019-03-20T16:28:15: PM2 log: Current arch         : x64
 PM2        | 2019-03-20T16:28:15: PM2 log: PM2 home             : /root/.pm2
 PM2        | 2019-03-20T16:28:15: PM2 log: PM2 PID file         : /root/.pm2/pm2.pid
 PM2        | 2019-03-20T16:28:15: PM2 log: RPC socket file      : /root/.pm2/rpc.sock
 PM2        | 2019-03-20T16:28:15: PM2 log: BUS socket file      : /root/.pm2/pub.sock
 PM2        | 2019-03-20T16:28:15: PM2 log: Application log path : /root/.pm2/logs
 PM2        | 2019-03-20T16:28:15: PM2 log: Process dump file    : /root/.pm2/dump.pm2
 PM2        | 2019-03-20T16:28:15: PM2 log: Concurrent actions   : 2
 PM2        | 2019-03-20T16:28:15: PM2 log: SIGTERM timeout      : 1600
 PM2        | 2019-03-20T16:28:15: PM2 log: ===============================================================================
 PM2        | 2019-03-20T16:28:15: PM2 log: App [app:0] starting in -fork mode
 PM2        | 2019-03-20T16:28:15: PM2 log: App [app:0] online
 /root/.pm2/logs/app-error.log last 15 lines:
 /root/.pm2/logs/app-out.log last 15 lines:
 0|app      | App is running…
 0|app      | App is running…
 0|app      | App is running…

更新PM2.

使用PM2 UPDATE命令拉动PM2更新。

sudo pm2 update