首先,了解一下每个Apache模块都会输出一个模块数据结构,其定义结构如下(以PHP模块为例):
AP_MODULE_DECLARE_DATA module php5_module = { STANDARD20_MODULE_STUFF, create_php_config, /* create per-directory config structure */ merge_php_config, /* merge per-directory config structures */ NULL, /* create per-server config structure */ NULL, /* merge per-server config structures */ php_dir_cmds, /* command apr_table_t */ php_ap2_register_hook /* register hooks */ };STANDARD20_MODULE_STUFF这个宏是必须的,这个宏在经过扩展以后,可以为编译后的模块载入服务器构建提供版本信息,在PHP的模块中,create_php_config函数创建目录配置结构,,merge_php_config合并目录配置结构,php_dir_cmds函数为模块配置相关指令,php_ap2_register_hook 注册模块的钩子程序,对这个结构体会在apache的模块中专门说明,在这里主要关注的是php_ap2_register_hook.这里定义的一些回调方法,会在Apache启动的时候直接调用。
php_ap2_register_hook函数的实现在源码sapi/apache2handler/sapi_apache2.c文件中:
void php_ap2_register_hook(apr_pool_t *p) { ap_hook_pre_config(php_pre_config, NULL, NULL, APR_HOOK_MIDDLE); ap_hook_post_config(php_apache_server_startup, NULL, NULL, APR_HOOK_MIDDLE); ap_hook_handler(php_handler, NULL, NULL, APR_HOOK_MIDDLE); ap_hook_child_init(php_apache_child_init, NULL, NULL, APR_HOOK_MIDDLE); }php_apache_server_startup函数也在同一个文件里面实现:
static int php_apache_server_startup(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s) { void *data = NULL; const char *userdata_key = "apache2hook_post_config"; /* Apache will load, unload and then reload a DSO module. This * prevents us from starting PHP until the second load. */ apr_pool_userdata_get(&data, userdata_key, s->process->pool); if (data == NULL) { /* We must use set() here and *not* setn(), otherwise the * static string pointed to by userdata_key will be mapped * to a different location when the DSO is reloaded and the * pointers won't match, causing get() to return NULL when * we expected it to return non-NULL. */ apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool); return OK; } /* Set up our overridden path. */ if (apache2_php_ini_path_override) { apache2_sapi_module.php_ini_path_override = apache2_php_ini_path_override; } #ifdef ZTS tsrm_startup(1, 1, 0, NULL); #endif sapi_startup(&apache2_sapi_module); apache2_sapi_module.startup(&apache2_sapi_module); apr_pool_cleanup_register(pconf, NULL, php_apache_server_shutdown, apr_pool_cleanup_null); php_apache_add_version(pconf); return OK; }