1

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.

2 Answers 2

2

This will depend on how many virtual hosts you have setup, but you could certainly guess by using code such as this:

 root=$(rgrep DocumentRoot /etc/apache2/ | awk '{print $NF}' | head -n 1)
 echo $root

This will find the DocumentRoot setting specified beneath /etc/apache2, and print the first one.

You could easily use this approach to see if you find any mention of /var/www/html and use it if so.

2
  • 1
    I see. I hadn't thought about the issue with virtual hosts. That makes it seem like a totally bulletproof solution isn't going to exist. Jul 10, 2014 at 19:41
  • 1
    Indeed unless there is only a single vhost - which you could determine via grep,etc - your best course of action is to add some rewrite/alias rules to cover both cases, or ask the user.
    – user9565
    Jul 10, 2014 at 20:02
1

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

2
  • 1
    The script isn't a CGI script, it's a script that installs the CGI, along with some data files in the document root. Is there some way to access DOCUMENT_ROOT from a script that isn't CGI? Jul 10, 2014 at 19:22
  • 2
    In that case, you probably should ask the user, rather than trying to guess, or parse config files (as you may parse the wrong one). Jul 10, 2014 at 19:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .