I have Lubuntu 20 running with latest Apache2 and PHP8.1.
I realized using "shell_exec" it only works for commands like "dir" or "echo" or "touch". But i need to run a command i installed into /usr/local/bin. Calling that one simply makes shell_exec return nothing.
I am also appending "2>&1" at the end of the command, but no luck.
Folders and files are owned by the same user i am running apache with, chown and chmod is fine. It seems it is blocking such commands, while systems commands are working from PHP.
PHP does not block any of the functions. Any idea why it is failing to run the command?
Update:
Non-working examples
echo shell_exec('/usr/local/bin/sfdx --help 2>&1');
echo shell_exec('/sfdx --help 2>&1');
echo passthru('/usr/local/bin/sfdx --help 2>&1');
Running all these on CLI on my Lubuntu works fine even with "php -r" command using the CLI mode of php works. Made a diff of apache php.ini and the cli one, only difference is my display_error and memory_limit setting.
Also tried with popen with no luck, it simply returns nothing.
$cmd = "/usr/local/bin/sfdx --help";
$handle = popen($cmd . " 2>&1", 'r');
$read = fread($handle, 2096);
pclose($handle);
Working examples
echo shell_exec('dir 2>&1');
echo shell_exec('touch test.log 2>&1');
echo, try usingvar_dumpto see what kind of "nothing" it really it. Can you confirm error reporting is cranked all the way up on your web version? When you run the commands from the CLI, PHP is run as your user account, however when you run it through the web version, usually a different user is used. I also like to steer people towardsproc_open, which, although more verbose, it allows you to inspectstdoutandstderrindependently which can sometimes help.proc_open, so I unfortunately can't answer why the redirection stuff doesn't work. According to the docs forshell_exec, however, "This function can returnnullboth when an error occurs or the program produces no output. It is not possible to detect execution failures using this function.exec()should be used when access to the program exit code is required. Have you tried that?