Press "Enter" to skip to content


Apache2开始引入了Hook方式, 对应的在PHP中也提供了apache2handler这种sapi.
和我之前介绍过的PHP lifecycle不同, 这种情况下的PHP, 通过注册handler钩子, 从而可以在handler hook阶段有机会处理请求, 通过判断请求的handler, 来确定是否需要处理, 如果需要就会调用自身的处理器.
那么, 这种情况下也就有了多种配置方法, 主要考虑如下俩种方式(第二种可以有多种变种):

第一种:  AddType application/x-httpd-php .php
      <filesMatch \.php$>
          SetHandler application/x-httpd-php

首先来说, 这俩中方式的起作用的时刻不同, 对于第一种方式来说, 他是在type_check钩子阶段起作用的, 也就是在apache2src/modules/http/mod_mime.c中, 通过注册type_checker钩子, 加入find_ct(content_type), 在find_ct中, 通过配置文件中的mime映射, 或者是通过addType指令增加的映射, 根据文件的扩展来填充请求中的handler字段:
而对于第二种方式, 是在fixup钩子阶段, 通过注册fixups钩子阶段加入core_override_type(apache2src/server/core.c)函数, 来将目录级的配置指令生效.
而fixups钩子是晚于type_checker钩子的, 也是handler钩子之前最后的一个可以利用的钩子. 所以如果同时采用1,2俩种方式, 那么第二种方式会覆盖第一种方式设置的handler.
其次, 俩种方式依赖的数据结构不同, 第一种方式依赖的是一个全局的mime对照表extension_mappings, 这个表由mime配置文件和AddType指令而来.
而对于第二种方式, 它是根据配置文件构造的dir_config而来:

  core_dir_config *conf =
        (core_dir_config *)ap_get_module_config(r->per_dir_config,
    /* Check for overrides with ForceType / SetHandler
    if (conf->mime_type && strcmp(conf->mime_type, "none"))
        ap_set_content_type(r, (char*) conf->mime_type);
    if (conf->handler && strcmp(conf->handler, "none"))
        r->handler = conf->handler;


  1. retro bowl
    retro bowl November 1, 2022

    Thank you so much for the article. It was very informative and I will be sure to read it again in the future. Keep up the good work!

  2. retro bowl
    retro bowl November 1, 2022

    Glad you set up this forum. It is very useful for everyone.

  3. 5 letter words
    5 letter words August 5, 2022

    Although both PHP and Apache are servers that can process requests from clients, there are a number of differences between the two.

  4. numberle
    numberle August 5, 2022

    It is important to understand the differences between running PHP with Apache2 in the default Web server configuration versus running PHP with Apache2 in the mod_php configuration.

  5. ai dungeon
    ai dungeon June 11, 2021

    您的文章给我带来了海量的宝贵资料,不凡的分享,不胜感激。 我会定期回到你的下一篇文章,所以请继续提供这些有用的数据。

  6. router login
    router login March 10, 2018

    Hope this site stays active, at least long enough for me to download EVERYTHING. Thanks guys.

  7. jankuo
    jankuo January 16, 2011


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.