Node.js 使用pm2部署项目、搭建node生产环境 提高线上服务稳定性

pm2 是最多人使用的用来管理服务器上 node.js 应用进程的库之一。

pm2 是一个带有负载均衡功能的Node应用的进程管理器。可以把你的独立代码利用全部的服务器上的所有CPU,并保证进程永远都活着,0秒的重载。

进程守护,系统奔溃自动重启服务,增加配置后可以启动多进程充分利用cpu和内存,自带日志记录功能

pm2的安装

npm install -g pm2
pm2 -v  查看版本号

pm2的使用

pm2 start app.js   // 后台运行pm2
pm2 list               // 显示所有进程状态
pm2 monit AppName/ID             // 监视所有进程 内存信息 全局日志
pm2 logs  AppName/ID             //  显示所有进程日志
pm2 stop all           // 停止所有进程
pm2 restart all        // 重启所有进程
pm2 restart AppName/ID       // 重启某个进程
pm2 stop AppName/ID             // 停止指定的进程
pm2 restart AppName/ID        // 重启指定的进程
pm2 delete AppName/ID         // 杀死指定的进程
pm2 delete all         // 杀死全部进程
pm2 info AppName/ID         // 当前服务的信息

demo案例

pm2-test/package.json

{
  "name": "pm2-test",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "cross-env NODE_ENV=dev nodemon app.js",
    "prd": "cross-env NODE_ENV=production pm2 start pm2.conf.json"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "cross-env": "^5.2.0",
    "nodemon": "^1.18.9"
  }
}

pm2-test/pm2.conf.json

{
  "apps": {
      "name": "pm2-server",
      "script": "app.js",
      "watch": true,
      "ignore_watch": [    // 不需要监听
          "node_modules",
          "logs"
      ],
      "instances": 4,  // 4核cpu  4个进程
      "error_file": "logs/err.log", //  错误日志
      "out_file": "logs/out.log", // 自定义日志
      "log_date_format": "YYYY-MM-DD HH:mm:ss" // 日志时间戳
  }
}
> cross-env NODE_ENV=production pm2 start pm2.conf.json


                        -------------

__/\\\\\\\\\\\\\____/\\\\____________/\\\\____/\\\\\\\\\_____
 _\/\\\/////////\\\_\/\\\\\\________/\\\\\\__/\\\///////\\\___
  _\/\\\_______\/\\\_\/\\\//\\\____/\\\//\\\_\///______\//\\\__
   _\/\\\\\\\\\\\\\/__\/\\\\///\\\/\\\/_\/\\\___________/\\\/___
    _\/\\\/////////____\/\\\__\///\\\/___\/\\\________/\\\//_____
     _\/\\\_____________\/\\\____\///_____\/\\\_____/\\\//________
      _\/\\\_____________\/\\\_____________\/\\\___/\\\/___________
       _\/\\\_____________\/\\\_____________\/\\\__/\\\\\\\\\\\\\\\_
        _\///______________\///______________\///__\///////////////__


                          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=/Users/niko4/.pm2
[PM2] PM2 Successfully daemonized
[PM2][WARN] Applications pm2-server not running, starting...
[PM2] App [pm2-server] launched (4 instances)
┌─────┬───────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id  │ name          │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
├─────┼───────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0   │ pm2-server    │ default     │ 1.0.0   │ cluster │ 1108     │ 0s     │ 0    │ online    │ 0%       │ 34.9mb   │ niko4    │ enabled  │
│ 1   │ pm2-server    │ default     │ 1.0.0   │ cluster │ 1109     │ 0s     │ 0    │ online    │ 0%       │ 34.5mb   │ niko4    │ enabled  │
│ 2   │ pm2-server    │ default     │ 1.0.0   │ cluster │ 1110     │ 0s     │ 0    │ online    │ 0%       │ 32.0mb   │ niko4    │ enabled  │
│ 3   │ pm2-server    │ default     │ 1.0.0   │ cluster │ 1111     │ 0s     │ 0    │ online    │ 0%       │ 21.0mb   │ niko4    │ enabled  │
└─────┴───────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘

实际应用

{
  "name": "blog-koa2",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "start": "node bin/www",
    "dev": "cross-env NODE_ENV=dev ./node_modules/.bin/nodemon bin/www",
    "prd": "cross-env NODE_ENV=production pm2 start bin/www",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "dependencies": {
    "debug": "^2.6.3",
    "koa": "^2.2.0",
    "koa-bodyparser": "^3.2.0",
    "koa-convert": "^1.2.0",
    "koa-generic-session": "^2.0.1",
    "koa-json": "^2.0.2",
    "koa-logger": "^2.0.1",
    "koa-morgan": "^1.0.1",
    "koa-onerror": "^1.2.1",
    "koa-redis": "^3.1.3",
    "koa-router": "^7.1.1",
    "koa-static": "^3.0.0",
    "koa-views": "^5.2.1",
    "mysql": "^2.16.0",
    "pug": "^2.0.0-rc.1",
    "redis": "^2.8.0",
    "xss": "^1.0.3"
  },
  "devDependencies": {
    "cross-env": "^5.2.0",
    "nodemon": "^1.8.1"
  }
}

你可能感兴趣的:(nodejs)