Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php shell_exec() command is not working

I am trying to run a .sh file from php. I tried doing it with shell_exec(). but its not working I refered many questions related to this in stack overflow but could not solve

my php code is(web.php)

    <?php
    echo shell_exec('/var/www/project/xxe.sh');
    echo "done";
    ?>

only done is printed. but it is working from terminal(php /var/www/project/web.php)

In xxe.sh I am calling a python file

    python vin.py

I have also changed the file permission to 777 for both .sh n .py files please help

like image 273
user3018038 Avatar asked Nov 21 '13 14:11

user3018038


4 Answers

If you say it works on the terminal and not on apache then apache's php.ini file may be disabling the use of shell_exec().

See http://www.php.net/manual/en/ini.core.php#ini.disable-functions

Your apache's php.ini file may look something like

disable_functions=exec,passthru,shell_exec,system,proc_open,popen

Remove shell_exec from this list and restart the web server, although this is a security risk and I don't recommend it.

like image 174
SamV Avatar answered Oct 26 '22 17:10

SamV


If it works well in shell, I think apache is chrooted. So php can't find /var/...

Or user of httpd user does not have permission to enter /var/...

If you are good at PHP. Open dir /var/... And readdir() and check dir exists and check file exists.

This question might help you. scanning /home/ with opendir()

like image 37
Jason Heo Avatar answered Oct 26 '22 18:10

Jason Heo


I tried everything here and nothing worked. What finally solved it for me was using the following before the shell_exec:

putenv('PATH=/usr/local/bin');
like image 4
Christopher Ditto Avatar answered Oct 26 '22 18:10

Christopher Ditto


While trying to run a script triggered by github post-receive webhook.

Here is where my project directory is located(cloned git repo):

/var/www/html/my-repo

I create a script inside the above directory called webhook.php:

<?php
#webhook.php

$cmd = shell_exec("git pull 2>&1");

#for debugging
echo $cmd;
?>

Execute the following command inside /var/www/html

sudo chown www-data:www-data -R my-repo/

Test it by going to http://www.myserver.com/my-repo/webhook.php

Add the path to your script to github webhooks.

like image 3
Jack Vial Avatar answered Oct 26 '22 17:10

Jack Vial