shell_exec('git push origin master') won't work - php

I want to push a folder with data to an empty repository in atlassian Git stash. The repository is created via the rest api. The remote origin is set. When I enter
$ git push origin master
in the shell it works all fine. When I use the PHP interactive shell and enter
exec('git push origin master');
it also works. But when the code is called in my PHP file nothing happens. Of course the code is the same. I am in the right working directory (I change it with chdir() before the shell_exec). No errors are reported (even when error_reporting is set to E_ALL) and when i set the output argument in the exec() I get an empty array.
Can anyone help?

When you execute code interactively, or commands via the shell, you're executing under your user account and your shell environment.
I would assume that the reason this isn't working is that you're executing the PHP script as another user (and possibly lacking your ssh/git environment config).
More information is needed to better answer your question, but you may want to start by verifying the user you're executing your PHP file as is also able to perform a git push origin master by doing something like:
sudo su -s /bin/bash -c "cd /my/git/repo/dir; git push origin master" MYPHPEXECUTEUSER

I found out that i have to add '2>&1' to the end of a shell command to get an error output when using shell_exec(). It returned "fatal: could not read Password for"url": device not configured". i just had to add the password to the url in a http basic auth fashion. Now it all works fine. Thanks for the answers :)

Related

webhook exec git pull not working when called from the browser

