The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Configure Apache with Perl Example

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