0

I want to execute a git command in my php script. And this is my script demo.php.

<?php
    exec("ssh -v > log1.txt 2>&1");
    exec('git clone git@github.com:xxx/xxx.git >log.txt 2> &1',
    $out,$ret);
?>

When I execute this:

php demo.php

It clones the target project. However, when I execute it through fpm by typing the url in the browser, it sends the request to nginx, then nginx transfer this request to fpm.

 type url: localhost:port/demo.php

Now it comes into trouble and the output is as below:

log1.txt

usage: ssh [-1246AaCfGgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
       [-D [bind_address:]port] [-E log_file] [-e escape_char]
       [-F configfile] [-I pkcs11] [-i identity_file]
       [-J [user@]host[:port]] [-L address] [-l login_name] [-m mac_spec]
       [-O ctl_cmd] [-o option] [-p port] [-Q query_option] [-R address]
       [-S ctl_path] [-W host:port] [-w local_tun[:remote_tun]]
       [user@]hostname [command]

It seemed that this script executed by fpm and ssh has already been installed. However log.txt is as below:

 Cloning into '/home/geek/xxx/phpStudy/idl'...
 error: cannot run ssh: No such file or directory
 fatal: unable to fork

It seemed that it can't find the ssh command when executing git clone from this file.


I can do it with cli, and I can execute ssh command with fpm, but I cannot run ssh with git clone and fpm. This question has tortured me for serveral days. I will appreciate any advice.

Frederik.L
  • 5,522
  • 2
  • 29
  • 41
nail fei
  • 2,179
  • 3
  • 16
  • 36
  • Possible duplicate of [why git clone do nothing in my php](https://stackoverflow.com/questions/45367105/why-git-clone-do-nothing-in-my-php) – user3942918 Jul 31 '17 at 02:44
  • If you want to clarify or add details to your question, do so by editing your original question rather than reposting it. – user3942918 Jul 31 '17 at 02:45
  • The possible duplicate @Paul linked is marked as a possible duplicate (also by @Paul) of [this question](https://stackoverflow.com/questions/12199353/how-can-i-debug-exec-problems) – Milk Jul 31 '17 at 02:51
  • I am not familiar with using git this way ( my IDE does it for me ) however, I can say that the `exec` are done independent of each other, so if one relies on the other, it wont work. It's like running each command in a separate console. But like I said I don't know if they rely on each other. P.S. the way around this is to run a bash or shell script from PHP that runs the commands. – ArtisticPhoenix Jul 31 '17 at 02:53
  • @Milk Yes, that's because that question lacked any of the debugging the latter question tells you how to do. If the poster here edits it to include these results I'll remove that. – user3942918 Jul 31 '17 at 02:53
  • Another thing to note, although it's probably not the issue, is that the CLI may use a different `php.ini` file. I had this issue when BCMATH extension was not enabled in the `php-cli.ini` file before. – ArtisticPhoenix Jul 31 '17 at 02:57
  • @Paul Sure, but no reason to send them on a click journey. Just link them to the useful answer in the first instance. As an aside, the intermediate answer is by the same user. – Milk Jul 31 '17 at 02:59

1 Answers1

2

I have solved this trouble. The solution as below and hope help others who encounter this trouble.


The fpm can listen several ports and there is a pool configuration file about every port. In this configuration pool there is a directive:

 ;env[PATH] = /usr/local/bin:/usr/bin:/bin 

So the environment variable PATH of fpm maybe different with os, that is to say some command we can use in shell may not be found in fpm.

Finally, I solve this question through amend env[PATH]

nail fei
  • 2,179
  • 3
  • 16
  • 36