Press "Enter" to skip to content

Apache2中俩种设置PHP的异同

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
     </filesMatch>

首先来说, 这俩中方式的起作用的时刻不同, 对于第一种方式来说, 他是在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,
                                                &core_module);
    /* 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;
.....

5 Comments

  1. 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.

  2. 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.

  3. ai dungeon
    ai dungeon June 11, 2021

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

  4. router login
    router login March 10, 2018

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

  5. jankuo
    jankuo January 16, 2011

    是不是在效率方面第一种方式更好点,
    在文件广度方面,是不是第一种更广点?
    第二种只限定了是PHP结尾的文件

Comments are closed.