简体   繁体   English

通过nginx / php-fpm exec调用时npm不运行

[英]npm does not run when called through nginx/php-fpm exec

I have a strange issue in my autodeploy script. 我的自动部署脚本中有一个奇怪的问题。

Basically that's the php script, that, when triggered through http, runs the other shell script with several commands like npm install and npm run build . 基本上就是php脚本,当通过http触发时,它会使用npm installnpm run build等几个命令来运行其他shell脚本。 This all works except the case when php script called from http nginx/php-fpm - this case npm command does not run. 除了从http nginx / php-fpm调用php脚本-这种情况npm命令不运行的情况以外,所有其他方法均有效。

I created the simple example repo to demonstrate the issue: https://github.com/Kasheftin/npm-bug 我创建了一个简单的示例存储库来演示该问题: https : //github.com/Kasheftin/npm-bug

We have test.php that just runs test.sh shell command: 我们有只运行test.sh shell命令的test.php:

<?php
  exec("/home/www/tests/npm-bug/test.sh > /home/www/tests/npm-bug/test.log");

We have test.sh that just runs npm run test command: 我们有只运行npm run test命令的test.sh:

#!/bin/bash
cd /home/www/tests/npm-bug
npm run test

Finally we have package.json with the following command: 最后,我们使用以下命令获取package.json:

"scripts": {
  "test": "echo \"Hello World!\""
}

The result of npm run test looks like: npm run test的结果如下所示:

> npm-bug@1.0.0 test /home/www/tests/npm-bug
> echo "Hello World!"

Hello World!

Notice the last row - that's the result of the echo command in package.json. 注意最后一行-这是package.json中echo命令的结果。 The same result appears in the case of ./test.sh command. ./test.sh命令的情况下, ./test.sh出现相同的结果。 And the same we get in test.log after running php test.php command. 与运行php test.php命令后在test.log获得的结果相同。

But when I trigger test.php from http, I get only 但是当我从http触发test.php时,我只能得到

> npm-bug@1.0.0 test /home/www/tests/npm-bug
> echo "Hello World!"

Notice, that the last string is absent, npm command did not run. 请注意,缺少最后一个字符串,因此npm命令未运行。

Also I have this location rule in my nginx: 我的nginx中也有这个位置规则:

location = /npm-test {
  root /home/www/tests/npm-bug;
  rewrite ^(.*)$ /test.php break;
  include snippets/fastcgi-php.conf;
  fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}

This all seems to be very standart, nginx and php7.0-fpm are right out of the box, the php config is just the same for cli and for fpm. 这一切似乎都是非常标准的,nginx和php7.0-fpm都是开箱即用的,cli和fpm的php配置都是一样的。 Checked on two servers with different environment. 在具有不同环境的两台服务器上检查。

It was a interesting issue to solve. 这是一个有趣的问题。 So the thing is that when you use 所以事情是当你使用

  exec("/home/www/tests/npm-bug/test.sh > /home/www/tests/npm-bug/test.log");

It mask all errors. 它掩盖了所有错误。 You should actually do it like 你实际上应该像

  exec("/home/www/tests/npm-bug/test.sh > /home/www/tests/npm-bug/test.log 2>&1");

And now you will see a big error 现在您将看到一个大错误

npm ERR! Linux 4.4.0-66-generic
npm ERR! argv "/usr/bin/nodejs" "/usr/bin/npm" "run" "test"
npm ERR! node v4.2.6
npm ERR! npm  v3.5.2
npm ERR! file sh
npm ERR! path sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn sh
npm ERR! npm-bug@1.0.0 test: `sh -c 'echo "Hello World!"'`
npm ERR! spawn sh ENOENT
npm ERR!
npm ERR! Failed at the npm-bug@1.0.0 test script 'sh -c 'echo "Hello World!"''.
npm ERR! Make sure you have the latest version of node.js and npm installed.

It took me sometime to figure out why. 我花了一些时间才弄清楚原因。 So I added the env statement to the shell file 所以我在外壳文件中添加了env语句

USER=vagrant
PWD=/var/www/html/npm-bug
SHLVL=1
HOME=/home/vagrant
OLDPWD=/home/vagrant/nginx/html/npm-bug
_=/usr/bin/env

As you can see there in no PATH variable defined. 如您所见,这里没有定义PATH变量。 This was the issue. 这就是问题所在。 I added the path variable in the shell script 我在shell脚本中添加了path变量

export PATH=......

I took the path that my terminal showed on echo $PATH , and now it works from fpm also 我采用了终端在echo $PATH上显示的echo $PATH ,现在它也可以从fpm运行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM