nuxt.js项目使用pm2部署测试环境和生产环境的配置

先说下需求:

目前是一个项目是一个nuxt-app,如果要启动多个nuxt项目时,这时就需要不同的项目使用不同的端口号,这样可以做到互不影响。此项目中的用的端口号是5000,但是在ecosystem.config.js中怎么都加不上端口号,后来经过网上搜索和自己的一番配置,生产环境还是监听不到5000端口,其实质还是服务没有启动。后来经过查看官方文档和参考https://juejin.im/post/5caf3d20e51d456e586640a0 这个,最终生产环境可以监听到5000端口了。

 

一、最后经过上网搜索。如下配置是可用的:

module.exports = {

  apps: [{
      name: 'anchor',
      script: './server.js',
      append_env_to_name: true,
      cwd: './',

      exec_mode: "cluster",

      // Options reference: https://pm2.io/doc/en/runtime/reference/ecosystem-file/
      out_file: "./logs/out-0.log",
      error_file: "./logs/err-0.log",
      merge_logs: true,
      log_data_format: 'YYYY-MM-DD HH:mm Z',
      autorestart: true,
      watch: [
        'src',
        'build'
      ],
      max_memory_restart: '1G',
      node_args: '--harmony',
      env: {
        PORT: 5000, 
        NODE_ENV: "production"
      }
    }

  ],

};

package.json:

{
  "name": "anchor",
  "version": "1.0.0",
  "description": "My splendiferous Nuxt.js project",
  "author": "",
  "private": true,
  "scripts": {
    "dev": "cross-env NODE_ENV=development PORT=5000 nodemon server/index.js --watch server --exec babel-node",
    "build": "nuxt build",
    "start": "cross-env NODE_ENV=production PORT=5000 nodemon server/index.js --exec babel-node",
    "prod": "nuxt start && node server/index.js  --exec babel-node",
    "generate": "nuxt generate",
    "pm2": "pm2 start ./ecosystem.config.js --interpreter babel-node --env production",
    "bundle": "rollup -c"
  },

npm run build之后再运行npm run pm2可以启动服务:

但是不知道为什么总是启动两个,明明配置文件中是一个啊,有大神知道是因为什么啊!!!!!!,后来是启动一个服务了,其实也没改什么,看来还得多看看pm2的文档了。

而且5000端口也可以监听到了。

另外在本地连接到生产环境,启动的结果如下:

而且5000端口也可以监听到:

netstat -tunlp

 

剩下的就是让运维配置nginx反向代理,把5000端口映射到域名上。

 

二、另外,可能有时需要配置测试环境和生产环境:

分环境的ecosystem.config.js配置如下:

module.exports = {

  apps: [{
      name: 'anchor',
      // port: '5000',
      script: './server.js',
      append_env_to_name: true,
      cwd: './',

      // exec_interpreter: "babel-node", //此配置就是使用babel-node去执行nodejs文件
      exec_mode: "cluster",

      // Options reference: https://pm2.io/doc/en/runtime/reference/ecosystem-file/
      out_file: "./logs/out-0.log",
      error_file: "./logs/err-0.log",
      merge_logs: true,
      log_data_format: 'YYYY-MM-DD HH:mm Z',
      autorestart: true,
      watch: [
        'src',
        'build'
      ],
      max_memory_restart: '1G',
      node_args: '--harmony',
      env: {
        PORT: 5000,
        NODE_ENV: "production"
      }
    },
    {
      name: 'anchor-test',
      script: './server.js',
      append_env_to_name: true,
      cwd: './',

      exec_mode: "cluster",

      // Options reference: https://pm2.io/doc/en/runtime/reference/ecosystem-file/
      out_file: "./logs/out-0.log",
      error_file: "./logs/err-0.log",
      merge_logs: true,
      log_data_format: 'YYYY-MM-DD HH:mm Z',
      autorestart: true,
      watch: [
        'src',
        'build'
      ],
      max_memory_restart: '1G',
      node_args: '--harmony',
      env: {
        PORT: 5000,
        NODE_ENV: "test"
      }
    }


  ],


};

 

package.json文件如下 :

{
  "name": "anchor",
  "version": "1.0.0",
  "description": "My splendiferous Nuxt.js project",
  "author": "",
  "private": true,
  "scripts": {
    "dev": "cross-env NODE_ENV=development PORT=5000 nodemon server/index.js --watch server --exec babel-node",
    "build": "nuxt build",
    "start": "cross-env NODE_ENV=production PORT=5000 nodemon server/index.js --exec babel-node",
    "prod": "nuxt start && node server/index.js  --exec babel-node",
    "generate": "nuxt generate",
    "pm2-test": "pm2 start ./ecosystem.config.js --interpreter babel-node --only anchor-test --env test",//其中--only anchor-test 是在生产环境只启动测试环境的pm2(anchor-test是ecosystem.config.js中的name)
    "pm2-prod": "pm2 start ./ecosystem.config.js --interpreter babel-node --only anchor --env production",  //其中--only anchor 是在生产环境只启动生产环境的pm2(anchor是ecosystem.config.js中的name)
    "bundle": "rollup -c"
  },

 

生产环境启动命令为:npm run pm2-prod   (启动之前先运行pm2 list查看一下有哪些pm2 的进程在启动着,再运行pm2 delete 项目名称或id ,先杀掉进程)

测试环境启动命令为:npm run pm2-test

 

三、如果想查看node在环境中的具体信息,可以运行如下命令:pm2 show 项目名称   

 

四、监控每个 node 进程的 cpu 和内存使用情况

使用如下命令:

$ pm2 monit

 

 

参考链接:

https://juejin.im/post/5be406705188256dbb5176f9

https://juejin.im/post/5caf3d20e51d456e586640a0

https://www.jianshu.com/p/4f8b5806dafb

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章