[PHP source code learning notes] PHP startup

Source: Internet
Author: User
Tags php source code sapi

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
    "Extensions and application repositories", 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.
    Resource Manager) thread-safe 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. Five hooks: php_value and php_flag
Php_admin_value php_admin_flag phpinidir main application and different directories and websites for calling different PHP configuration: 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.