NAME
mod_perl 2.0 Server Configuration
Description
This chapter provides an indepth mod_perl 2.0 configuration details.
mod_perl configuration directives
Similar to mod_perl 1.0, in order to use mod_perl 2.0 a few configuration settings should be added to httpd.conf. They are quite similar to 1.0 settings but some directives were renamed and new directives were added.
Enabling mod_perl
To enable mod_perl built as DSO add to httpd.conf:
LoadModule perl_module modules/mod_perl.so
This setting specifies the location of the mod_perl module relative to the ServerRoot
setting, therefore you should put it somewhere after ServerRoot
is specified.
If mod_perl has been statically linked it's automatically enabled.
Perl's Command Line Switches
Now you can pass any Perl's command line switches in httpd.conf using the PerlSwitches
directive. For example to enable warnings and Taint checking add:
PerlSwitches -wT
As an alternative to using use lib
in startup.pl to adjust @INC
, now you can use the command line switch -I
to do that:
PerlSwitches -I/home/stas/modperl
You could also use -Mlib=/home/stas/modperl
which is the exact equivalent as use lib
, but it's broken on certain platforms/version (e.g. Darwin/5.6.0). use lib
is removing duplicated entries, whereas -I
does not.
PerlOptions Directive
The directive PerlOptions
provides fine-grained configuration for what were compile-time only options in the first mod_perl generation. It also provides control over what class of PerlInterpreter
is used for a <VirtualHost>
or location configured with <Location>
, <Directory>
, etc.
Options are enabled by prepending +
and disabled with -
. The options include:
Enable
On by default, can be used to disable mod_perl for a given VirtualHost
. For example:
<VirtualHost ...>
PerlOptions -Enable
</VirtualHost>
Clone
Share the parent Perl interpreter, but give the VirtualHost
its own interpreter pool. For example should you wish to fine tune interpreter pools for a given virtual host:
<VirtualHost ...>
PerlOptions +Clone
PerlInterpStart 2
PerlInterpMax 2
</VirtualHost>
This might be worthwhile in the case where certain hosts have their own sets of large-ish modules, used only in each host. By tuning each host to have its own pool, that host will continue to reuse the Perl allocations in their specific modules.
When cloning a Perl interpreter, to inherit base Perl interpreter's PerlSwitches
use:
<VirtualHost ...>
...
PerlSwitches +inherit
</VirtualHost>
Parent
Create a new parent Perl interpreter for the given VirtualHost
and give it its own interpreter pool (implies the Clone
option).
A common problem with mod_perl 1.0 was the shared namespace between all code within the process. Consider two developers using the same server and each wants to run a different version of a module with the same name. This example will create two parent Perl interpreters, one for each <VirtualHost>
, each with its own namespace and pointing to a different paths in @INC
:
<VirtualHost ...>
ServerName dev1
PerlOptions +Parent
PerlSwitches -Mblib=/home/dev1/lib/perl
</VirtualHost>
<VirtualHost ...>
ServerName dev2
PerlOptions +Parent
PerlSwitches -Mblib=/home/dev2/lib/perl
</VirtualHost>
Or even for a given location, for something like "dirty" cgi scripts:
<Location /cgi-bin>
PerlOptions +Parent
PerlInterpMaxRequests 1
PerlInterpStart 1
PerlInterpMax 1
PerlResponseHandler ModPerl::Registry
</Location>
will use a fresh interpreter with its own namespace to handle each request.
Perl*Handler
Disable Perl*Handler
s, all compiled-in handlers are enabled by default. The option name is derived from the Perl*Handler
name, by stripping the Perl
and Handler
parts of the word. So PerlLogHandler
becomes Log
which can be used to disable PerlLogHandler
:
PerlOptions -Log
Suppose one of the hosts does not want to allow users to configure PerlAuthenHandler
, PerlAuthzHandler
, PerlAccessHandler
and <Perl> sections:
<VirtualHost ...>
PerlOptions -Authen -Authz -Access -Sections
</VirtualHost>
Or maybe everything but the response handler:
<VirtualHost ...>
PerlOptions None +Response
</VirtualHost>
AutoLoad
Resolve Perl*Handlers
at startup time, which includes loading the modules from disk if not already loaded.
In mod_perl 1.0, configured Perl*Handlers
which are not a fully qualified subroutine names are resolved at request time, loading the handler module from disk if needed. In mod_perl 2.0, configured Perl*Handlers
are resolved at startup time. By default, modules are not auto-loaded during startup-time resolution. It is possible to enable this feature with:
PerlOptions +Autoload
Consider this configuration:
PerlResponseHandler Apache::Magick
In this case, Apache::Magick
is the package name, and the subroutine name will default to handler. If the Apache::Magick
module is not already loaded, PerlOptions +Autoload
will attempt to pull it in at startup time. With this option enabled you don't have to explicitly load the handler modules. For example you don't need to add:
PerlModule Apache::Magick
in our example.
GlobalRequest
Setup the global request_rec
for use with Apache->request
. This setting is needed for example if you use CGI.pm
to process the incoming request.
This setting is enabled by default for sections configured as:
<Location ...>
SetHandler perl-script
...
</Location>
And can be disabled with:
<Location ...>
SetHandler perl-script
PerlOptions -GlobalRequest
...
</Location>
ParseHeaders
Scan output for HTTP headers, same functionality as mod_perl 1.0's PerlSendHeaders
, but more robust. This option is usually needs to be enabled for registry scripts which send the HTTP header with:
print "Content-type: text/html\n\n";
MergeHandlers
Turn on merging of Perl*Handler
arrays. For example with a setting:
PerlFixupHandler Apache::FixupA
<Location /inside>
PerlFixupHandler Apache::FixupB
</Location>
a request for /inside only runs Apache::FixupB
(mod_perl 1.0 behavior). But with this configuration:
PerlFixupHandler Apache::FixupA
<Location /inside>
PerlOptions +MergeHandlers
PerlFixupHandler Apache::FixupB
</Location>
a request for /inside will run both Apache::FixupA
and Apache::FixupB
handlers.
SetupEnv
Set up enviroment variables for each request ala mod_cgi.
When this option is enabled, mod_perl fiddles with the environment to make it appear as if the code is called under the mod_cgi handler. For example, the $ENV{QUERY_STRING}
environment variable is initialized with the contents of Apache::args(), and the value returned by Apache::server_hostname() is put into $ENV{SERVER_NAME}
.
But %ENV
population is expensive. Those who have moved to the Perl Apache API no longer need this extra %ENV
population, and can gain by disabling it. A code using the CGI.pm
module require PerlOptions +SetupEnv
because that module relies on a properly populated CGI environment table.
This option is enabled by default for sections configured as:
<Location ...>
SetHandler perl-script
...
</Location>
Since this option adds an overhead to each request, if you don't need this functionality you can turn it off for a certain section:
<Location ...>
SetHandler perl-script
PerlOptions -SetupEnv
...
</Location>
or globally:
PerlOptions -SetupEnv
<Location ...>
...
</Location>
and then it'll affect the whole server. It can still be enabled for sections that need this functionality.
When this option is disabled you can still read environment variables set by you. For example when you use the following configuration:
PerlOptions -SetupEnv
PerlModule Modperl::Registry
<Location /perl>
PerlSetEnv TEST hi
SetHandler perl-script
PerlHandler ModPerl::Registry
Options +ExecCGI
</Location>
and you issue a request for this script:
setupenvoff.pl
--------------
use Data::Dumper;
my $r = Apache->request();
$r->send_http_header('text/plain');
print Dumper(\%ENV);
you should see something like this:
$VAR1 = {
'GATEWAY_INTERFACE' => 'CGI-Perl/1.1',
'MOD_PERL' => 'mod_perl/2.0.1',
'PATH' => 'bin:/usr/bin',
'TEST' => 'hi'
};
Notice that we have got the value of the environment variable TEST.
Handlers Directives
META: need to add descriptions
PerlChildInitHandler
PerlOpenLogsHandler
PerlPostConfigHandler
PerlPreConnectionHandler
PerlProcessConnectionHandler
PerlHeaderParserHandler
PerlAccessHandler
PerlAuthenHandler
PerlAuthzHandler
PerlTypeHandler
PerlFixupHandler
PerlResponseHandler
PerlLogHandler
PerlPostReadRequestHandler
PerlInitHandler
PerlTransHandler
PerlOutputFilterHandler
The mod_perl 2.0 interface to the Apache filtering API is much simpler than the C API, hiding most of the details underneath. Perl filters are configured using the PerlOutputFilterHandler
directive. For example:
PerlOutputFilterHandler Apache::ReverseFilter
This simply registers the filter, which can then be turned on using the core AddOutputFilter
directive:
<Location /filterme>
AddOutputFilter Apache::ReverseFilter
</Location>
The Apache::ReverseFilter
handler will now be called for anything accessed in the /filterme URL space. The AddOutputFilter
directive takes any number of filters. For example, the configuration:
AddOutputFilter INCLUDE Apache::ReverseFilter
will first send the output to mod_include, which will in turn pass its output down to Apache::ReverseFilter
.
Threads Mode Specific Directives
These directives are enabled only in a threaded mod_perl+Apache combo:
PerlInterpStart
The number of intepreters to clone at startup time.
PerlInterpMax
If all running interpreters are in use, mod_perl will clone new interpreters to handle the request, up until this number of interpreters is reached. when PerlInterpMax
is reached, mod_perl will block (via COND_WAIT()) until one becomes available (signaled via COND_SIGNAL()).
PerlInterpMinSpare
The minimum number of available interpreters this parameter will clone interpreters up to PerlInterpMax
, before a request comes in.
PerlInterpMaxSpare
mod_perl will throttle down the number of interpreters to this number as those in use become available.
PerlInterpMaxRequests
The maximum number of requests an interpreter should serve, the interpreter is destroyed when the number is reached and replaced with a fresh clone.
PerlInterpScope
As mentioned, when a request in a threaded mpm is handled by mod_perl, an interpreter must be pulled from the interpreter pool. The interpreter is then only available to the thread that selected it, until it is released back into the interpreter pool. By default, an interpreter will be held for the lifetime of the request, equivalent to this configuration:
PerlInterpScope request
For example, if a PerlAccessHandler
is configured, an interpreter will be selected before it is run and not released until after the logging phase.
Intepreters will be shared across subrequests by default, however, it is possible to configure the intepreter scope to be per-subrequest on a per-directory basis:
PerlInterpScope subrequest
With this configuration, an autoindex generated page, for example, would select an interpreter for each item in the listing that is configured with a Perl*Handler.
It is also possible to configure the scope to be per-handler:
PerlInterpScope handler
With this configuration, an interpreter will be selected before PerlAccessHandlers
are run, and putback immediately afterwards, before Apache moves onto the authentication phase. If a PerlFixupHandler
is configured further down the chain, another interpreter will be selected and again putback afterwards, before PerlResponseHandler
is run.
For protocol handlers, the interpreter is held for the lifetime of the connection. However, a C protocol module might hook into mod_perl (e.g. mod_ftp) and provide a request_rec
record. In this case, the default scope is that of the request. Should a mod_perl handler want to maintain state for the lifetime of an ftp connection, it is possible to do so on a per-virtualhost basis:
PerlInterpScope connection
mod_perl Directives Argument Types and Allowed Location
The following table shows where in the configuration files mod_perl configuration directives are allowed to appear, what kind of and how many arguments they expect:
General directives:
Directive Arguments SRV DIR
---------------------------------------------------
PerlSwitches ITERATE V
PerlRequire ITERATE V
PerlModule ITERATE V
PerlOptions ITERATE V V
PerlSetVar TAKE2 V V
PerlAddVar ITERATE2 V V
PerlSetEnv TAKE2 V V
PerlPassEnv TAKE1 V
<Perl> Sections RAW_ARGS V
PerlTrace TAKE1 V
Handler assignment directives:
Directive Arguments TYPE SRV DIR
---------------------------------------------------------
PerlOpenLogsHandler ITERATE RUN_ALL V
PerlPostConfigHandler ITERATE RUN_ALL V
PerlChildInitHandler ITERATE VOID V
PerlPreConnectionHandler ITERATE RUN_ALL V
PerlProcessConnectionHandler ITERATE RUN_FIRST V
PerlPostReadRequestHandler ITERATE RUN_ALL V
PerlTransHandler ITERATE RUN_FIRST V
PerlInitHandler ITERATE RUN_ALL V V
PerlHeaderParserHandler ITERATE RUN_ALL V V
PerlAccessHandler ITERATE RUN_ALL V V
PerlAuthenHandler ITERATE RUN_FIRST V V
PerlAuthzHandler ITERATE RUN_FIRST V V
PerlTypeHandler ITERATE RUN_FIRST V V
PerlFixupHandler ITERATE RUN_ALL V V
PerlResponseHandler ITERATE RUN_FIRST V V
PerlLogHandler ITERATE RUN_ALL V V
PerlCleanupHandler ITERATE XXX V V
PerlInputFilterHandler ITERATE VOID V V
PerlOutputFilterHandler ITERATE VOID V V
Perl Interpreter management directives:
Directive Arguments SRV DIR
---------------------------------------------------
PerlInterpStart TAKE1 V
PerlInterpMax TAKE1 V
PerlInterpMinSpare TAKE1 V
PerlInterpMaxSpare TAKE1 V
PerlInterpMaxRequests TAKE1 V
PerlInterpScope TAKE1 V V
mod_perl 1.0 back compatibility directives:
Directive Arguments SRV DIR
---------------------------------------------------
PerlHandler ITERATE V V
PerlSendHeader FLAG V V
PerlSetupEnv FLAG V V
PerlTaintCheck FLAG V
PerlWarn FLAG V
The Arguments column represents the type of arguments directives accepts, where:
- ITERATE
-
Expects a list of arguments.
- ITERATE2
-
Expects one argument, followed by at least one or more arguments.
- TAKE1
-
Expects one argument only.
- TAKE2
-
Expects two arguments only.
- FLAG
-
One of
On
orOff
(case insensitive). - RAW_ARGS
-
The function parses the command line by itself.
The second and the third column show the locations the directives are allowed to appear in:
- SRV
-
Global configuration and
<VirtualHost>
(mnemonic: SeRVer). These directives are defined asRSRC_CONF
in the source code. - DIR
-
<Directory>
,<Location>
,<Files>
and all their regex variants (mnemonic: DIRectory). These directives can also appear in .htaccess files. These directives are defined asOR_ALL
in the source code.
Apache specifies other allowed location types which are currently not used by the core mod_perl directives and their definition can be found in include/httpd_config.h (hint: search for RSRC_CONF
).
Server Startup Options Retrieval
Inside httpd.conf one can do conditional configuration based on the define options passed at the server startup. For example:
<IfDefine PERLDB>
<Perl>
use Apache::DB ();
Apache::DB->init;
</Perl>
<Location />
PerlFixupHandler Apache::DB
</Location>
</IfDefine>
So only when the server is started as:
% httpd C<-DPERLDB> ...
The configuration inside IfDefine
will have an effect. If you want to have some configuration section to have an effect if a certain define wasn't defined use !
, for example here is the opposite of the previous example:
<IfDefine !PERLDB>
# ...
</IfDefine>
If you need to access any of the startup defines in the Perl code you use Apache::exists_config_define
. For example in a startup file you can say:
use Apache::ServerUtil ();
if (Apache::exists_config_define("PERLDB")) {
require Apache::DB;
Apache::DB->init;
}
For example to check whether the server has been started in a single mode use:
if (Apache::exists_config_define("ONE_PROCESS")) {
print "Running in a single mode";
}
MODPERL2 Define Option
When running under mod_perl 2.0 a special configuration define MODPERL2
is enabled internally, as if the server had been started with -DMODPERL2
. For example this can be used to write a configuration file which needs to do something different whether it's running under mod_perl 1.0 or 2.0:
<IfDefine MODPERL2>
# 2.0 configuration
</IfDefine>
<IfDefine !MODPERL2>
# else
</IfDefine>
From within Perl code this can be tested with Apache::exists_config_define()
, for example:
if (Apache::exists_config_define("MODPERL2")) {
# some 2.0 specific code
}
Debug Directives
PerlTrace [level]
set the trace level. This directive is enabled when mod_perl is compiled with the MP_TRACE option. level
is either:
all
which sets maximum logging and debugging levels;
a combination of one or more option letters (or option numerical equivalents) from the following list:
d ( 1) directive processing
f ( 2) filters
g ( 4) Perl runtime interaction
h ( 8) handlers
i ( 16) interpreter pool management
m ( 32) memory allocations
s ( 64) perl sections
t (128) benchmark-ish timings
When level
is not specified, the tracing level will be set to the value of the MOD_PERL_TRACE environment variable.
Maintainers
Maintainer is the person(s) you should contact with updates, corrections and patches.
Doug MacEachern <dougm (at) covalent.net>
Stas Bekman <stas (at) stason.org>
Authors
Doug MacEachern <dougm (at) covalent.net>
Only the major authors are listed above. For contributors see the Changes file.