mod_perl logo
perl icon







no previous pagepage up: What is mod_perl?no next page


Configure Apache with Perl Example











Writing Apache Modules with Perl and C

Writing Apache Modules with Perl and C

By Lincoln Stein, Doug MacEachern
Embedding Perl in HTML with Mason

Embedding Perl in HTML with Mason

By Dave Rolsky, Ken Williams
Practical mod_perl

Practical mod_perl

By Stas Bekman, Eric Cholet


Table of Contents

Configure virtual hosts

With mod_perl, Perl code can be embedded directly in the Apache configuration file. Perl in httpd.conf is commonly used to dynamically configure Apache, but anything from URL translation to content generation can be accomplished directly in the configuation file within <Perl> sections.

This example reads configuration settings from a text file and configures Apache's virtual hosts.

The httpd.conf setup:

  NameVirtualHost 192.168.0.1:80
  <Perl>
      my $config = "/etc/apache/vhosts.txt";
      open HOSTS, $config or die "Failed to open $config: $!";
  
      while (<HOSTS>) {
          my %config;
          my @params = qw/ServerName DocumentRoot ErrorLog TransferLog ServerAdmin/;
          @config{ @params } = split /\t/;
          $config{ Directory }{ $config{DocumentRoot} } = { Allow => 'from all' };
  
          push @{ $VirtualHost{'192.168.0.1:80'} }, \%config;
      }
      close HOSTS;
  
  </Perl>

See The Guide for other examples of configuring Apache with mod_perl.

« back





TOP
no previous pagepage up: What is mod_perl?no next page