Home php教程 php手册 【php源代码学习笔记】php的启动

【php源代码学习笔记】php的启动

Jun 06, 2016 pm 07:49 PM
php start up study Way module source code notes

从 apche 的模块方式进入 php 上回说道 apache 调用 php 有两种方式一个是模块方式,一种是 cgi 模式,我就从模块方式调用 php 开始分析。 首先得看看 Php 源代码目录机构 摘自: http://www.php-internal.com/book/?p=chapt01/01-02-code-structure 根目录

apche的模块方式进入php


上回说道apache调用php有两种方式一个是模块方式,一种是cgi模式,我就从模块方式调用php开始分析。




首先得看看Php源代码目录机构

摘自:http://www.php-internal.com/book/?p=chapt01/01-02-code-structure

  • 根目录:/这个目录包含的东西比较多,主要包含一些说明文件以及设计方案。其实项目中的这些README文件是非常值得阅读的例如:

    • /README.PHP4-TO-PHP5-THIN-CHANGES这个文件就详细列举了PHP4PHP5的一些差异。

    • 还有有一个比较重要的文件/CODING_STANDARDS,如果要想写PHP扩展的话,这个文件一定要阅读一下,不管你个人的代码风格是什么样,怎么样使用缩进和花括号,既然来到了这样一个团体里就应该去适应这样的规范,这样在阅读代码或者别人阅读你的代码是都会更轻松。

  • build顾名思义,这里主要放置一些和源码编译相关的一些文件,比如开始构建之前的buildconf脚本等文件,还有一些检查环境的脚本等。

  • ext官方扩展目录,包括了绝大多数PHP的函数的定义和实现,如array系列,pdo系列,spl系列等函数的实现,都在这个目录中。个人写的扩展在测试时也可以放到这个目录,方便测试和调试。

  • main这里存放的就是PHP最为核心的文件了,主要实现PHP的基本设施,这里和Zend引擎不一样,Zend引擎主要实现语言最核心的语言运行环境。

  • ZendZend引擎的实现目录,比如脚本的词法语法解析,opcode的执行以及扩展机制的实现等等。

  • pear“PHP 扩展与应用仓库”,包含PEAR的核心文件。

  • sapi包含了各种服务器抽象层的代码,例如apachemod_phpcgifastcgi以及fpm等等接口。

  • TSRMPHP的线程安全是构建在TSRM库之上的,PHP实现中常见的*G宏通常是对TSRM的封装,TSRM(ThreadSafe Resource Manager)线程安全资源管理器。

  • testsPHP的测试脚本集合,包含PHP各项功能的测试文件

  • win32这个目录主要包括Windows平台相关的一些实现,比如sokcet的实现在Windows下和*Nix平台就不太一样,同时也包括了Windows下编译PHP相关的脚本。



要想apache能运行php文件得做一些配置。否则php文件会被当作文件被输出。


采用模块方式运行php,在apache的配置文件中需要添加

LoadModulephp5_module /usr/lib/apache2/modules/libphp5.so


这样的一句。其实就是说加载php模块

apache在启动时候会加载php模块,通过加载结构体:

AP_MODULE_DECLARE_DATAmodule php5_module = {

STANDARD20_MODULE_STUFF,/*宏,包括版本,小版本,模块索引,模块名,下一个模块指针等信息,其中模块名以__FILE__体现

*/

create_php_config, /*创建php目录配置*/

merge_php_config, /*合并php目录配置*/

NULL, /*创建主机目录配置  */

NULL, /*合并主机合并配置*/

php_dir_cmds, /*为模块配置相关指令 */

php_ap2_register_hook /*注册模块的钩子函数 */

};
Copy after login



此代码在./sapi/apache2handler/mod_php5.c中。


apache通过这里的配置配置参数,分别加载对应的配置。apapche会维护三个链表:hook链表模块链表模块配置链表。将这些配置分别加入到这三个链表中。这里涉及到apache的一些细节,这两天看得有点迷糊。下次做专门研究。

参数说明:

STANDARD20_MODULE_STUFF,这个参数是个宏,存储了模块当前适应的apapche版本信息,apapche加载模块的时候会检测这个宏中的定义来看是否兼容此模块,如果不兼容则停止加载。


create_php_config

merge_php_config是两个函数指针在同目录下的apache_config.c中。当apache启动配置时候会调用做初始化配置。(这里详细的得后查)


主机的配置主要用于服务器虚拟主机,php不是先虚拟主机,没有。


php_dir_cmds也是以函数指针,也在apache_config.c中。主要用于注册php启动时候传递给php的参数钩子。其中有5个钩子:php_valuephp_flag php_admin_value php_admin_flag PHPINIDir主要应用与不同目录和网站对调用不同php配置的情况:详情参看:http://blog.zuoxizhidu.com/1/125.htm



php_ap2_register_hook应用处理相关的钩子 ,也在sapi_apache2.c中。其注册了4个钩子。

服务器启动时调用ap_hook_pre_configap_hook_post_configap_hook_child_initap_hook_pre_config钩子用于检测php进程是否启动,已经启动报错。ap_hook_post_config调用的是函数php_apache_server_startup用于php模块在这里会被正式启动,初始化phpzend

ap_hook_child_init用于子进程的启动。php_handle用于用户请求时候的hook在这里检查文件正式执行php




一个启动弄了好半天才看得稍微明白,惭愧!


如果我理解的不对的 希望大牛帮忙指正!


另外发现一个看php源代码的好网站,直接给函数定义上了链接 一点就可以进函数定义和引用的地方非常好http://lxr.sweon.net/










Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles