使用 Elastic Beanstalk 权限问题部署 NodeJS

Deploy NodeJS with Elastic Beanstalk permission problem

我正在尝试将我的 NodeJS 应用程序上传到 Amazon AWS 中的 Elastic Beanstalk。但是当执行 npm install 时,它会出现一些权限被拒绝的情况,因此它无法 运行 该应用程序。这是日志

Application update failed at 2018-10-02T15:18:14Z with exit status 1 and error: Hook /opt/elasticbeanstalk/hooks/appdeploy/pre/50npm.sh failed.

+ /opt/elasticbeanstalk/containerfiles/ebnode.py --action npm-install
npm WARN deprecated tar.gz@1.0.7: ⚠️ WARNING ⚠️ tar.gz module has been deprecated and your application is vulnerable. Please use tar module instead: https://npmjs.com/tar
npm WARN deprecated fs-promise@2.0.3: Use mz or fs-extra^3.0 with Promise Support

> scrypt@6.0.3 preinstall /tmp/deployment/application/node_modules/scrypt
> node node-scrypt-preinstall.js

Error: Error: Command failed: ./configure
./configure: line 1904: config.log: Permission denied
./configure: line 1914: config.log: Permission denied


> keccak@1.4.0 install /tmp/deployment/application/node_modules/keccak
> npm run rebuild || echo "Keccak bindings compilation fail. Pure JS implementation will be used."


> keccak@1.4.0 rebuild /tmp/deployment/application/node_modules/keccak
> node-gyp rebuild

gyp ERR! configure error
gyp ERR! stack Error: EACCES: permission denied, mkdir '/tmp/deployment/application/node_modules/keccak/build'
gyp ERR! System Linux 4.14.67-66.56.amzn1.x86_64
gyp ERR! command "/opt/elasticbeanstalk/node-install/node-v8.11.4-linux-x64/bin/node" "/opt/elasticbeanstalk/node-install/node-v8.11.4-linux-x64/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /tmp/deployment/application/node_modules/keccak
gyp ERR! node -v v8.11.4
gyp ERR! node-gyp -v v3.6.2
gyp ERR! not ok
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! keccak@1.4.0 rebuild: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the keccak@1.4.0 rebuild script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?

┌──────────────────────────────────────────────────┐
│ npm update check failed │
│ Try running with sudo or get access │
│ to the local update config store via │
│ sudo chown -R $USER:$(id -gn $USER) /tmp/.config │
└──────────────────────────────────────────────────┘
Keccak bindings compilation fail. Pure JS implementation will be used.

> scrypt@6.0.3 install /tmp/deployment/application/node_modules/scrypt
> node-gyp rebuild

gyp ERR! configure error
gyp ERR! stack Error: EACCES: permission denied, mkdir '/tmp/deployment/application/node_modules/scrypt/build'
gyp ERR! System Linux 4.14.67-66.56.amzn1.x86_64
gyp ERR! command "/opt/elasticbeanstalk/node-install/node-v8.11.4-linux-x64/bin/node" "/opt/elasticbeanstalk/node-install/node-v8.11.4-linux-x64/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /tmp/deployment/application/node_modules/scrypt
gyp ERR! node -v v8.11.4
gyp ERR! node-gyp -v v3.6.2
gyp ERR! not ok
npm WARN cryptocms@0.1.0 No repository field.

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! scrypt@6.0.3 install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the scrypt@6.0.3 install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! /tmp/.npm/_logs/2018-10-02T15_18_14_229Z-debug.log
Running npm install: /opt/elasticbeanstalk/node-install/node-v8.11.4-linux-x64/bin/npm
Setting npm config jobs to 1
npm config jobs set to 1
Running npm with --production flag
Failed to run npm install. Snapshot logs for more details.
UTC 2018/10/02 15:18:14 cannot find application npm debug log at /tmp/deployment/application/npm-debug.log

Traceback (most recent call last):
File "/opt/elasticbeanstalk/containerfiles/ebnode.py", line 695, in <module>
main()
File "/opt/elasticbeanstalk/containerfiles/ebnode.py", line 677, in main
node_version_manager.run_npm_install(options.app_path)
File "/opt/elasticbeanstalk/containerfiles/ebnode.py", line 136, in run_npm_install
self.npm_install(bin_path, self.config_manager.get_container_config('app_staging_dir'))
File "/opt/elasticbeanstalk/containerfiles/ebnode.py", line 180, in npm_install
raise e
subprocess.CalledProcessError: Command '['/opt/elasticbeanstalk/node-install/node-v8.11.4-linux-x64/bin/npm', '--production', 'install']' returned non-zero exit status 1.
Incorrect application version "nodecms_0.1.0" (deployment 3). Expected version "nodecms-0.1.0" (deployment 1).

我寻找了一些解决方案,但没有人奏效。我还在 .ebextensions/00_change_npm_permissions.config 中添加了此脚本以添加 root 权限,但没有任何效果。

files:
"/opt/elasticbeanstalk/hooks/appdeploy/post/00_set_tmp_permissions.sh":
mode: "000755"
owner: root
group: root
content: |
  #!/usr/bin/env bash
  chown -R nodejs:nodejs /tmp

我是 Amazon AWS 的新手,希望有人能帮我解决这个问题。

我也尝试过将带有 unsafe-perm=true 的 .npmrc 文件放在 NodeJS 项目的根目录中(如此处所述:Beanstalk: Node.js deployment - node-gyp fails due to permission denied),但问题仍然存在

因此,在尝试了所有方法之后,唯一可行的解​​决方案是将环境属性添加到 Beanstalk 控制台中:

NPM_CONFIG_UNSAFE_PERM = true

加上我之前的尝试,EC2 终于能够部署我的 NodeJS 项目了。