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

I am trying to configure virtual hosts dynamically in mod_perl2.

I have a startup file which sets up an 'application' based on my own framework. Part of that inflation creates a number of virtual hosts.

I have been trying the following:

------------------------- In httpd.conf: PerlConfigRequire /path/startup.pl ------------------------- In startup.pl: .... my $config = <<VHOST <VirtualHost 127.0.0.1> # Config </VirtualHost> VHOST Apache2::Server->server->add_config([split /\n/ $config]); -------------------------

When I tried with PerlRequire or PerlPostConfigRequire, adding the virtual host would give me a segfault.

With PerlConfigRequire, it runs fine, but if I do:

  @vhosts = Apache2::Directive::conftree->lookup("VirtualHost")

it doesn't find any virtual hosts.

I want to keep the line in httpd.conf as simple as possible. How should I go about doing this?

I could use PerlSections, but how?

<Perl> my $config = do 'startup.pl'; #parse config into @vhosts foreach my $vhost (@vhosts) { $VirtualHost{$vhost}=>$config->{$vhost} } </Perl>

Or should I use something like:

<Perl handler="startup.pl"> </Perl>

Or:

PerlOpenLogsHandler startup.pl

Help appreciated

Replies are listed 'Best First'.
Re: Configuring virtual hosts dynamically with mod_perl
by Fletch (Bishop) on Aug 14, 2006 at 13:48 UTC

    It's been a while since I've mucked with mod_perl, but I want to say that the magic variables like %VirtualHost and @Alias only work within <Perl> sections, not code read in with a PerlRequire directive or the like.

    If you want to keep the configuration segregated from the rest of your httpd.conf you probably could stick the <Perl> section into another file (say perl_vhost.conf) and then use the Apache Include directive to read in that other conf file.

    You might also check the mod_perl mailing list archives and/or run this question by them.

Re: Configuring virtual hosts dynamically with mod_perl
by clinton (Priest) on Aug 14, 2006 at 16:08 UTC

    I figured out what I was doing wrong, so I thought I'd document it, because it took me so long to figure out.

    According to http://httpd.apache.org/docs/2.2/mod/core.html#namevirtualhost, "The NameVirtualHost directive is a required directive if you want to configure name-based virtual hosts."

    I am specifying IP-based virtual hosts and so thought that a NameVirtualHost directive was not required. But it didn't work.

    When I added the NameVirtualHost directive, it all worked swimmingly.

    So, to dynamically configure virtual hosts, you can do as follows:

    In httpd.conf: ---------------------- PerlConfigRequire "conf/startup.pl" ---------------------- In conf/startup.pl: ---------------------- use Apache2::ServerUtil(); my $config = ''; foreach my $site (@sites) { $config.= = <<CONFIG; NameVirtualHost $site->{ip}:80 <VirtualHost $site->{ip}:80> ServerName $site->{name} # Other config details </VirtualHost> CONFIG my $server = Apache2::ServerUtil->server; $server->add_config([split /\n/, config]); ----------------------

    hope this saves somebody else some pain