为什么我不能 运行 来自 PHP 的作曲家脚本使用 exec,shell_exec,系统?

Why can't I run the composer script from PHP using exec, shell_exec, system?

我可以 运行 关于任何二进制文件,但不能 composer.phar 使用 PHP 的 execshell_exec.

在同一个文件夹中,我有 composer 和另一个具有相同权限的 php 可执行文件:

ls -lh
total 1,8M
-rwxr-xr-x  1 me me 1,8M août  22 20:48 composer.phar
-rwxr-xr-x  1 me me   39 août  22 21:05 test.php

Test.php 包含:

#!/usr/bin/env php
<?php
print 'hello';

然后我有这个脚本:

<?php
print $cmd  = "composer.phar --version 2>&1" ;
print "<br>";

$return  = exec( $cmd );
var_dump($return);

print "<br><br>";

print $cmd  = "test.php 2>&1";
print "<br>";

$return  = shell_exec( $cmd );
var_dump($return);

这是我得到的:

composer.phar --version 2>&1
[...]Process.php:81:string 'sh: 1: : Permission denied' (length=26)

test.php 2>&1
[...]Process.php:88:string 'hello' (length=5)

为什么我会收到 string 'sh: 1: : Permission denied' 错误?我尝试在 PHP 中使用 /usr/bin/env php composer.php /usr/bin/php composer.php 执行,我得到了同样的错误。

我认为它可以与 system('php /usr/local/bin/composer install -d /...') 一起使用,因为如果您只是 运行 直接在 shell 中作曲,它将通过读取文件开头的 shebang 来工作它应该由 php 执行,但是使用 system() 你没有 shell 所以你需要自己指定它。

我通过禁用 xdebug 扩展解决了这个问题。

来自文档:

To improve performance when the xdebug extension is enabled, Composer automatically restarts PHP without it.

所以我猜这个 "PHP restart" 在从 PHP 调用 binary/phar 时是个问题。

可以使用环境变量 COMPOSER_ALLOW_XDEBUG 使其与 xdebug 一起工作,同时禁用一些可能改变性能的 xdebug 选项:

<?php
$result = shell_exec('COMPOSER_ALLOW_XDEBUG=1 /usr/bin/env php -d xdebug.remote_enable=0 -d xdebug.profiler_enable=0 -d xdebug.default_enable=0 composer.phar --version 2>&1');