How do I give cmd permissions to apache? - php

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.

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.

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"

Execute php using exec() not working

I am trying to execute a shell command from php command line in following way
php -r '$test = exec("aws s3 cp s3://test/my-container/testing.txt /var/www/files-test"); echo $test;'
this works and as result files get downloaded from s3 to mentioned destination /var/www/files-test
But when I execute same command from web app it does not work. Code is
$test = exec("aws s3 cp s3://test/my-container/testing.txt /var/www/files-test");
print_r($test);
it does not work and as output I got
Completed 1 part(s) with ... file(s) remaining
I have ensured apache user has required privileges. What can be missing here?
I got this resolved, web-server I am using was nginix and web app some how was running on apache user, I gave permissions for nginix user but not for apache user!
so fix was to get back the app to run on nginix user.

Unable to run bash commands in php script

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

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

Categories