Getting value of DOCUMENT_ROOT from PHP

I'm running PHP on nginx (on a RPi-4B) that is also running Node-RED.

Is there a way to get hold of... $_SERVER['DOCUMENT_ROOT'] and use it within Node-RED ??

e.g. like executing <?php echo $_SERVER['DOCUMENT_ROOT']; ?> in a PHP script.

I can use the http request node to execute a script and return the value.
Screen Shot 08-08-21 at 09.31 PM
Screen Shot 08-08-21 at 09.31 PM 002
I was hoping there might be a way to use something simple like the 'exec' node or equivalent.

I think you could run your php script directly in a terminal through a exec node.

I've tried that but must be doing something wrong as I get a syntax error.
Any suggestions?
Screen Shot 08-09-21 at 09.16 AM
Screen Shot 08-09-21 at 09.19 AM

You cannot put raw php into the exec node - just as you wouldn't try that on the command line.

  1. put that line into a file
  2. set the exec node to run php yourFile.php

You might need to find the full path to the php command.

Hi Nick,
I've followed your instructions to create a file and call it from the 'exec' node.
It seems to run, well it doesn't give any errors - just code:0, but it doesn't return a value.
Any further suggestions??

Screen Shot 08-09-21 at 10.51 AM 001
Screen Shot 08-09-21 at 10.51 AM 003
Screen Shot 08-09-21 at 10.51 AM 002

Does it run in a terminal?

Edit: Also set the debug node to show full output

I think the main problem is that the php process running the command and the process executing scripts when you use a http request are not the same processes. Maybe it's "easier" to read the nginx configuration to determine the document_root.

And how do I do that (as I'm very new to nginx) ??

I'm not an nginx expert either but maybe you could analyse the sites-enabled directory of your nginx server? I believe the root is defined in one of those files there. On my ubuntu test machine it's /etc/nginx/sites-enabled. You can use the command whereis nginx to get the directories you are looking for.

Edit: Or you could still use the http request approach but instead of returning the absolute path, you are writing it into a file on your server (which only you know, the file I mean) and then read the content of that file from Node-RED.

Thanks for the swift response - I'll give it a try.

Edit: Your idea of reading in the nginx file and filtering out the line with keyword 'root' worked fine. Thanks.

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.