首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >npm安装不能通过php system() (exec / shell_exec)

npm安装不能通过php system() (exec / shell_exec)
EN

Stack Overflow用户
提问于 2016-08-01 20:18:43
回答 1查看 1.5K关注 0票数 5

我正在使用npm install安装gulp-sass和其他软件包。使用CLI (使用sudo su forge)可以很好地工作:

代码语言:javascript
复制
cd /path/to/package.json/
npm install

package.json如下所示:

代码语言:javascript
复制
{
  "name": "skeleton",
  "version": "1.0.0",
  "authors": [
    "Me <me@mycompany.co.uk>"
  ],
  "private": true,
  "devDependencies": {
    "gulp": "^3.8.11",
    "gulp-autoprefixer": "^2.1.0",
    "gulp-concat": "^2.5.2",
    "gulp-minify-css": "^1.0.0",
    "gulp-notify": "^2.2.0",
    "gulp-sass": "^1.3.3",
    "gulp-sourcemaps": "^1.5.2",
    "gulp-uglify": "^1.2.0"
  },
  "dependencies": {
    "node-notifier": "^4.2.1",
    "streamqueue": "^0.1.3"
  }
}

在命令行中使用PHP也可以很好地工作:

代码语言:javascript
复制
php -r "system('npm install');"

但是通过nginx/ PHP -FPM运行以下PHP:

代码语言:javascript
复制
<?php

set_time_limit(0);
chdir('/path/to/package.json/');
echo "<pre>";
echo shell_exec('npm install 2>&1');

die();

输出包含以下行的调试文件:

代码语言:javascript
复制
81427 error Linux 4.5.5-x86_64-linode69
81428 error argv "/usr/local/bin/node" "/usr/local/bin/npm" "install"
81429 error node v0.12.7
81430 error npm  v2.11.3
81431 error file sh
81432 error path sh
81433 error code ELIFECYCLE
81434 error errno ENOENT
81435 error syscall spawn sh
81436 error node-sass@2.1.1 install: `node scripts/install.js`
81436 error spawn sh ENOENT
81437 error Failed at the node-sass@2.1.1 install script 'node scripts/install.js'.
81437 error This is most likely a problem with the node-sass package,
81437 error not with npm itself.
81437 error Tell the author that this fails on your system:
81437 error     node scripts/install.js
81437 error You can get their info via:
81437 error     npm owner ls node-sass
81437 error There is likely additional logging output above.

我尝试将这两个场景与以下两个场景进行比较:

代码语言:javascript
复制
which npm

两者都返回'/usr/local/bin/npm‘

--

代码语言:javascript
复制
echo $USER

两者都返回'forge‘

--

代码语言:javascript
复制
echo $GROUP

两者都返回'‘(空)

--

代码语言:javascript
复制
echo $PATH

CLI返回:

代码语言:javascript
复制
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

PHP返回:

代码语言:javascript
复制
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

--

代码语言:javascript
复制
which sh

两者都返回'/bin/sh‘

--

代码语言:javascript
复制
ps $$

CLI返回:

代码语言:javascript
复制
    PID TTY      STAT   TIME COMMAND
  13111 pts/2    S      0:00 bash

PHP返回:

代码语言:javascript
复制
    PID TTY      STAT   TIME COMMAND
  25759 ?        S      0:00 sh -c ps $$

--

代码语言:javascript
复制
npm -v

两者都返回'2.11.3‘

--

代码语言:javascript
复制
npm version

CLI返回:

代码语言:javascript
复制
{ skeleton: '1.0.0',
  npm: '2.11.3',
  http_parser: '2.3',
  modules: '14',
  node: '0.12.7',
  openssl: '1.0.1p',
  uv: '1.6.1',
  v8: '3.28.71.19',
  zlib: '1.2.8' }

PHP返回没有骨架1.0.0的内容:

代码语言:javascript
复制
{ npm: '2.11.3',
  http_parser: '2.3',
  modules: '14',
  node: '0.12.7',
  openssl: '1.0.1p',
  uv: '1.6.1',
  v8: '3.28.71.19',
  zlib: '1.2.8' }

--

npm配置ls -l

CLI返回:

代码语言:javascript
复制
; cli configs
long = true
user-agent = "npm/2.11.3 node/v0.12.7 linux x64"

; userconfig /home/forge/.npmrc
ignore-scripts = false
loglevel = "info"
prefix = "/home/forge/npm"
progress = false

