代码之家  ›  专栏  ›  技术社区  ›  Edito

如何在pm2下运行nuxt?

  •  3
  • Edito  · 技术社区  · 6 年前

    我有两个nuxt项目需要在服务器上运行。每当我在本地运行应用程序时,它似乎都在使用: npm run dev

    用于此操作的命令是: sudo pm2 start npm --name "dev" -- dev ,即使我单独运行应用程序,也会出错。 sudo pm2 start npm --name "app1" -- app1:dev sudo pm2 start npm --name "app2" -- app2:dev


    包.json

    {
        ...
        "scripts": {
            "app1:dev": "nuxt --config-file src/app1/nuxt.config.js -p=3000",
            "app2:dev": "nuxt --config-file src/app2/nuxt.config.js -p=4000",
            "dev": "concurrently \"npm run app1:dev\" \"npm run app2:dev\"",
        },
        "dependencies": {
            ...
        },
        "devDependencies": {
            "concurrently": "^3.6.0",
            "cross-env": "^5.2.0"
        }
    }
    


    pm2日志

    /home/ubuntu/.pm2/pm2.log :
    PM2        | [2018-08-16T10:05:55.046Z] PM2 log: ===============================================================================
                                                                    ...
    PM2        | [2018-08-16T10:07:32.825Z] PM2 log: App [app1] with id [0] and pid [11135], exited with code [1] via signal [SIGINT]
    PM2        | [2018-08-16T10:07:32.827Z] PM2 log: Starting execution sequence in -fork mode- for app name:app1 id:0
    PM2        | [2018-08-16T10:07:32.828Z] PM2 log: App name:app1 id:0 online
    PM2        | [2018-08-16T10:07:33.105Z] PM2 log: App [app1] with id [0] and pid [11145], exited with code [1] via signal [SIGINT]
    PM2        | [2018-08-16T10:07:33.106Z] PM2 log: Starting execution sequence in -fork mode- for app name:app1 id:0
    PM2        | [2018-08-16T10:07:33.108Z] PM2 log: App name:app1 id:0 online
    PM2        | [2018-08-16T10:07:33.383Z] PM2 log: App [app1] with id [0] and pid [11155], exited with code [1] via signal [SIGINT]
    PM2        | [2018-08-16T10:07:33.383Z] PM2 log: Script /usr/local/bin/npm had too many unstable restarts (16). Stopped. "errored"
    
    /home/ubuntu/.pm2/logs/app1-error.log :
    /home/ubuntu/.pm2/logs/app1-out.log :
                                ...
    0|app1   | Specify configs in the ini-formatted file:
    0|app1   |     /home/ubuntu/.npmrc
    0|app1   | or on the command line via: npm <command> --key value
    0|app1   | Config info can be viewed via: npm help config
    0|app1   |
    0|app1   | npm@5.6.0 /usr/local/lib/node_modules/npm
    0|app1   |
    0|app1   | Usage: npm <command>
    0|app1   |
    0|app1   | where <command> is one of:
    0|app1   |     access, adduser, bin, bugs, c, cache, completion, config,
    0|app1   |     ddp, dedupe, deprecate, dist-tag, docs, doctor, edit,
    0|app1   |     explore, get, help, help-search, i, init, install,
    0|app1   |     install-test, it, link, list, ln, login, logout, ls,
    0|app1   |     outdated, owner, pack, ping, prefix, profile, prune,
    0|app1   |     publish, rb, rebuild, repo, restart, root, run, run-script,
    0|app1   |     s, se, search, set, shrinkwrap, star, stars, start, stop, t,
    0|app1   |     team, test, token, tst, un, uninstall, unpublish, unstar,
    0|app1   |     up, update, v, version, view, whoami
    0|app1   |
    0|app1   | npm <command> -h     quick help on <command>
    0|app1   | npm -l           display full usage info
    0|app1   | npm help <term>  search for help on <term>
    0|app1   | npm help npm     involved overview
    0|app1   |
                              ...
    

    额外信息:
    服务器: Ubuntu 16.04
    5.6.0
    nuxt版本: 1.4.2
    pm2版本: 3.0.3
    8.11.1

    3 回复  |  直到 6 年前
        1
  •  5
  •   JC97    6 年前

    要使多个nuxt实例与pm2一起工作,您需要指向 nuxt-start 脚本输入 node_modules/nuxt/bin/nuxt-start

    This guide 解释得很好。 生态系统 Here 是一个包含所有可用参数的列表。

    你的应该是这样的:

    module.exports = {
      apps: [
        {
          name: 'app1',
          port: 3000,
          script: './node_modules/nuxt/bin/nuxt-start',
          cwd: '/home/user/your-nuxt-project/app1',
          env: {
            NODE_ENV: 'development'
          },
          env_production: {
            NODE_ENV: 'production'
          }
        },
        {
          name: 'app2',
          port: 4000,
          script: './node_modules/nuxt/bin/nuxt-start',
          cwd: '/home/user/your-nuxt-project/app2',
          env: {
            NODE_ENV: 'development'
          },
          env_production: {
            NODE_ENV: 'production'
          }
        }
      ]
    };
    

    然后简单地cd到项目dir并运行 sudo pm2 start . 它将自动找到配置文件并同时运行两个应用程序。

        2
  •  3
  •   Fatihd    6 年前

    代码如下:

    pm2 start npm --name "anyName" -- run dev
    
        3
  •  1
  •   PirateApp    5 年前

    首先将nuxt安装为npm install--保存nuxt

    在项目文件夹中创建一个名为config的文件夹,该文件夹是nuxt生成的服务器页面中间件和其他目录的同级

    添加4个.env文件.env.deb、.env.dev、.env.sta、.env.pro以进行调试、开发准备和生产

    运行命令pm2 ecosystem,它将生成默认文件

    它将每个环境创建为一个单独的应用程序

    pm2 start ecosystem.config.js --only myapp_sta

    如果你想的话,你可以一次跑完4个!!!

    const dotenv = require('dotenv')
    
    const autorestart = true
    const watch = false
    const maxMemoryRestart = '512M'
    
    module.exports = {
      apps: [
        {
          name: 'myapp_dev',
          script: 'npm run clear && npm run dev',
          instances: 1,
          autorestart,
          watch,
          max_memory_restart: maxMemoryRestart,
          env: dotenv.config({ path: './config/.env.dev' }).parsed
        },
        {
          name: 'myapp_deb',
          script: 'npm run clear && npm run deb',
          instances: 1,
          autorestart,
          watch,
          max_memory_restart: maxMemoryRestart,
          env: dotenv.config({ path: './config/.env.deb' }).parsed
        },
        {
          name: 'myapp_sta',
          script: 'npm run clear && npm run sta',
          instances: 1,
          autorestart,
          watch,
          max_memory_restart: maxMemoryRestart,
          env: dotenv.config({ path: './config/.env.sta' }).parsed
        },
        {
          name: 'myapp_pro',
          script: 'npm run clear && npm run build && npm run start',
          instances: 1,
          autorestart,
          watch,
          max_memory_restart: maxMemoryRestart,
          env: dotenv.config({ path: './config/.env.pro' }).parsed
        }
      ],
    
      deploy: {
        myapp_dev: {
          user: 'zupstock',
          host: '192.168.1.103',
          ref: 'origin/master',
          repo: 'git@github.com:owner/myapp_v1.git',
          path: '/',
          'post-deploy':
            'cd myapp_v1 && npm install && pm2 startOrRestart ecosystem.config.js --only myapp_dev'
        }
      }
    }
    
        4
  •  0
  •   Mohit Kumar Bordia    6 年前

    如果有效,请尝试下面的命令

    sudo pm2 start npm -- app1:dev