pm2是一个前端服务器工具,是一套基于jQuery的框架,在node平台上早期比较常用的前端服务器平台。
可以通过他们的官网: PM2 - Home 了解更多信息。
如果你没有接触过pm2,那么你是幸运的,说明你足够年轻!
本文仅讲讲项目中用到的一些操作,希望对你有所帮助!
1, 安装:
先安装node:
# wget
# tar xf node-v10.9.0-linux-x64.tar.xz // 解压
# cd node-v10.9.0-linux-x64/ // 进入解压目录
# ./bin/node -v // 执行node命令 查看版本
建立软链接:
ln -s /usr/software/nodejs/bin/npm /usr/local/bin/
ln -s /usr/software/nodejs/bin/node /usr/local/bin/
安装pm2
#npm install pm2@14.0.9 //安装v14.0.9版本的pm2
2,项目配置
package.json
{
"name": "web-manager",
"version": "1.0.0",
"description": "web manager",
"main": "run.js",
"scripts": {
"dev": "cross-env NODE_ENV_CONFIG=development NODE_ENV=development LANG=en-us DEBUG=koa-views nodemon --inspect run.js",
"test": "gulp build && cross-env NODE_ENV_CONFIG=test-20 NODE_ENV=test LANG=en-us nodemon --inspect run.js",
"test-india": "gulp build && cross-env NODE_ENV_CONFIG=test-india-98 NODE_ENV=test LANG=en-us nodemon --inspect run.js",
"build": "gulp build",
"concat": "gulp concat",
"js-concat": "gulp js-concat",
"css-concat": "gulp css-concat",
"pm2-dev": "pm2 start pm2.yml --env development",
"pm2": "gulp build && pm2 start pm2.yml",
"pm2-test": "gulp build && pm2 start pm2.yml --env test",
"pm2-pakistan-dev": "gulp build && pm2 start pm2.yml --env pakistan-dev",
"pm2-pakistan-test": "gulp build && pm2 start pm2.yml --env pakistan-test",
"pm2-pakistan-uat": "gulp build && pm2 start pm2.yml --env pakistan-uat",
"pm2-pakistan-prod": "gulp build && pm2 start pm2.yml --env pakistan-product",
"pm2-pakistan-prod-186": "gulp build && pm2 start pm2.yml --env pakistan-product-186",
"pm2-pakistan-prod-187": "gulp build && pm2 start pm2.yml --env pakistan-product-187",
"pm2-india-dev": "gulp build && pm2 start pm2.yml --env india-dev",
"pm2-india-test": "gulp build && pm2 start pm2.yml --env india-test",
"pm2-india-uat": "gulp build && pm2 start pm2.yml --env india-uat",
"pm2-india-prod": "gulp build && pm2 start pm2.yml --env india-product",
"pm2-pakistan-fra": "gulp build && pm2 start pm2.yml --env pakistan-fra",
"pakistan-dev": "gulp build && cross-env NODE_ENV_CONFIG=pakistan-dev NODE_ENV=test LANG=en-us nodemon --inspect run.js",
"pakistan-test": "gulp build && cross-env NODE_ENV_CONFIG=pakistan-test NODE_ENV=test LANG=en-us nodemon --inspect run.js",
"pakistan-uat": "gulp build && cross-env NODE_ENV_CONFIG=pakistan-uat NODE_ENV=test LANG=en-us nodemon --inspect run.js",
"pakistan-product": "gulp build && cross-env NODE_ENV_CONFIG=pakistan-product NODE_ENV=production LANG=en-us nodemon --inspect run.js",
"india-dev": "gulp build && cross-env NODE_ENV_CONFIG=india-dev NODE_ENV=dev LANG=en-us nodemon --inspect run.js",
"india-test": "gulp build && cross-env NODE_ENV_CONFIG=india-test NODE_ENV=test LANG=en-us nodemon --inspect run.js",
"india-uat": "gulp build && cross-env NODE_ENV_CONFIG=india-uat NODE_ENV=uat LANG=en-us nodemon --inspect run.js",
"india-product": "gulp build && cross-env NODE_ENV_CONFIG=india-product NODE_ENV=production LANG=en-us nodemon --inspect run.js",
"pakistan-fra": "gulp build && cross-env NODE_ENV_CONFIG=pakistan-fra NODE_ENV=production LANG=en-us nodemon --inspect run.js"
},
"author": "Liuzh",
"license": "ISC",
"dependencies": {
"babel-plugin-transform-runtime": "^6.9.0",
"babel-preset-env": "^1.7.0",
"babel-preset-stage-0": "^6.5.0",
"babel-register": "^6.8.0",
"babel-runtime": "^6.9.2",
"bluebird": "^3.4.7",
"crypto": "^0.0.3",
"fs": "0.0.1-security",
"fs-extra": "^0.30.0",
"fs-promise": "^0.5.0",
"http": "^0.0.0",
"http-proxy": "^1.17.0",
"https": "^1.0.0",
"is-descriptor": "^3.0.0",
"jaeger-client": "^3.14.4",
"koa": "^2.0.0",
"koa-bodyparser": "^3.1.0",
"koa-compress": "^2.0.0",
"koa-conditional-get": "^1.0.3",
"koa-convert": "^1.2.0",
"koa-cors": "^0.0.16",
"koa-etag": "^3.0.0",
"koa-i18n-2": "^1.3.0",
"koa-json": "^1.1.1",
"koa-locale": "^1.2.0",
"koa-logger": "^1.3.0",
"koa-multer": "^1.0.0",
"koa-proxy": "^0.9.0",
"koa-router": "^7.0.1",
"koa-send": "^3.2.0",
"koa-server-http-proxy": "^0.1.0",
"koa-sslify": "^1.1.0",
"koa-static": "^2.0.0",
"koa-static-cache": "^4.1.0",
"koa-views": "^5.0.1",
"log4js": "^0.6.38",
"minimatch": "^3.0.4",
"mkdirp-then": "^1.2.0",
"node-fetch": "^1.6.0",
"nunjucks": "^2.5.2",
"opentracing": "^0.14.3",
"path": "^0.12.7",
"querystring": "^0.2.0",
"request": "^2.88.2",
"thrift": "^0.12.0",
"urlencode": "^1.1.0"
},
"devDependencies": {
"babel-eslint": "^6.0.4",
"cross-env": "^4.0.0",
"del": "^2.2.2",
"eslint": "^3.0.1",
"graceful-fs": "^4.2.2",
"gulp": "^3.9.1",
"gulp-all": "^1.1.0",
"gulp-cache": "^0.4.6",
"gulp-clean": "^0.3.2",
"gulp-concat": "^2.6.1",
"gulp-csso": "^3.0.0",
"gulp-htmlmin": "^3.0.0",
"gulp-imagemin": "^3.4.0",
"gulp-rename": "^1.4.0",
"gulp-rev": "^7.1.2",
"gulp-rev-collector": "^1.1.1",
"gulp-rev-replace": "^0.4.4",
"gulp-uglify": "^2.1.2",
"gulp-util": "^3.0.8",
"koa-onerror": "^2.0.0",
"mongoose": "^4.6.0",
"nodemon": "^1.18.10"
}
}
pm2.yml
apps:
name : itel-web
script : ./run.js
watch : true
instances: 4
ignore_watch : [.idea, node_modules, logs, .vscode, static]
exec_mode: cluster
env_development :
NODE_ENV: development
NODE_ENV_CONFIG: development
LANG: en-us
env_test :
NODE_ENV: test
NODE_ENV_CONFIG: test
LANG: en-us
env:
NODE_ENV: production
NODE_ENV_CONFIG: production
LANG: en-us
env_test-pak-115 :
NODE_ENV: test
NODE_ENV_CONFIG: test-pak-115
env_test-154:
NODE_ENV: test
NODE_ENV_CONFIG: test-154
LANG: en-us
env_pakistan-dev :
NODE_ENV: test
NODE_ENV_CONFIG: pakistan-dev
LANG: en-us
env_pakistan-test :
NODE_ENV: test
NODE_ENV_CONFIG: pakistan-test
LANG: en-us
env_pakistan-uat :
NODE_ENV: test
NODE_ENV_CONFIG: pakistan-uat
LANG: en-us
env_pakistan-product :
NODE_ENV: production
NODE_ENV_CONFIG: pakistan-product
LANG: en-us
env_pakistan-product-186:
NODE_ENV: test
NODE_ENV_CONFIG: pakistan-product-186
LANG: en-us
PORT: 6800
env_pakistan-product-187:
NODE_ENV: test
NODE_ENV_CONFIG: pakistan-product-187
LANG: en-us
PORT: 6800
env_india-dev :
NODE_ENV: dev
NODE_ENV_CONFIG: india-dev
LANG: en-us
env_india-test :
NODE_ENV: test
NODE_ENV_CONFIG: india-test
LANG: en-us
env_india-uat :
NODE_ENV: uat
NODE_ENV_CONFIG: india-uat
LANG: en-us
env_india-product :
NODE_ENV: production
NODE_ENV_CONFIG: india-product
LANG: en-us
env_pakistan-fra :
NODE_ENV: production
NODE_ENV_CONFIG: pakistan-fra
LANG: en-us
error_file: logs/err.log
out_file: logs/out.log
log_date_format: YYYY-MM-DD HH:mm Z
3,启动和停止项目
#pm2 run pm2-uat //启动uat环境的配置
#pm2 kill //停止所有的pm2服务
#pm2 log //查看pm2 实时日志
注意:
1,pm2会自动启动做一个前端服务器,会有自己的端口号,并且在前端服务上做了负载均衡,只有当几个负载节点都成功启动,整个服务才算启动。
2,如果项目用了Nginx反向代理时,在启动pm2前端项目时,需要关闭该台服务器上的反向代理,不然就会出现404的错误。等到pm2启动日志中,出现Server On的时候,再打开反向代理,让流量进入就可以正常访问了。