0

I have php program, that executes a test program with a parameter. That returns json data. I tested with print_r what I get and use a foreach loop to get each row.

$jsondata = shell_exec("/bin/test node");
$data = json_decode($jsondata, true);
...
print_r($data);
/* output:
Array
(
[nodes] => Array
( 
...
*/
foreach ($data['nodes'] as $node) {
...

The result should be a table in html. The command line prints out that table as expected. If I try to execute that script via web I get an error:

The error in the log file:

2019/04/02 18:41:33 [error] 1482#1482: *14607 FastCGI sent in stderr: "PHP message: PHP Warning: Invalid argument supplied for foreach() in /media/... xxx.com/test.php on line 85" while reading response header from upstream, client: 192.168.178.1, server: xxx.com, request: "GET /Lightning/listnodes.php HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.2-fpm.sock:", host: "xxx.com"

If I pipe the test program to 1.html and display it in a web browser, it works.

3
  • Maybe a permmissions issue? Commented Apr 2, 2019 at 10:50
  • Make sure the user that runs the web server (like www-root or similar) has permission to execute that file. Commented Apr 2, 2019 at 10:50
  • See also: How can I debug exec() problems? Commented Apr 2, 2019 at 11:16

1 Answer 1

1

shell_exec is executed in the context of the web-server if the script is invoked through that web-server (i.e. via web request). You probably have the permission to execute /bin/test node when logged in via SSH to the server, but the web-server software (e.g. Apache or nginx) maybe does not have the permission to run /bin/test node. Check the permissions for the command.

See also: https://www.php.net/manual/en/function.shell-exec.php#37971

Sign up to request clarification or add additional context in comments.

5 Comments

I believe, if it would be a permission problem, then the log file would say so. However, here it says line 85 (foreach...) has invalid parameter.
@RonaldWiplinger Yes, that is exactly why that came to my mind: There are not enough user rights/permission to gather the $jsondata via a shell command. Thus that $jsondata is empty (probably NULL) which leads to the subsequent error of foreach not being able to handle an empty variable. In PHP the messages in the error log are often subsequent errors of programming mistakes. In this case, not checking if there was any data returned by shell_exec("/bin/test node").
How can I test if the permission is sufficient? I tried "sudo su" to become root and then "su www-data" (as the user the web server is running). That results in "This account is currently not available."
/bin/test has the permission -rwxr-xr-x 1 root root
I came one step further: I found that the /bin/test program is just a short form for a "docker exec ..." and nginx runs with the user www-data. I added www-data to the docker group (sudo usermod -a -G docker www-data) and restarted nginx. It is still the same. I doubt if restart is equivalent to log-out/log-in and therefore it has no effect (yet).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.