The following diagram depicts the Apache 2.0 server life cycle and highlights which handlers are available to mod_perl 2.0:

Apache 2.0 starts by parsing the configuration file.
After the configuration file is parsed,
the PerlOpenLogsHandler handlers are executed if any.
After that it's a turn of PerlPostConfigHandler handlers to be run.
When the post_config phase is finished the server immediately restarts,
to make sure that it can survive graceful restarts after starting to serve the clients.

When the restart is completed,
Apache 2.0 spawns the workers that will do the actual work.
Depending on the used MPM,
these can be threads,
processes or a mixture of both.
For example the worker MPM spawns a number of processes,
each running a number of threads.
When each child process is started PerlChildInitHandler handlers are executed.
Notice that they are run for each starting process,
not a thread.

From that moment on each working thread processes connections until it's killed by the server or the server is shutdown.

When we perform a server startup followed by a shutdown, the logs/startup_log is created if it didn't exist already (it shares the same directory with error_log and other standard log files), and each stage appends to that file its log information. So when we perform:

First of all, we can clearly see that Apache always restart itself after the first post_config phase is over. The logs show that the post_config phase is preceded by the open_logs phase. Only after Apache has restarted itself and has completed the open_logs and post_config phase again, the child_init phase is run for each child process. In our example we have had the setting StartServers=4, therefore you can see four child processes were started.

Finally you can see that on server shutdown, the child_exit phase is run for each child process and the END {} block is executed by the parent process and each of the child processes. This is because that END block was inherited from the parent on fork.

However the presented behavior varies from MPM to MPM. This demonstration was performed using prefork mpm. Other MPMs like winnt, may run open_logs and post_config more than once. Also the END blocks may be run more times, when threads are involved. You should be very careful when designing features relying on the phases covered in this chapter if you plan support multiple MPMs. The only thing that's sure is that you will have each of these phases run at least once.

Apache also specifies the pre_config phase, which is executed before the configuration files are parsed, but this is of no use to mod_perl, because mod_perl is loaded only during the configuration phase.

Now let's discuss each of the mentioned startup handlers and their implementation in the MyApache2::StartupLog module in detail.

The open_logs handler is passed four arguments: the configuration pool, the logging stream pool, the temporary pool and the main server object.

The pool arguments are:

$conf_pool is the main process sub-pool, therefore its life-span is the same as the main process's one. The main process is a sub-pool of the global pool.

$log_pool is a global pool's sub-pool, therefore its life-span is the same as the Apache program's one.

META: what is it good for if it lives the same life as conf pool?

$temp_pool is a $conf_pool subpool, created before the config phase, lives through the open_logs phase and get destroyed after the post_config phase. So you will want to use that pool for doing anything that can be discarded before the requests processing starts.

In our example the handler opens a log file for appending and sets the filehandle to unbuffered mode. It then logs the fact that it's running in the parent process.

As you've seen in the example this handler is configured by adding to the top level of httpd.conf:

PerlOpenLogsHandler MyApache2::StartupLog::open_logs

This handler can be executed only by the main server. If you want to traverse the configured virtual hosts, you can accomplish that using a simple loop. For example to print out the configured port numbers do:

The post_config phase happens right after Apache has processed the configuration files, before any child processes were spawned (which happens at the child_init phase).

This phase can be used for initializing things to be shared between all child processes. You can do the same in the startup file, but in the post_config phase you have an access to a complete configuration tree (via Apache2::Directive).

The child_init phase happens immediately after the child process is spawned. Each child process (not a thread!) will run the hooks of this phase only once in their life-time.

In the prefork MPM this phase is useful for initializing any data structures which should be private to each process. For example Apache::DBI pre-opens database connections during this phase and Apache2::Resource sets the process' resources limits.

Opposite to the child_init phase, the child_exit phase is executed before the child process exits. Notice that it happens only when the process exits, not the thread (assuming that you are using a threaded mpm).

As explained in the Server Life Cycle section, on start Apache normally runs the server configuration phase, followed by PerlOpenLogsHandler and PerlPostConfigHandler phases, then immediately restarts itself. Therefore everything running at the server startup is executed twice. During the restart, Perl is completely destroyed and started again.

If Apache is started as 'httpd -t' (equivalent to 'apachectl configtest') or as 'httpd -S', it will run only the configuration phase and exit. Depending on your configuration file, it may or may not start perl. See the details below.

During the normal startup, mod_perl 2.0 postpones the startup of perl until after the configuration phase is over, to allow the usage of the PerlSwitches directive, which can't be used after Perl is started.

When any of the following configuration directives is encountered (during the configuration phase) mod_perl 2.0 is forced to start as soon as they are encountered (as these options require a running perl):

Therefore if you want to trigger an early Perl startup, you could add an empty <Perl> section in httpd.conf:

<Perl>
# trigger an early Perl startup
</Perl>

right after loading the mod_perl module, if you are using DSO, or just before your mod_perl configuration section, if you're using a static mod_perl build. But most likely you want to use the PerlConfigRequire instead.