How to debug shell_exec issues
OK, here we have a problem: some thing works in terminal and does't work in php's shell_exec (or exec, spawn, whatever). Let's think: what is the difference between You and php? Here are three:
PHP is executed from HTTP server rules
Indeed, Your terminal is running from
yourname user and php is executed from
www-data. So, first idea is to open terminal from
www-data user and try same command. So....
/etc/passwd file, find line with
www-data user and change it's login shell (last one) from
/bin/false (or whatever) to
- Try php -v or whatever You can't execute from php. If it does not work -- You'll see nice logs and will be able to debug problem.
- Do not forget to fix back
/etc/passwd file while You're done
PHP is executed from PHP.
PHP is paranoid enogh and there are a lot of options in apache/nginx and php.ini configuration which may break Your attempt.
This is a bit more complicated to debug. There are two options here:
Enable logs in browser and see the problem. Edit Your
php.ini, turn on
display_errors and all other flags to see errors in browser. They You can read and debug them.
www-data's shell (see first section) and execute something like
echo '<?php shell_exec("php -v"); ?>' | php
which will execute same php code in console and You'll be able to see errors and debug.
PHP is executed from SELinux/apparmor
Selinux and apparmor are security things which forbid applications to perform specific actions (for example, spawning other applications or some specific other applications). Maybe it's enabled on Your server.
To check -- disable selinux/apparmor and check if problem exists.
To fix -- read appropriate manual and fix write permissive rules for Your case.