php shell_exec() command is not working

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()


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

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.


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.