i searched on the internet for hours to find a good solution but all the topics that allready exits dont help for me
i try to build a webhook to update my website by doing a git pull that is called afther a git hook when commits pushed to my live branche. But it isn't working when i try to call git pull by requesting my php script in the browser.
apache is running as www-data, all the rights a set up well.
the php script is running good when i try to run it as www data user with su - www-data and than php webHook.php
but when i call it in the browser, i get no response. I guess apache is blocking
this is the command i use in the php script:
echo shell_exec('cd /home/my-site/public_html && git pull');
the result is empty and the git repository is not pulled at all.
but if i try this:
echo shell_exec('cd /home/my-site/public_html && git');
i got this result
usage: git [--version] [--exec-path[=]] [--html-path] [--man-path] [--info-path] [-p|--p..........
so everything is working, except the git pull command.
the respository is saved on another server with atlassian stash. and i saved the password with git config --global credential.helper "cache --timeout=360000000"

PHP exec() git fetch failing with return value 255

I have modified a version of this github project https://github.com/lkwdwrd/git-deploy [I modified it as for some reason JSON wasnt being parsed on my server (Mediatemple Grid Server) & I also added custom logging] The script autodeploys github repos using git hooks.
The script was working fine for a while but recently stopped working.
I've isolated the problem to the git fetch command. This the exact code I'm using:
exec( 'git fetch ' . $this->_remote . ' ' . $this->_branch, $fetch_output, $fetch_return_var );
The $fetch_output array is blank and the $fetch_return_var is 255. So I believe that the 255 really means -1, which is an error with the git command.
However I am able to successfully execute this command when SSH'd into the server and running as the same user with the exact same string as is being build as the 1st argument of the exec() function.
So now I'm lost.
Is there an effective difference between running commands manually via SSH and using exec()?
Is there any way I can get the actual git fetch error that is being returned to exec()?
Could this be some kind of server configuration that has been changed by my host and that I should contact them for?
thanks in advance
UPDATE:
Thanks to #Matthias Huttar I changed my exec() to a proc_open() call that allowed me to see the output of STDERR [not sure why it was not logging thru exec() but I think it is because it was a subprocess of git fetch]. This output is:
error: cannot fork() for git-remote-https: Resource temporarily unavailable
So I think there is an issue with my server environment. However I'm still not certain.
if there is an error in you git fetch then the output will very likely not be written to stdout but to stderr. exec will only give you the output of stdout and ignore stderr.
exec( 'git fetch ' . $this->_remote . ' ' . $this->_branch.' 2>&1', $fetch_output, $fetch_return_var );
will very likely show your error. my suspicion is that the git process you invoke from php runs as a different user (e.g. you apache server's user) and therefore does nit have access to you ssh key (or username/password). If that is the case your error msg will be something with "permission denied". the solution would be o generate a new default ssh key for that user and grant this key access in github ("deploy key" is how github calls this)

Git pull “permission denied” when using shell_exec in PHP

I'm trying to make a hook on bitbucket, that executes a php file, and this file executes the pull command:
shell_exec('/usr/local/cpanel/3rdparty/bin/git pull');
The pull command works fine on the SSH console, but the PHP returns the error:
Permission denied (publickey). fatal: Could not read from remote
repository.
Please make sure you have the correct access rights and the repository
exists.
The command --version shows the path to git is right, whoiami returns the same user on both, so I don't know if it is a permission issue.
What can be going wrong?
Edit: An additional issue: the alias I added for git don't work on PHP, only the full path as above. Via terminal it works just fine. Maybe it's the same reason why the key don't work in php.
Edit 2: $PATH is different on both.
When you run this command within a PHP script you are not running the command as yourself:
shell_exec('/usr/local/cpanel/3rdparty/bin/git pull');
The reason it works from the terminal console is you run the command as yourself from the console. But on a web server, you are not the user running the command. Remember: When you run PHP on a web server, it is a an Apache module. Meaning the web server user—which could be www-data, root or even apache on some systems—is running the PHP script which then runs the shell_exec command.
So it would never work as you have it setup. Perhaps you can kludge something together that would allow a key-pair to be used by the web server for these purposes, but that seems like a security risk waiting to happen.

Cannot run "git clone" from php on a Minimac

I have a problem executing a git clone on a Minimac 10.8.5.
If I execute the command from a shell, it works.
If I execute the same command from a php file (through a button press on a web page), I get
"error: ssl peer certificate or ssh remote key was not ok while accessing...".
Already executed: git config --global http.sslVerify = false
Already changed the User and Group of the file /etc/apache2/httpd.conf
Tried to execute it with a git clone --verbose
The only error I see is the above one, no more informations.
Where or how can I find a more detailed log ?
What could be the cause ?
This look that the remote requires an authentication either through a ssh key like heroku an github or with an password via https.
In both cases you will need this, you can configure your system first and then try to run the git via php.
I believe that you are doing some type of deploy script right? If so you may want consider use git pull on the after the first deploy.
Try this
env GIT_SSL_NO_VERIFY=true git pull origin master
Edit: solved in another way.
The only way I've found to create a shell with the correct user, is starting the process via ssh. So I do the following and all works as expected.
<?php
$cmd = "ssh user#localhost <cmd to execute>";
shell_exec($cmd);
?>

Can I rip DVDs from a local web app with HandBrake?

I'm building a PHP web application that will run on my machine, and one of its purposes is to call HandBrakeCLI with an exec() or similar call. If I run the command
HandBrakeCLI -i path_to_dvd_drive -o output_file --preset preset_name
from a shell, it works fine. However, when I put the exact same command in an exec() php function (or similar), it doesn't work, and doesn't return anything, aside from a return status of 0. No errors, nothing else.
Is this just a simple permissions issue that I'm not seeing, due to the lack of errors being spit out? Or am I missing something else?
For debugging, try running the command from the console but as the user PHP runs as. Here are some pointers how to find out which user that is.
Then use sudo to run the command as the Apache user. You will probably get more helpful information from that.
try to exec your script using absolute path (type which HandBrakeCLI in terminal to find it) and append 2>&1 to the end of command, like:
exec('HandBrakeCLI -i path_to_dvd_drive -o output_file --preset preset_name 2>&1')
this way if command outputs anything to stderr you will see it.
is handbrake in the path of whatever shell PHP is invoking when it does the exec()? Does whatever account PHP/webserver is running under have access to the directory where handbrak.exe is, and have permission to execute handbrake.exe? Does the webserver account have permissions to access the dvd drive, etc...
Many things to check, and without better diagnostic information, this is about the best you'll be able to get here.

Categories