精华 node deploy practice
发布于 10 年前 作者 i5ting 4949 次浏览 最后一次编辑是 8 年前 来自 分享

node-deploy-practice

启动服务器

expressjs里默认给出的是node bin/www

它有2个问题

  1. 没有cluster,不适合产品模式
  2. 代码变动不能自动reload,不适合开发模式

启动产品上线模式

产品模式我们推荐使用pm2

安装pm2

sudo npm install -g pm2

启动服务器

npm start product

启动开发模式

开发模式我们推荐supervisor

npm start dev

package.json示例

{
  "name": "node-deploy-practice",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "npm run product",
    "product": "sudo NODE_ENV=production pm2 start bin/www -f -x -i max --name push",
    "dev": "./node_modules/.bin/supervisor ./bin/www",
		"test": "./node_modules/.bin/mocha -u tdd",
		"status": "sudo pm2 status",
		"stop": "sudo pm2 kill",
		"web": "sudo pm2 web",
		"monit": "sudo pm2 monit",
		"log": "sudo pm2 logs",
		"startup": "sudo pm2 startup centos"
  },
  "dependencies": {
    "apn": "^1.6.2",
    "body-parser": "~1.8.1",
    "cookie-parser": "~1.3.3",
    "debug": "~2.0.0",
    "express": "~4.9.0",
    "jade": "~1.6.0",
    "morgan": "~1.3.0",
    "pm2": "^0.12.1",
    "serve-favicon": "~2.1.3"
  },
  "devDependencies": {
    "mocha": "^2.0.1",
    "supervisor": "^0.6.0"
  }
}

npm run命令详解

见https://github.com/i5ting/npm-run-test

pm2高级用法

stop

npm run stop

monit

npm run monit

更多内容 https://github.com/Unitech/PM2#monitoring

log

npm run log

更多内容 https://github.com/Unitech/PM2#log-facilities

$ pm2 logs
$ pm2 logs big-api
$ pm2 flush          # Clear all the logs

web

npm run web

访问 http://127.0.0.1:9615

startup

npm run startup

注意:示例的package.json里给出的是centos里增加开机启动项,如果是其他操作系统,请自己更改

更多内容 https://github.com/Unitech/PM2#startup-script-generation

$ pm2 startup <ubuntu|centos|gentoo|systemd>

资源

https://github.com/Unitech/PM2

4 回复

说 deploy 但是没有讲

	pm2 deploy ecosystem.json production

@russj 一会补上,哈哈

很好,收藏

好奇怪的一个问题

pm2 ecosystem or pm2 generate报错


path.js:360
        throw new TypeError('Arguments to path.join must be strings');
              ^
TypeError: Arguments to path.join must be strings
    at path.js:360:15
    at Array.filter (native)
    at Object.exports.join (path.js:358:36)
    at Object.CLI.generateSample (/usr/local/lib/node_modules/pm2/lib/CLI.js:1132:25)
    at Command.<anonymous> (/usr/local/lib/node_modules/pm2/bin/pm2:378:9)
    at Command.listener (/usr/local/lib/node_modules/pm2/node_modules/commander/index.js:248:8)
    at Command.emit (events.js:98:17)
    at Command.parseArgs (/usr/local/lib/node_modules/pm2/node_modules/commander/index.js:490:12)
    at Command.parse (/usr/local/lib/node_modules/pm2/node_modules/commander/index.js:376:21)
    at /usr/local/lib/node_modules/pm2/bin/pm2:106:15

回到顶部