How to run nuxt under pm2?
我有2个nuxt项目需要在服务器上运行。 每当我在本地运行该应用程序时,它似乎都可以使用:
用于此的命令是:
package.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | { ... "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日志
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | /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 | [email protected] /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 | ... |
这一切是什么意思,pm2不能识别npm命令吗? 我在这里缺少参数吗? ...
额外信息:
服务器:
npm版本:
nuxt版本:
pm2版本:
节点版本:
下面的代码:
1 | pm2 start npm --name"anyName" -- run dev |
要使多个nuxt实例与pm2一起使用,您需要指向
本指南对此进行了很好的解释。
总结一下:您需要为应用程序配置生态系统(ecosystem.config.js),在其中为应用程序设置所有给定参数。这是所有可用参数的列表。
您的外观应如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 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插入项目目录并运行
对于那些只想以生产模式启动应用程序的人:
Seeing this page comes up at the top of search results of how to use Nuxt.js with PM2, I thought I should add this answer.
尽管PM2是一个名为
1 2 3 4 5 6 7 8 9 10 11 12 13 | module.exports = { apps: [ { name: 'NuxtAppName', exec_mode: 'cluster', // Optional: If you want it run multiple instances. instances: 'max', // Or a number of instances. // 'max' auto detects how many CPU cores there are. // The previous option must exist to use the above. script: './node_modules/nuxt/bin/nuxt.js', args: 'start', }, ], } |
现在使用
并与
检查状态
您的Nuxt.js应用程序现在可以使用了!
资源
尽管JC97的答案很不错,但Nuxt已升级,如果您要分别运行调试,开发,登台和生产设置,则还需要一个更加分隔的生态系统文件
首先将nuxt安装为npm install --save nuxt
在项目文件夹中创建一个名为config的文件夹,此文件夹是nuxt生成的服务器页面中间件和其他目录的同级文件
添加4个.env文件.env.deb,.env.dev,.env.sta,.env.pro用于调试,开发阶段和生产
运行命令pm2生态系统,它将生成默认文件
将默认文件替换为以下文件
它将每个环境创建为一个单独的应用程序
如果只想运行登台版本,则可以将其作为
如果需要,可以一次运行所有4个!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | 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: '[email protected]:owner/myapp_v1.git', path: '/', 'post-deploy': 'cd myapp_v1 && npm install && pm2 startOrRestart ecosystem.config.js --only myapp_dev' } } } |
对我来说,这适用于单个应用程序
https://nuxtjs.org/faq/deployment-pm2/
1 2 3 4 5 6 7 8 9 10 11 | module.exports = { apps: [ { name: 'NuxtAppName', exec_mode: 'cluster', instances: 'max', // Or a number of instances script: './node_modules/nuxt/bin/nuxt.js', args: 'start' } ] } |
如果有效,请尝试以下命令
1 | sudo pm2 start npm -- app1:dev |