Deep understanding of PHP kernel (2) and SAPI Exploration

Source: Internet
Author: User
Tags sapi
This article mainly introduces how to deeply understand the SAPI inquiry of PHP kernel (2). For more information, see

This article mainly introduces how to deeply understand the SAPI inquiry of PHP kernel (2). For more information, see

SAPI is the abbreviation of Server Application Programming Interface (Server Application Programming Interface. PHP provides a set of interfaces through SAPI for data interaction between the supply and the PHP kernel.

To put it simply, just like the input and output of functions, we execute a piece of PHP code through the Linux Command Line. In essence, the Linux Shell passes in a set of parameters through the SAPI of PHP, after the Zend engine is executed, it is returned to the shell, which is displayed by the shell. Similarly, Apache calls PHP and transmits data to SAPI through the Web server. After the Zend engine is executed, Apache returns the data to Apache, which is displayed on the page.


. PHP Architecture

PHP provides many forms of interfaces, including apache, apache2filter, apache2handler, caudium, cgi, cgi-fcgi, cli, cli-server, continuity, embed, isapi, litespeed, milter, nsapi, phttpd pi3web, roxen, thttpd, tux and webjames. However, there are only five common forms: CLI/CGI (command line), Multiprocess (multi-process), Multithreaded (multi-thread), FastCGI, and Embedded (Embedded ).

PHP provides a function to view the current SAPI type:

The Code is as follows:


String php_sapi_name (void)

PHP running and Loading

No matter which SAPI is used, before and after PHP executes the script, it contains a series of events: Module Init (MINT), Shutdown (MSHUTDOWN), Request Init (RINT), and Shutdown (RSHUTDOWN ). The first stage is the PHP module initialization phase (MINT). It can initialize internal variables for expansion, allocate resources, and register resource processors. This process is only executed once throughout the lifecycle of the PHP instance.

What is the PHP module? Through the above PHP architecture diagram, you can use the get_loaded_extensions function in PHP to view all the compiled and loaded modules/extensions, which is equivalent to php-m in CLI mode.

Take the Memcached extension source code of PHP as an example:

PHP_MINIT_FUNCTION (memcached) {zend_class_entry ce; memcpy (& memcached_object_handlers, zend_get_std_object_handlers (), sizeof (zend_object_handlers); handler = NULL; /* some similar initialization operations */return SUCCESS ;}

The second stage is the request initialization stage (RINT). After the module is initialized and activated, the PHP runtime environment is created, and the RINT functions registered by all modules are called to call the initialization functions of each extension request, set specific environment variables, allocate resources, or execute other tasks, such as review.

PHP_RINIT_FUNCTION (memcached) {/* execute some request initialization */return SUCCESS ;}

In the third stage, after the request processing is complete, PHP_RSHUTDOWN_FUNCTION will be called for recycling. This is the final cleaning of every extended request to close the function. The Zend engine executes the cleanup process, garbage collection, and unset for each variable used during the previous request. The request may be completed by executing the script or calling the die () or exit () function.

In the fourth stage, when the lifecycle of PHP is over, PHP_MSHUTDOWN_FUNCTION recycles the module. This is the function for each extended module to close its own kernel subsystem.

PHP_MSHUTDOWN_FUNCTION (memcached) {/* execute the module destruction work */UNREGISTER_INI_ENTRIES (); return SUCCESS ;}

Common running Modes

There are five common SAPI modes:

CLI and CGI modes (single process mode)
Multi-process mode
Multithreading Mode
FastCGI Mode
Embedded

1. CLI/CGI Mode

CLI and CGI are in single-process mode, and the lifecycle of PHP is completed in one request. That is to say, each execution of the PHP script will execute the four INT and Shutdown events described in the second part.

. CGI/CLI Lifecycle

2. Multiprocess)

In the multi-process mode, PHP can be built into the Web Server. PHP can be compiled into the prefork MPM mode and APXS module under Apache. After Apache is started, many sub-processes will be fork, each sub-process has its own independent process address space.


. Multi-process mode Lifecycle

In a sub-process, the life cycle of PHP is to execute multiple requests (RINT/RSHUTDOWN) after the MINT is called for startup. After Apache is disabled or the process ends, will call MSHUTDOWN for the recovery phase.

. Multi-process Lifecycle

In the multi-process model, each sub-process runs independently without code or data sharing. Therefore, the termination and re-generation of a sub-process will not affect the stability of other sub-processes.

3. Multithreaded)

The Worker MPM of Apache2 adopts a multi-threaded model. Multiple Threads are created under a process and executed in the same process address space.


. Multithreading Lifecycle

4. FastCGI Mode

The Nginx + PHP-FPM we use is FastCGI mode, Fastcgi is a special CGI Mode, is a resident process type CGI, after running can Fork multiple processes, you do not need to take the time to dynamically Fork sub-processes or call MINT/MSHUTDOWN for each request. PHP manages and schedules FastCGI process pools through PHP-FPM. Nginx and PHP-FPM communicate with each other through a local TCP Socket and Unix Socket.


. FastCGI mode Lifecycle

The PHP-FPM Process Manager initializes itself and starts Multiple CGI interpreter processes to wait for requests from Nginx. When the client request reaches the PHP-FPM, the manager selects a CGI process for processing, Nginx sends the CGI Environment Variables and standard input to a PHP-CIG sub-process. After the PHP-CGI sub-process processing is complete, the standard output and error information is returned to Nginx, when the PHP-CGI sub-process closes the connection, the request processing is complete. The PHP-CGI sub-process waits for the next connection.

You can imagine the system overhead of CGI. Every Web request, PHP, must re-Parse php. ini, load all extensions, and start all data structures. With FastCGI, all of these occur only once when the process starts. In addition, continuous connections between databases and Memcache can work.

5. Embedded mode (Embedded)

Embed SAPI is a special SAPI that allows you to call functions provided by PHP in C/C ++. In the same SAPI and CLI modes, run in Module Init => Request Shutdown => Module Shutdown mode.

Embed SAPI can call a variety of PHP class libraries or implement advanced gameplay. For example, you can view php opcode (intermediate code executed by PHP, Zend engine Command, generated by PHP code ).

For details, see:

SAPI Running Mechanism

Let's take CGI as an example to look at the SAPI running mechanism.

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.