Unable to run bash commands in php script - php

I'm trying to use a script to automate some syncing stuff with the website's corresponding github.
The offending code is
<html>
<head><title>github_sync script</title></head>
<body><p>Github sync script working... Although you're probably a github webhook anyways</p><body>
<?php
`mkdir test_dir`;
`cd github_sync/`;
`git clone https://github.com/frczyxw/my-website.git`;
`cd my-website/`;
`rm sixdegrees/`;
`cp -R * ../../`;
?>
</html>
The script is placed in the public_html/beta/directory. I can log into and navigate through the ftp and launch the script by clicking on it fine (the body text displays), but upon rechecking the ftp directory, I find that public_html/beta/test_dir has not appeared, nor does /public_html/beta/github_sync/ have anything in it (I created it manually previous to running the script).
The server should be running php5.3, and I am hosting the website vie Bluehost

OK, so I've solved the problem-
The server was configured correctly- however it turns out that to run all of these commands in sequence and in the same context (i.e. cd into github_sync make a git clone in that folder) the commands need to be executed all at once, like
`cd github_sync/;git clone wsgzsgg; rm -rf...`
Thnaks to everyone who tried to help

Related

Automation git pull doesn't work on server with cron and url

I want to pulling commits from git repo automatically. I have ready server where I can't use ssh and make configuration becouse I don't have permission but I remember login and password in git files.
For automation I want to use cron or place php file with script in public folder. I writed script:
<?php system('echo "Updating";'); ?>
<?php system('cd project && ls -al && pwd && git pull'); ?>
When I run it with php72-cli(it's version of php on server) script.php it's work and pull commits. When I run it in broswer by url myurl.com/script.php it showing text 'Updating' but didn't update. I made script executable by chmod + x.
I thing raeson of that is problem with wrong rights or access but I don't know how to find error and fix it. I can think wrong.

How do I give cmd permissions to apache?

I'm working on a php application that pulls github repositories to the server. Github webhooks call the php files.
I want to execute a cmd command using php. I assume I need apache permissions but I don't know how to give them.
The php code below creates an mkdir.bat and a gitclone.bat and runs them. The mkdir runs successfully, it creates an empty folder, but the gitclone doesn't create any folders or files. When I run the gitclone manually it does create folders and files.
file_put_contents("mkdir.bat", "mkdir test");
exec("mkdir.bat");
file_put_contents("gitclone.bat", "git clone https://github.com/gutyina700/WPTG.git");
exec("gitclone.bat");
I executed your code in my computer
file_put_contents("gitclone.bat", "git clone https://github.com/gutyina700/WPTG.git");
exec("gitclone.bat 2>&1",$o);
print_r($o);
the extra code is to check the output from cmd and it returned "git command not exsists"
so you only have to add a line as
<?php
putenv("PATH=C:\Program Files\Git\cmd");
file_put_contents("gitclone.bat", "git clone https://github.com/gutyina700/WPTG.git");
exec("gitclone.bat 2>&1",$o);
print_r($o);
?>
This was working properly.

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"

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.

how to run a batch file in php under xampp on windows

I'm trying to write a little php to update an svn repo on a server running xampplite under windows. (This is a development server, not a production one.)
Here's my php:
<?php
passthru("update.bat");
// I also tried exec() & putting the svn command in directly
?>
update.bat is sitting in the same folder as the php script
Here's the content of update.bat:
svn up c:\path\to\my\repo
When I run the batch file by itself, it works. When I run it via php, I get this printed to the browser:
C:\path\to\script\folder>svn up c:\path\to\my\repo
which looks good, but the project isn't updated.
Adding the username and password to the batch made the difference. Here's the new update.bat:
svn up --username <usr> --password <pwd> c:\path\to\the\repo
Try this tip on php.net/function.exec
The other option is to manually compile the php svn extension (there's no Windows DLL), but you also need the svn libraries first.

Categories