NAME
Apache2::PerlSections - write Apache configuration files in Perl
SSyynnooppssiiss@PerlModule = qw(Mail::Send Devel::Peek); DDeessccrriippttiioonn With "#run the server as whoever starts it
$User = getpwuid(>) || >;
$Group = getgrgid()) || );
$ServerAdmin = $User;
"..." " sections, it is possible to configure your server entirely in Perl. "" sections can contain any and as much Perl code as you wish. These sections are compiled into a special package whose symbol table modperl can then walk and grind the names and values of Perl variables/structures through the Apache core configuration gears. Block sections such as " ".." " are represented in a%Location hash, e.g.:
If an Apache directive can take two or three arguments you may push strings (the lowest number of arguments will be shifted off the @list) or use an array reference to handle any number greater than the minimum for that directive: push @Redirect, "/foo", "http://www.foo.com/"; push @Redirect, "/imdb", "http://www.imdb.com/"; push @Redirect, [qw(temp "/here" "http://www.there.com")]; $Location{"/~dougm/"} = {
AuthUserFile => '/tmp/htpasswd', AuthType => 'Basic', AuthName => 'test', DirectoryIndex => [qw(index.html index.htm)], Limit => { "GET POST" => { require => 'user dougm', } }, };Other section counterparts include %VirtualHost, %Directory and %Files.
To pass all environment variables to the children with a single configuration directive, rather than listing each one via "PassEnv" or "PerlPassEnv", a "" section could read in a file and: push @PerlPassEnv, [$key => $val];
orApache2->httpdconf("PerlPassEnv $key $val");
These are somewhat simple examples, but they should give you the basic idea. You can mix in any Perl code you desire. See eg/httpd.conf.pl and eg/perlsections.txt in the modperl distribution for more examples. Assume that you have a cluster of machines with similar configurations and only small distinctions between them: ideally you would want to maintain a single configuration file, but because the configurations aren't exactly the same (e.g. the "ServerName" directive) it's not quite that simple. "" sections come to rescue. Now you have a single configuration file and the full power of Perl to tweak the local configuration. For example to solve the problem of the "ServerName" directive you might have this " " section: For example if you want to allow personal directories on all machines except the ones whose names start with secure: $ServerName = `hostname`;
$ServerName = `hostname`;
if ($ServerName !~ /^secure/) {
$UserDir = "public.html";
} else {$UserDir = "DISABLED";
}@@PPeerrllCCoonnffiigg and $PerlConfig
This array and scalar can be used to introduce literal configuration into the apache configuration. For example: push @PerlConfig, 'Alias /foo /bar'; Or:$PerlConfig .= "Alias /foo /bar\n";
See also "$r->addconfig"
CCoonnffiigguurraattiioonn VVaarriiaabblleess There are a few variables that can be set to change the default behaviour of "" sections. $$AAppaacchhee22::::PPeerrllSSeeccttiioonnss::::SSaavvee
Each "" section is evaluated in its unique namespace, by default residing in a sub-namespace of "Apache2::ReadConfig::", therefore any
local variables will end up in that namespace. For example if a "" section happened to be in file /tmp/httpd.conf starting on line 20, the namespace: "Apache2::ReadConfig::tmp::httpdconf::line20" will be used. Now if it had: $foo = 5;
my $bar = 6;
$My::tar = 7;
The local global variable $foo becomes
$Apache2::ReadConfig::tmp::httpdconf::line20::foo, the other variable
remain where they are. By default, the namespace in which "" sections are evaluated is cleared after each block closes. In our example nuking $Apache2::ReadConfig::tmp::httpdconf::line20::foo, leaving the rest
untouched.By setting $Apache2::PerlSections::Save to a true value, the content of
those namespaces will be preserved and will be available for inspectionby "Apache2::Status" and "Apache2::PerlSections->dump" In our example
$Apache2::ReadConfig::tmp::httpdconf::line20::foo will still be
accessible from other perl code, after the "" section was parsed. PPeerrllSSeeccttiioonnss DDuummppiinngg ""AAppaacchhee22::::PPeerrllSSeeccttiioonnss->>dduummpp""
This method will dump out all the configuration variables modperl will be feeding to the apache config gears. The output is suitable to read back in via "eval".my $dump = Apache2::PerlSections->dump;
ret: $dump ( string / "undef" )
A string dump of all the Perl code encountered inblocks, suitable to be read back via "eval" For example: $Apache2::PerlSections::Save = 1;
$Listen = 8529;
$Location{"/perl"} = {
SetHandler => "perl-script",
PerlHandler => "ModPerl::Registry", Options => "ExecCGI", }; @DirectoryIndex = qw(index.htm index.html);$VirtualHost{"www.foo.com"} = {
DocumentRoot => "/tmp/docs", ErrorLog => "/dev/null", Location => { "/" => { Allowoverride => 'All', Order => 'deny,allow', Deny => 'from all', Allow => 'from foo.com', }, }, };This will print something like this: print Apache2::PerlSections->dump;
$Listen = 8529;
@DirectoryIndex = ( 'index.htm', 'index.html' );$Location{'/perl'} = (
PerlHandler => 'Apache2::Registry',SetHandler => 'perl-script',
Options => 'ExecCGI' );$VirtualHost{'www.foo.com'} = (
Location => { '/' => { Deny => 'from all', Order => 'deny,allow', Allow => 'from foo.com', Allowoverride => 'All' } }, DocumentRoot => '/tmp/docs', ErrorLog => '/dev/null' ); 1; END It is important to put the call to "dump" in it's own "" section, otherwise the content of the current " " section will not be dumped. ""AAppaacchhee22::::PPeerrllSSeeccttiioonnss->>ssttoorree""
This method will call the "dump" method, writing the output to a file, suitable to be pulled in via "require" or "do".Apache2::PerlSections->store($filename);
arg1: $filename (string)
The filename to save the dump output to ret: no return value AAddvvaanncceedd AAPPII modperl 2.0 now introduces the same general concept of handlers to"
handler for them. To specify a different handler for a given perl section, an extra handler argument must be given to the section:" sections. Apache2::PerlSections simply being the default And in My/PerlSection/Handler.pm: sub My::Handler::handler : handler { $foo = 1;
$bar = 2;
my ($self, $parms, $args) = @;
#do your thing!
} So, when that given "" block in encountered, the code within will first be evaluated, then the handler routine will be invoked with 3 arguments: arg1: $self
self-explanatory
arg2: $parms ( "Apache2::CmdParms" )
$parms is specific for the current Container, for example, you
might want to call "$parms->server()" to get the current server.
arg3: $args ( "APR::Table object")
the table object of the section arguments. The 2 guaranteed ones will be:$args->{'handler'} = 'My::PerlSection::Handler';
$args->{'package'} = 'Apache2::ReadConfig';
Other "name="value"" pairs given on the "" line will also be included. At this point, it's up to the handler routing to inspect the namespace of the $args->{'package'} and chooses what to do.
The most likely thing to do is to feed configuration data back intoapache. To do that, use Apache2::Server->addconfig("directive"), for
example:$parms->server->addconfig("Alias /foo /bar");
Would create a new alias. The source code of "Apache2::PerlSections" is
a good place to look for a practical example. VVeerriiffyyiinngg ""<>"" SSeeccttiioonnss If the " " sections include no code requiring a running modperl, it is possible to check those from the command line. But the following trick should be used: # file: httpd.conf
Now you can run: #!perl
# ... code here ...
END% perl -c httpd.conf
BBuuggss <> ddiirreeccttiivvee mmiissssiinngg cclloossiinngg ''>>'' httpd-2.0.47 had a bug in the configuration parser which caused the
startup failure with the following error: Starting httpd: Syntax error on line ... of /etc/httpd/conf/httpd.conf:directive missing closing '>' [FAILED] This has been fixed in httpd-2.0.48. If you can't upgrade to this or a
higher version, please add a space before the closing '>' of the opening tag as a workaround. So if you had:change it to be: # some code
< # some code
>[[......]]>> wwaass nnoott cclloosseedd.. On encountering a one-line
will cause a startup failure with an error similar to this one: Starting httpd: Syntax error on line ... of /etc/httpd/conf/httpd.conf:block, httpd's configuration parser use> was not closed. If you have written a simple one-line
section like this one : use Apache::DBI; change it to be:use Apache::DBI; This is caused by a limitation of httpd's configuration parser and isnot likely to be changed to allow one-line block like the example
above. Use multi-line blocks instead.
SSeeee AAllssoo modperl 2.0 documentation. CCooppyyrriigghhtt modperl 2.0 and its core modules are copyrighted under The Apache Software License, Version 2.0. AAuutthhoorrss The modperl development team and numerous contributors.peralpavc5h.e8.m8odperl-101.1~2::modper2l0-025.-01.02-:2:0docs::api::Apache2::PerlSections(3)