; default values
access = null
always-auth = false
bin-links = true
browser = null
ca = null
cache = "/home/forge/.npm"
cache-lock-retries = 10
cache-lock-stale = 60000
cache-lock-wait = 10000
cache-max = null
cache-min = 10
cafile = undefined
cert = null
color = true
depth = null
description = true
dev = false
editor = "vi"
engine-strict = false
fetch-retries = 2
fetch-retry-factor = 10
fetch-retry-maxtimeout = 60000
fetch-retry-mintimeout = 10000
force = false
git = "git"
git-tag-version = true
global = false
globalconfig = "/home/forge/npm/etc/npmrc"
globalignorefile = "/home/forge/npm/etc/npmignore"
group = 1013
heading = "npm"
https-proxy = null
if-present = false
ignore-scripts = false
init-author-email = ""
init-author-name = ""
init-author-url = ""
init-license = "ISC"
init-module = "/home/forge/.npm-init.js"
init-version = "1.0.0"
json = false
key = null
link = false
local-address = undefined
; loglevel = "warn" (overridden)
; long = false (overridden)
message = "%s"
node-version = "0.12.7"
npat = false
onload-script = null
optional = true
parseable = false
; prefix = "/usr/local" (overridden)
production = false
proprietary-attribs = true
proxy = null
rebuild-bundle = true
registry = "https://registry.npmjs.org/"
rollback = true
save = false
save-bundle = false
save-dev = false
save-exact = false
save-optional = false
save-prefix = "^"
scope = ""
searchexclude = null
searchopts = ""
searchsort = "name"
shell = "/bin/bash"
shrinkwrap = true
sign-git-tag = false
spin = true
strict-ssl = true
tag = "latest"
tag-version-prefix = "v"
tmp = "/tmp"
umask = 2
unicode = true
unsafe-perm = true
usage = false
user = 1013
; user-agent = "npm/{npm-version} node/{node-version} {platform} {arch}" (overridden)
userconfig = "/home/forge/.npmrc"
version = false
versions = false
viewer = "man"

PHP返回:

代码语言:javascript
复制
; cli configs
long = true
user-agent = "npm/2.11.3 node/v0.12.7 linux x64"

; userconfig /home/forge/.npmrc
ignore-scripts = false
loglevel = "warn"
prefix = "/home/forge/npm"
progress = false

; default values
access = null
always-auth = false
bin-links = true
browser = null
ca = null
cache = "/home/forge/.npm"
cache-lock-retries = 10
cache-lock-stale = 60000
cache-lock-wait = 10000
cache-max = null
cache-min = 10
cafile = undefined
cert = null
color = true
depth = null
description = true
dev = false
editor = "vi"
engine-strict = false
fetch-retries = 2
fetch-retry-factor = 10
fetch-retry-maxtimeout = 60000
fetch-retry-mintimeout = 10000
force = false
git = "git"
git-tag-version = true
global = false
globalconfig = "/home/forge/npm/etc/npmrc"
globalignorefile = "/home/forge/npm/etc/npmignore"
group = 1000
heading = "npm"
https-proxy = null
if-present = false
ignore-scripts = false
init-author-email = ""
init-author-name = ""
init-author-url = ""
init-license = "ISC"
init-module = "/home/forge/.npm-init.js"
init-version = "1.0.0"
json = false
key = null
link = false
local-address = undefined
loglevel = "warn"
; long = false (overridden)
message = "%s"
node-version = "0.12.7"
npat = false
onload-script = null
optional = true
parseable = false
; prefix = "/usr/local" (overridden)
production = false
proprietary-attribs = true
proxy = null
rebuild-bundle = true
registry = "https://registry.npmjs.org/"
rollback = true
save = false
save-bundle = false
save-dev = false
save-exact = false
save-optional = false
save-prefix = "^"
scope = ""
searchexclude = null
searchopts = ""
searchsort = "name"
shell = "bash"
shrinkwrap = true
sign-git-tag = false
spin = true
strict-ssl = true
tag = "latest"
tag-version-prefix = "v"
tmp = "/tmp"
umask = 18
unicode = true
unsafe-perm = true
usage = false
user = 1000
; user-agent = "npm/{npm-version} node/{node-version} {platform} {arch}" (overridden)
userconfig = "/home/forge/.npmrc"
version = false
versions = false
viewer = "man"

--

报告的潜在相关配置差异包括:

代码语言:javascript
复制
- group (1013 / 1000)
- shell (/bin/bash / bash) - I've tried using /bin/bash -c "npm install" but it seems to make no difference.
- umask (2 / 18)
- user (1013 / 1000) (user 1000 is "forge", user 1013 is myself)

我已经没有主意了。如果您对nginx/php-fpm的引入导致这个问题有任何想法,我们将非常感谢您的意见。

非常感谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-24 20:14:28

在作为npm脚本调用时,$PATH似乎不包含/bin,因此它无法找到每个error file sh/bin/sh

解决方案是将/bin/sh符号链接到/usr/bin/sh

代码语言:javascript
复制
ln -s /bin/sh /usr/bin/sh
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38698695

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档