最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

“ exec cwd”与JSON文件中设置的“ cwd”不同吗?

运维笔记admin10浏览0评论

“ exec cwd”与JSON文件中设置的“ cwd”不同吗?

“ exec cwd”与JSON文件中设置的“ cwd”不同吗?

Hiyah,

我的node.js应用程序显然不了解cwd变量...

[我在ecosystem.config.js中添加了以下代码,因为尽管我在path: '/home/ubuntu/projet2/', 块中写了deploy,但pm2仍试图从/ home / ubuntu / projet2 / source取消我的应用程序。

    cwd: '/home/ubuntu/projet2/',

现在,我有这个:

module.exports = {
  apps: [{
    name: 'projet2',
    cwd: '/home/ubuntu/projet2/',
    script: './index.js'
  }],

pm2现在在gitlab-CI执行其工作时运行平稳,我再也没有错误,并且该过程很好地“在线”运行。但是,当我通过浏览器检查远程服务器时,出现此错误:

Error: Failed to lookup view "./list.ejs" in views directory "/home/ubuntu/projet2/source/views"

我认为此错误是由于exec cwd的值(命令pm2 show 2的结果:

┌───────────────────┬──────────────────────────────────────────┐
│ status            │ online                                   │
│ name              │ projet2                                  │
│ version           │ N/A                                      │
│ restarts          │ 63                                       │
│ uptime            │ 12m                                      │
│ script path       │ /home/ubuntu/projet2/index.js            │
│ script args       │ N/A                                      │
│ error log path    │ /home/ubuntu/.pm2/logs/projet2-error.log │
│ out log path      │ /home/ubuntu/.pm2/logs/projet2-out.log   │
│ pid path          │ /home/ubuntu/.pm2/pids/projet2-2.pid     │
│ interpreter       │ node                                     │
│ interpreter args  │ N/A                                      │
│ script id         │ 2                                        │
│ exec cwd          │ /home/ubuntu/projet2/source              │
│ exec mode         │ fork_mode                                │
│ node.js version   │ 8.16.1                                   │
│ node env          │ N/A                                      │
│ watch & reload    │ ✘                                        │
│ unstable restarts │ 0                                        │
│ created at        │ 2019-09-28T16:02:30.137Z                 │
└───────────────────┴──────────────────────────────────────────┘

为什么服务器一直在寻找这样的source目录?我从来没有提到过,cwd应该解决这个问题吗?我不明白我对该项目的所有文件都位于projet2文件夹中,并且包含views所在的list.js文件夹。

感谢您的帮助,我不知道如何以一种干净优雅的方式解决此问题...

M。

编辑:

整个生态系统.config.js:

module.exports = {
  apps: [{
    name: 'projet2',
    cwd: '/home/ubuntu/projet2/',
    script: './index.js'
  }],
  deploy: {
    production: {
      user: 'ubuntu',
      host: 'ec2-18-188-86-74.us-east-2pute.amazonaws',
      ref: 'origin/master',
      repo: '[email protected]:dofrema/projet2.git',
      key: './pk.pem',
      path: '/home/ubuntu/projet2/',
      'post-deploy': 'npm install && pm2 startOrRestart ~/projet2/ecosystem.config.js'
    }
  }
}

。gitlab-ci.yml:

# This file is a template, and might need editing before it works on your project.
# Official framework image. Look for the different tagged releases at:
# /
image: node:alpine

stages:
  - deploy

deploy:
  stage: deploy
  before_script:
    # Install ssh-agent if not already installed, it is required by Docker.
    # (change apt-get to yum if you use a CentOS-based image)
    - 'which ssh-agent || ( apk add --update openssh )'

    # Add bash
    - apk add --update bash

    # Add git
    - apk add --update git

    # Run ssh-agent (inside the build environment)
    - eval $(ssh-agent -s)

    # Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
    - echo "$SSH_PRIVATE_KEY" > "./pk.pem"
    - chmod 400 ./pk.pem
    - echo "$SSH_PRIVATE_KEY" | ssh-add -

    # For Docker builds disable host key checking. Be aware that by adding that
    # you are suspectible to man-in-the-middle attacks.
    # WARNING: Use this only with the Docker executor, if you use it with shell
    # you will overwrite your user's SSH config.
    - mkdir -p ~/.ssh
    - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
    # In order to properly check the server's host key, assuming you created the
    # SSH_SERVER_HOSTKEYS variable previously, uncomment the following two lines
    # instead.
    # - mkdir -p ~/.ssh
    # - '[[ -f /.dockerenv ]] && echo "$SSH_SERVER_HOSTKEYS" > ~/.ssh/known_hosts'
  script:
  - npm i -g pm2
  - pm2 deploy ecosystem.config.js production
  only:
  - master
回答如下:

我想我有一些有用的信息。如果使用pm2 start 2运行节点服务器,则会出现此错误:

Error: Failed to lookup view "list.ejs" in views directory "/home/ubuntu/projet2/source/views"

如果我使用node ./projet2/index.js(从〜目录)运行节点服务器,则会出现此错误:Error: Failed to lookup view "list.ejs" in views directory "/home/ubuntu/views"

如果我从~/projet2/文件夹运行节点服务器,并使用命令node ./index.js,则我的应用程序可以运行。

所以,这里有两个问题:-我在其中编写运行节点服务器的命令的文件夹设置了整个服务器的根路径(我认为这实际上不是问题,但我不知道它像oO一样工作)-pm2似乎从文件夹~/projet2/source运行节点服务器,但我不知道为什么,或如何修复它。实际上,由于第一次尝试创建source时出现此错误,因此创建了pm2 start 2目录:

{ Error: Cannot find module '/home/ubuntu/projet2/source/index.js'
    at Function.Module._resolveFilename (module.js:548:15)
    at Function.Module._load (module.js:475:25)
    at Object.<anonymous> (/home/ubuntu/.nvm/versions/node/v8.16.1/lib/node_modules/pm2/lib/ProcessContainerFork.js:27:21)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Function.Module.runMain (module.js:694:10)
    at startup (bootstrap_node.js:204:16) code: 'MODULE_NOT_FOUND' }
发布评论

评论列表(0)

  1. 暂无评论