How can I learn DOCUMENT_ROOT in startup.pl under mod_perl2?

document-root, mod-perl, mod-perl2, perl

Solution

See Apache2::Directive. For example, on my development system:

use Apache2::Directive ();
my $tree = Apache2::Directive::conftree();
my $vhost = $tree->lookup(VirtualHost => 'unur.localdomain:8080');

File::Slurp::write_file "C:/bzzzt.txt", [ $vhost->{DocumentRoot}, "\n" ];

created a file `C:/bzzzt.txt` with the contents `"E:/srv/unur/deploy/htdocs"` after I discovered that I had to specify my virtual hosts using

<VirtualHost unur.localdomain:8080>
...
</VirtualHost>

<VirtualHost qtau.localdomain:8080>
...
</VirtualHost>

rather than `<VirtualHost *:8080>`. Otherwise, each `<VirtualHost *:8080>` section was overwriting the previous one.

This is annoying. I would have thought each `VirtualHost` entry would have been keyed by the `ServerName` used.

As for if there is an easier way, I am afraid there isn't if you want to do this in `startup.pl`. However, I am not sure if it is necessary to do it in `startup.pl`. You can find out the document root while processing a request as well using Apache2::RequestUtil::document_root.

If you are running Registry scripts, and want to change to `DOCUMENT_ROOT`, then you should be able to add:

chdir $ENV{DOCUMENT_ROOT} 
    or die "Cannot chdir to '$ENV{DOCUMENT_ROOT}': $!";

to the script instead of having to mess around with `startup.pl` and handlers etc.

Problem

I want to learn DOCUMENT_ROOT in startup.pl, but the best I can do is to learn server_root: ``` use Apache2::ServerUtil (); $server_root = Apache2::ServerUtil::server_root(); ``` which is quite useless. I can set an environment variable with ``` SetPerlEnv DOCUMENT_ROOT /path/to/www ``` but I don't like extra configuration if possible. Is there a way to get DOCUMENT_ROOT by other means?

Original source