Home > Net >  PHP 8.1 on Apache: shell_exec works for shell commands but not other commands
PHP 8.1 on Apache: shell_exec works for shell commands but not other commands

Time:10-12

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

CodePudding user response:

Solution was to install "sfdx-cli" locally and not globally.

When it is installed globally (npm install -g sfdx-cli) then PHP is for whatever reason not able to run it. No errors returned even when shell_exec returns NULL, which means "error".

After installing it locally and running with "npx sfdx" everything works as expected.

  • Related