4

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?

codeholic
  • 5,680
  • 3
  • 23
  • 43

1 Answers1

3

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.

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
  • Isn't there an easier solution? – codeholic Feb 16 '10 at 20:54
  • I couldn't vote up since I didn't have reputation of 15 :-) What I want to do is to chdir to DOCUMENT_ROOT, since I need to run old CGI code with ModPerl::Registry. Is PerlInitHandler alright for this purpose? – codeholic Feb 16 '10 at 22:24
  • @codeholic Sorry, forgot about the 15 points thing. Thank you for accepting my answer. – Sinan Ünür Feb 16 '10 at 22:49