Detecting in a script whether apache2 root is /var/www or /var/www/html

Ben Crowell asked:

Apache2 recently changed the default root directory from /var/www to /var/www/html, which breaks the scripts I distribute for installing some software I wrote. Is there a reliable way of detecting from a script which it is? I would prefer a reasonably portable solution, say one that would run on Linux, BSD, and MacOS.

I guess I could check for the existence of the html subdirectory, but that doesn’t seem very bulletproof.

I also thought of this:

source /etc/apache2/envvars ; apache2 -S

This generates output that actually seems wrong:

Main DocumentRoot: "/var/www"

In fact, the root directory on this machine is /var/www/html.

My answer:


If your script is called via CGI or FastCGI, it should find the document root in the DOCUMENT_ROOT environment variable.


View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.