http://www.perlmonks.org?node_id=800585

sgifford has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I'm looking at using a Perl module to dynamically set up some virtual hosts at run-time.

mod_perl is what I'm looking at, of course, and PerlMapToStorageHandler seems like just about what I want. I can look up the user information in the database based on $r->hostname(), find their home directory, then call $r->filename($new_path) to set up the mapping. If I can get this to work, I'll cache the results, so I won't have to hit the database very often.

But, that method doesn't seem to work like I expect it to. Just to test, I wrote this sub, which should cause all requests to return test.txt:

sub handler { my $r = shift; my $newfn = "/var/www/test.txt"; warn "In PerlMapToStorageHandler Handler, hostname " . $r->hostnam +e() . ", uri " . $r->uri() . ", filename " . $r->filename() . ", new filename $newfn\n"; $r->filename($newfn); return Apache2::Const::OK; }
Instead, I get this in the error log:
In PerlMapToStorageHandler Handler, hostname www.example.com, uri /, f +ilename /var/www/, new filename /var/www/test.txt [Sun Oct 11 22:12:38 2009] [error] [client 174.129.128.58] File does n +ot exist: /var/www/test.txt
and a 404 error in the browser.

The background is I'm working on a clustered environment with virtual hosts that are added from a Web interface. I'd like to be able to set up new virtual hosts by adding to the database and having Apache pick them up automatically, without the Web interface script having to arrange for the cluster of Web servers to be restarted. If somebody has a better idea for solving this problem, please let me know.

Thanks!

Updates: A few things other Monks have asked below that I should have included in the original.

Replies are listed 'Best First'.
Re: mod_perl PerlMapToStorageHandler
by jethro (Monsignor) on Oct 12, 2009 at 09:57 UTC
    The error message from apache means it was looking for /var/www/test.txt, just what you wanted.

    That it ultimately sent a 404 means it didn't find or couldn't access a file at /var/www/test.txt

    To change that you either would have to put in a PerlResponseHandler too that generates a response or make sure that a file is accessible at /var/www/test.txt. Anonymous Monk already listed a few possibilities why it didn't work, another would be that apache doesn't have the rights to read /var/www/test.txt.

    Disclaimer: I'm no expert of mod_perl, this answer is just theoretical reasoning not backed by actual experience

      Thanks, I should have included that information in the original post. I have added it.

      Any other thoughts are appreciated.

      One thing that's weird is when I strace Apache, I can't see it trying to open anything before returning the error.

Re: mod_perl PerlMapToStorageHandler
by Anonymous Monk on Oct 12, 2009 at 09:13 UTC
    Here is some ideas
    • your apache2/mod_perl2 versions are too old and buggy
    • file does not exist
    • your program is in chroot jail and its as if file does not exist
    Hope that helps :)