npm安装无法通过php system()运行(exec/shell_exec) [英] npm install not working via php system() (exec / shell_exec)

查看:80
本文介绍了npm安装无法通过php system()运行(exec/shell_exec)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用npm install在其他软件包中安装gulp-sass.使用CLI(使用sudo su forge)可以正常工作:

  cd/path/to/package.json/npm安装 

package.json如下:

  {"name":骨骼","version":"1.0.0",作者":[我< me@mycompany.co.uk>"],私人":是的,"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"},依赖关系":{"node-notifier":"^ 4.2.1","streamqueue":"^ 0.1.3"}} 

在命令行上使用PHP也可以正常工作

  php -r"system('npm install');" 

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

 <?phpset_time_limit(0);chdir('/path/to/package.json/');回声< pre>";回声shell_exec('npm install 2>& 1');死(); 

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

  81427错误Linux 4.5.5-x86_64-linode6981428错误argv"/usr/local/bin/node""/usr/local/bin/npm"安装"81429错误节点v0.12.781430错误npm v2.11.381431错误文件sh81432错误路径sh81433错误代码ELIFECYCLE81434错误errno ENOENT81435错误syscall生成sh81436错误的node-sass@2.1.1安装:`node scripts/install.js`81436错误生成sh ENOENT81437错误在node-sass@2.1.1安装脚本'node scripts/install.js'处失败.81437错误这很可能是node-sass软件包的问题,81437错误不是npm本身.81437错误告诉作者这在您的系统上失败:81437错误节点脚本/install.js81437错误您可以通过以下方式获取其信息:81437错误npm所有者ls节点-ass81437错误上面可能还有其他日志记录输出. 

我尝试将两种情况与以下情况进行比较:

 其中npm 

都返回'/usr/local/bin/npm'

-

  echo $ USER 

都返回'forge'

-

  echo $ GROUP 

都返回"(空)

-

  echo $ PATH 

CLI返回:

 <代码>/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games 

PHP返回:

 <代码>/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin 

-

 其中哪一个 

都返回'/bin/sh'

-

  ps $$ 

CLI返回:

  PID TTY STAT TIME COMMAND13111点/2 S 0:00打击 

PHP返回:

  PID TTY STAT TIME COMMAND25759?S 0:00 sh -c ps $$ 

-

  npm -v 

都返回'2.11.3'

-

  npm版本 

CLI返回:

  {骨架:"1.0.0",npm:"2.11.3",http_parser:"2.3",模组:'14',节点:"0.12.7",openssl:'1.0.1p',uv:"1.6.1",v8:"3.28.71.19",zlib:'1.2.8'} 

PHP返回无框架1.0.0:

  {npm:'2.11.3',http_parser:"2.3",模组:'14',节点:"0.12.7",openssl:'1.0.1p',uv:"1.6.1",v8:"3.28.71.19",zlib:'1.2.8'} 

-

npm config ls -l <​​/p>

CLI返回:

 ;CLI配置长=真user-agent ="npm/2.11.3节点/v0.12.7 linux x64";用户配置/home/forge/.npmrc忽略脚本=假loglevel =信息"前缀="/home/forge/npm"进度=错误;默认值访问权限=空始终验证=否bin-links = true浏览器= nullca =空缓存="/home/forge/.npm"缓存锁定重试= 10缓存锁定失效= 60000缓存锁定等待= 10000cache-max =空高速缓存最小值= 10cafile =未定义cert =空颜色=真深度=空描述=真dev =错误编辑器="vi"引擎限制=否提取重试= 2提取重试因子= 10fetch-retry-maxtimeout = 60000fetch-retry-mintimeout = 10000力=假git ="git"git-tag-version = true全局=假globalconfig ="/home/forge/npm/etc/npmrc"globalignorefile ="/home/forge/npm/etc/npmignore"小组= 1013标题="npm"https-proxy = null如果存在=假忽略脚本=假init-author-email ="init-author-name ="init-author-url ="init-license ="ISC"初始化模块="/home/forge/.npm-init.js"init-version ="1.0.0"json =错误键=空链接=假本地地址=未定义;loglevel =警告"(覆盖);long =假(覆盖)消息=%s"node-version ="0.12.7"npat =假onload-script = null可选=真可解析=假;prefix ="/usr/local"(已覆盖)生产=错误专有攻击=真代理= nullrebuild-bundle = true注册表="https://registry.npmjs.org/"回滚= true保存=否保存捆绑=否save-dev = false保存精确=否save-optional = falsesave-prefix ="^"范围="searchexclude = nullsearchopts ="searchsort =名称"shell ="/bin/bash"收缩包装=真sign-git-tag = false旋转=真strict-ssl = true标签=最新"tag-version-prefix ="v"tmp ="/tmp"umask = 2unicode = true不安全的烫发=真用法=假用户= 1013;user-agent ="npm/{npm-version}节点/{node-version} {平台} {arch}"(已覆盖)userconfig ="/home/forge/.npmrc"版本=假版本=假查看者="man" 

PHP返回:

 ;CLI配置长=真user-agent ="npm/2.11.3节点/v0.12.7 linux x64";用户配置/home/forge/.npmrc忽略脚本=假loglevel =警告"前缀="/home/forge/npm"进度=错误;默认值访问权限=空始终验证=否bin-links = true浏览器= nullca =空缓存="/home/forge/.npm"缓存锁定重试= 10缓存锁定失效= 60000缓存锁定等待= 10000cache-max =空高速缓存最小值= 10cafile =未定义cert =空颜色=真深度=空描述=真dev =错误编辑器="vi"引擎限制=否提取重试= 2提取重试因子= 10fetch-retry-maxtimeout = 60000fetch-retry-mintimeout = 10000力=假git ="git"git-tag-version = true全局=假globalconfig ="/home/forge/npm/etc/npmrc"globalignorefile ="/home/forge/npm/etc/npmignore"组= 1000标题="npm"https-proxy = null如果存在=假忽略脚本=假init-author-email ="init-author-name ="init-author-url ="init-license ="ISC"初始化模块="/home/forge/.npm-init.js"init-version ="1.0.0"json =错误键=空链接=假本地地址=未定义loglevel =警告";long =假(覆盖)消息=%s"node-version ="0.12.7"npat =假onload-script = null可选=真可解析=假;prefix ="/usr/local"(已覆盖)生产=错误专有攻击=真代理= nullrebuild-bundle = true注册表="https://registry.npmjs.org/"回滚= true保存=否保存捆绑=否save-dev = false保存精确=否save-optional = falsesave-prefix ="^"范围="searchexclude = nullsearchopts ="searchsort =名称"shell =重击"收缩包装=真sign-git-tag = false旋转=真strict-ssl = true标签=最新"tag-version-prefix ="v"tmp ="/tmp"umask = 18unicode = true不安全的烫发=真用法=假用户= 1000;user-agent ="npm/{npm-version}节点/{node-version} {平台} {arch}"(已覆盖)userconfig ="/home/forge/.npmrc"版本=假版本=假查看者="man" 

-

报告的可能相关的配置差异是:

 -组(1013/1000)-外壳程序(/bin/bash/bash)-我尝试使用/bin/bash -c"npm install",但似乎没有什么区别.-umask(2/18)-用户(1013/1000)(用户1000是伪造",用户1013是我本人) 

我全都没主意了.如果您对引入nginx/php-fpm为何导致此问题有任何想法,将不胜感激.

非常感谢.

解决方案

当作为npm脚本调用时, $ PATH 似乎不包含/bin ,因此它找不到每个错误文件sh /bin/sh .

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

  ln -s/bin/sh/usr/bin/sh 

I am installing gulp-sass among other packages using npm install. This works fine using CLI (with sudo su forge):

cd /path/to/package.json/
npm install

The package.json look like:

{
  "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"
  }
}

It also works fine using PHP at the command line:

php -r "system('npm install');"

But running the following PHP via nginx/PHP-FPM:

<?php

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

die();

Outputs a debug file containing the following lines:

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.

I have tried comparing the two scenarios with the following:

which npm

both return '/usr/local/bin/npm'

--

echo $USER

both return 'forge'

--

echo $GROUP

both return '' (empty)

--

echo $PATH

CLI returns:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

PHP returns:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

--

which sh

both return '/bin/sh'

--

ps $$

CLI returns:

    PID TTY      STAT   TIME COMMAND
  13111 pts/2    S      0:00 bash

PHP returns:

    PID TTY      STAT   TIME COMMAND
  25759 ?        S      0:00 sh -c ps $$

--

npm -v

both return '2.11.3'

--

npm version

CLI returns:

{ 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 returns without 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' }

--

npm config ls -l

CLI returns:

; 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 returns:

; 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"

--

Potentially relevant config differences reported are:

- 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)

I'm all out of ideas. If you have any idea as to why the introduction of nginx/php-fpm causes this issue, your input would be greatly appreciated.

Many thanks.

解决方案

It appears that $PATH doesn't include /bin when called as an npm script so it fails to find /bin/sh per error file sh.

The solution is to symlink /bin/sh to /usr/bin/sh:

ln -s /bin/sh /usr/bin/sh

这篇关于npm安装无法通过php system()运行(exec/shell_exec)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