Ira Baxter Ira Baxter - 1 month ago 14
Linux Question

How to configure PHP server variable $_SERVER['DOCUMENT_ROOT']?

[Asked this at SU; suggestion there was to repost at SO]

We have PHP programs that use $_SERVER['DOCUMENT_ROOT'] as a path to a place where we can place a private directory (using a unique name) in a safe, known place on server. This directory holds data summaries collected from multiple perhaps thousands of served PHP pages; this summary has no impact on the main functionality of the pages served, and eventually can be ignored or deleted. While it exists, a PHP-based tool can inspect the summary data and provide a report.

This has worked well on a variety of PHP servers, and appears to be "automatically" (?) set according to the PHP Manual documentation:


'DOCUMENT_ROOT'

The document root directory under which the current script
is executing, as defined in the server's configuration file.



We installed several different PHP servers over the years, and they all seem to come with this pre-configured to a reasonable path without any explicit configuration effort on our part.

We have some clients using our software that complain that this path seems to be set to null, which seems inconsistent with the above definition. (Does null signify some special path?) (One of these clients claims they have (php -v) "PHP 5.6.16-3+deb.sury.org~trusty+1 (cli) ... Zend Engine v2.6.0, ... with Zend OPcache v7.0.6-dev, ..." and (uname -or) "3.19.0-25-generic GNU/Linux" [??Generic GNU/Linux??]

Pardon my ignorance, but is DOCUMENT_ROOT a configurable item? Where do I find the server "configuration file" for this? Is it the same place for all different PHP server implementations? Can I/should I change that configuration files to set this explicitly if it is not set?

If not, is there another way to provide a path to a safe long-term "temporary" local directory?

Answer

$_SERVER['DOCUMENT_ROOT'] is found in your apache httpd.conf and it can be configured. It's the directory out of which you will serve your documents. By default, all web requests are taken from this directory so it should not be null.

This link gives you quick run-down as to why documentroot is in place: http://httpd.apache.org/docs/2.4/urlmapping.html#documentroot.

I personally don’t see an issue using $_SERVER['DOCUMENT_ROOT'] for servers which I have full control, but I would be wary using this path reference on shared hosting solution’s due to aliases in the config. In this instance, I would just set the $_SERVER['DOCUMENT_ROOT'] manually.

<?php 

echo $_SERVER['DOCUMENT_ROOT'];//Real Path

$_SERVER['DOCUMENT_ROOT'] = "/new/path/www";

echo $_SERVER['DOCUMENT_ROOT'];//New Path

?>
Comments