[Php source code learning notes] php startup

Source: Internet
Author: User
Tags sapi
From the module mode of apche to php, I said apache has two ways to call php: module mode and cgi Mode. I will call php in the module mode to start analysis. First you have to look at the Php source code directory from: www. php-internal.combook? Pchapt0101-02-code-structure root directory

From the module mode of apche to php, I said apache has two ways to call php: module mode and cgi Mode. I will call php in the module mode to start analysis. First you have to look at the Php source code directory from: http://www.php-internal.com/book? P = chapt01/01-02-code-structure root directory

From apcheModule to enter php


ApacheCall phpThere are two methods: one is the module mode and the other is cgi.Mode.Start analysis.




First, let's take a look at PhpSource code directory

From: http://www.php-internal.com/book? P = chapt01/01-02-code-structure

  • Root directory :/This directory contains many things, including some instruction files and design solutions. In fact, the README files in the project are worth reading. For example:

    • /README.PHP4-TO-PHP5-THIN-CHANGES this file lists in detail some of the differences between PHP4 and PHP5.

    • There is also an important file/CODING_STANDARDS. If you want to write PHP extensions, you must read this file, no matter what your code style is, how to use indentation and curly brackets, now that you have come to such a group, you should adapt to this specification, so that it is easier to read the code or other people's code.

  • BuildAs the name suggests, here we mainly place some files related to source code compilation, such as the buildconf script before building, and some scripts for checking the environment.

  • ExtThe official extension directory contains the definition and implementation of most PHP functions, such as the array, pdo, and spl functions. Extensions written by individuals can also be placed in this directory during testing to facilitate testing and debugging.

  • MainThe most core PHP files are stored here, mainly implementing the basic facilities of PHP. This is different from the Zend engine. The Zend engine mainly implements the language runtime environment with the most core.

  • ZendZend engine implementation directory, such as script lexical Syntax Parsing, opcode execution, and extension mechanism implementation.

  • Pear"PHP extension and application repository", including core PEAR files.

  • SapiContains various Server abstraction layer code, such as apache mod_php, cgi, fastcgi, and fpm interfaces.

  • TSRMPHP thread security is built on the TSRM library. Common * G Macros in PHP implementation are encapsulation of TSRM, and TSRM (ThreadSafe Resource Manager) thread security Resource Manager.

  • TestsPHP test script set, which contains test files for various PHP Functions

  • Win32This directory mainly includes some implementations related to the Windows platform. For example, sokcet is not the same as * Nix platform in Windows, but also includes compiling PHP-related scripts in Windows.



To run the PHP file in apache, you must make some configuration. Otherwise, the PHP file will be output as a file.


Run php In the module mode and add it to the apache configuration file

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


Such a sentence. Load the php module.

Apache loads the php module at startup by loading the struct:

AP_MODULE_DECLARE_DATAmodule php5_module = {STANDARD20_MODULE_STUFF,/* macro, including version, minor version, module index, Module name, next module pointer, and other information, the module name is represented by _ FILE _ */create_php_config,/* Create php Directory configuration */merge_php_config,/* merge php Directory configuration */NULL, /* Create host directory configuration */NULL,/* merge host configuration */php_dir_cmds, /* configure related commands for the module */php_ap2_register_hook/* register the hook function of the module */};



This code is in./sapi/apache2handler/mod_php5.c.


Apache uses the configuration parameters here to load the corresponding configurations. Apapche maintains three linked lists: the linked list module of the hook Chain List module and configures the linked list. Add these configurations to the three linked lists respectively. Some details about apache are involved here. These two days are a bit confusing. Next time, we will make a special research.

Parameter description:

STANDARD20_MODULE_STUFF. this parameter is a macro that stores the current apapche version information of the module. When the apapche module is loaded, it checks the definition in this macro to see if it is compatible with this module, if not compatible, Stop loading.


Create_php_config

Merge_php_config is two function pointers in apache_config.c in the same directory. The Initialization Configuration is called When apache starts the configuration. (Check the details here)


The host configuration is mainly used for Server Virtual Hosts. php is not the first virtual host, but not the first virtual host.


Php_dir_cmds is also a function pointer, which is also in apache_config.c. It is mainly used to register the parameter hooks passed to php during php startup. There are five hooks: php_value, php_flag php_admin_value php_admin_flag PHPINIDir main application and different directories and websites to call different php configuration: For details, see: http://blog.zuoxizhidu.com/1/125.htm



The php_ap2_register_hook hook related to application processing is also in sapi_apache2.c. It registers four hooks.

When the server is started, ap_hook_pre_config, ap_hook_post_config, and ap_hook_child_init are called. The ap_hook_pre_config hook is used to check whether the php process is started. An error is reported when the php process is started. Ap_hook_post_config calls the php_apache_server_startup function for the php module. It will be officially started here, and php and zend will be initialized.

Ap_hook_child_init is used to start sub-processes. The hook used by php_handle for user request is checked here and the file is officially executed in php.




It took a long time for a startup to get a little clear. Sorry!


If I understand something wrong, I hope Daniel can help me correct it!


Also found a good look at php source code site, directly to the function definition on the link a little can be in the function definition and reference A Very Good http://lxr.sweon.net/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.