2

I can run about any binary but not that composer.phar using PHP's exec or shell_exec.

In the same folder I have composer and another php executable with same permissions:

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 contains:

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

I then have this script:

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

Here is what I get:

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)

Why do I get that string 'sh: 1: : Permission denied' error? I tried executing in PHP with /usr/bin/env php composer.php /usr/bin/php composer.php, I get the same error.

Laurent
  • 1,048
  • 11
  • 23
  • 2
    Possible duplicate of [Run composer with a PHP script in browser](https://stackoverflow.com/questions/17219436/run-composer-with-a-php-script-in-browser) – BadHorsie Aug 22 '17 at 11:56
  • I just saw your note about the permissions, so it's not that. I think you have to unpack the phar. See above link. – BadHorsie Aug 22 '17 at 11:57
  • It isn't a duplicate. Running composer programmatically from a PHP script could be a workaround but not a solution to that weird issue. – Laurent Aug 22 '17 at 21:48

2 Answers2

7

I solved it by disabling the xdebug extension.

From the doc:

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

So I guess that this "PHP restart" is an issue when calling the binary/phar from PHP.

It is be possible to use the environment variable COMPOSER_ALLOW_XDEBUG to get it working with xdebug, also disbaling a few xdebug options that could alter performances:

<?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');
Laurent
  • 1,048
  • 11
  • 23
0

I think it will work with system('php /usr/local/bin/composer install -d /...'), because if you just run composer directly in a shell it will work by reading the shebang at the beginning of the file that says it should be executed by php, but using system() you don't have a shell so you need to specify this yourself.

gunr2171
  • 16,104
  • 25
  • 61
  • 88
  • 1
    I get the same `sh: 1: : Permission denied error` using system. The composer you have in /usr/local/bin/composer is the same composer.phar, but without the extension. – Laurent Aug 22 '17 at 10:55