I'm running in trouble with my new VPS. I created a file pull.php with this code in my GIT folder:
<?php
$output = shell_exec('git pull');
echo "<pre>$output</pre>";
?>
It was working fine on my shared hosting but on my VPS it's return null and no 'pull' command executed. When I change the command 'git pull' to 'git status' it's show result :
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files: (use "git add ..." to include in what will be
committed)
info.php pull.php test.txt
nothing added to commit but untracked files present (use "git add" to
track)
My folder chmod to 777 and php seems can write on it properly. 'git pull' on SSH's fine. My server : Ubuntu 14.x, Apache2, php5
I would very much appreciate any help!
Probably git pull is throwing an error but the errors are not printed by default. To fix this change this line
$output = shell_exec('git pull');
for this one:
$output = shell_exec('git pull 2>&1');
I find it weird you get no output nothing...
do you have root access?
try as root in the right folder: sudo -u apache git pull
see if you get any output then.
You should add following line to /etc/sudoers
apache2 ALL=(root) NOPASSWD: /usr/bin/git
This allows apache to execute git as root and therefore access the files
then call git command (proper executable location is preferred)
shell_exec('sudo /usr/bin/git pull /path/to/your/repo');
Related
What I am trying to do
I have a git repository on bitbucket. After pushing to the repository from my local machine I want to automatically pull the master branch to my webspace.
What I have done so far
I connected to my server using ssh, created the ssh key and registered the public key on github.
I created a .sh script which pulls the master branch using ssh - so far so god - everything works when I run the script from the command line/putty
What is the problem
I want to trigger the .sh script with a webhook on bitbucket (I can give an url). For that purpose I created a .php file in my webspace:
<?php
$output = shell_exec('./deploy.sh 2>&1');
echo $output;
my .sh script looks like this:
#!/bin/bash
git pull git#bitbucket.org:dualmeta/test.git master
As already said, running the .sh script with putty works perfectly fine. However if I enter the url to the .php file in my browser it gives me an error:
Host key verification failed.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
I already did some reserach and found that many people have the exact same problem. However in my case i do not have root/sudo access because it is a rented webspace and not my own vServer.
Is there any chance getting this to work?
You must add access www-data or apache user to your git directory.
chown -R apache:apache git_directory
or
chown -R www-data:www-data git_directory
or
chmod o+rw -R git_directory
Use this too :
git config credential.helper store
After a lot of searching, I'm about to tear my hair out on this one and solution might even be dead simple but I've just overlooked it...
I'm trying run a shell script from PHP to git add -A and commit everything in the repository when a button on a web UI is clicked.
<? php
$commitMsg = 'foo';
$output = shell_exec('/Applications/MAMP/htdocs/gitlist/bash/gitlist-commit '.$commitMsg);
#!/bin/bash
cd /var/www/html/development
sudo -H -u username git add -A
sudo -H -u username git commit -m $1
It works on my MAMP/OSX setup, but not on my Ubuntu LTS box. What might I have overlooked?
On the server, I get returned, which I'm guessing means that the git add -A command is just not working. Am I right?
It also works when running directly from the terminal, but not when running via the web UI.
On branch master
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
modified: README.md
no changes added to commit (use "git add" and/or "git commit -a")
Any help would be appreciated.
try git add .; git add -u as this will accomplish the same thing "adding all files" but is a potential workaround based on your shell setup.
I have created a makefile as below:
.PHONY: all update-repo dependency-install unit-tests file-permission
all: update-repo dependency-install unit-tests file-permission
update-repo:
git reset --hard
git pull origin master
dependency-install:
composer update
unit-tests:
vendor/bin/phpunit
file-permission:
chmod 777 application/logs
chmod 777 application/cache
chmod 777 application/models/proxies
Now trying to execute it from php with following script:
echo shell_exec("make");
Not that both files are given 777 permission.
Now I am trying to execute those make commands by executing the php script from web url. After executing start, I only get following output:
git reset --hard
HEAD is now at 758a275 test commit
git pull origin master
So, for some reason other commands aren't being executed. Does anyone have any clue why? Thanks.
This is a permissions issue. Make sure the errors are also going to stdout to find out what is going wrong:
echo shell_exec("make 2>&1");
More info: In the shell, what does " 2>&1 " mean?
I'm trying to use a post-receive hook to update a remote server. I'm using ssh and everything works great (running git pull does indeed pull). I've started with this deploy.php and the other commands, like git status, do run and output as expected, but the pull doesn't appear to do anything at all.
I've chowned the hell out of everything (the git repo, known_hosts, etc...), so my www-data user should be able to access just about anything on the sever. Is there any way I can at least find some error logging for what's going wrong?
edit: I don't see any change in the output after changing git pull to git pull --verbose as suggested by adder. The output is still:
$ whoami
www-data
$ git pull --verbose
$ git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# deploy.php
nothing added to commit but untracked files present (use "git add" to track)
Solution: the apache user needs to be setup to ssh to github. I was connecting as root then getting confused when my post-receive hook target couldn't make the same update. I was doing it as root, the script was running as www-data.
git help pull
--verbose
Pass --verbose to git-fetch and git-merge.
Haven't been able to figure this out yet.. I've seen a few answers around but none of them help.
I'm trying to use Github Webhooks to have github hit a url on my server and have the server pull down newly committed items as soon as that hits. I have a php script with the following:
<?php `git pull git#github.com:my-user/myrepo.git`; ?>
However that script when hit is run as user apache so I tried:
chown -R apache:apache .
and it still has a permission denied error.
So then I tried editing the sudoers file and changing the following:
Host_Alias LOCAL=127.0.0.1
apache LOCAL=NOPASSWD: /var/www/html/git-hook.php
and that still doesn't work.
How can this be accomplished? If I run:
sudo php git-hook.php
it works just fine so nothing is wrong with the code in the php file. I just want that to be automated.
Any ideas?
Edit:
I also forgot to mention. I even created a folder /home/apache/.ssh and copied the public key for the git pull over and same result.
Change your PHP to run git via sudo
<?php `sudo git pull git#github.com:my-user/myrepo.git`; ?>
Then change your suoders to allow git to be run by the apache user
apache ALL = NOPASSWD: /usr/bin/git
There are already Git Wrappers and librarys. Maybe you can try one of them:
https://github.com/kbjr/Git.php and/or http://www.gitphp.org/projects/gitphp/wiki
I did this for a dev site -- i wouldnt advise this for a prd site although i cant think of anything particularly dangerous about it provided the scripts dont take parameters..
I created a php script that does a git pull. In the web browser I navigate to that script and any changes pushed by deisgners etc are automatically deployed.
http://.../gitpullscript/gitpullscript.php
This works by creating a git checkout that the apache user owns. You do this by creating a directory somewhere outside the document root belongs to the apache user (www-data in this case). Then a git clone into that directory, so all the files belong to www-data. afterwards soft link the directories i want into my document root so they can be accessed ni the web browser.
www-data is not in the git group, and the repositories are setup so that everyone can read (but not write).. therefore www-data can pull but not push
in the project heirarchy I created a directory to hold the gitpull script.. I use .htaccess to password protect this dir.
<?php exec('cd /var/www-data/projects/myrepo; git pull');
mkdir /var/www-data
sudo chown www-data-www-data
su www-data
mkdir /var/www-data/projects
cd /var/www-data/projects
git clone my-repo