Explain the execution principle and process of PHP in detail

Introduction

  Let's take a look at the following process: 
  • We have never manually started the PHP-related process, it runs with the startup of Apache; 
  • PHP is connected to Apache through the mod_php5.so module (specifically, SAPI, the server application programming interface); 
  • PHP has a total of three modules: kernel, Zend engine, and extension layer; 
  • PHP kernel is used to process requests, file streams, error handling and other related operations; 
  • Zend Engine (ZE) is used to convert source files into machine language, and then run it on a virtual machine; 
  • The extension layer is a set of functions, class libraries, and streams that PHP uses to perform some specific operations. For example, we need the mysql extension to connect to the MySQL database; 
  • When ZE executes the program, it may need to connect to several extensions, then ZE gives control to the extension, and returns it after processing a specific task; 
  • Finally, ZE runs the program The result is returned to the PHP kernel, which transmits the result to the SAPI layer and finally outputs it to the browser. 
  Drilling down 
  and more, it's not that simple. The above process is just an abridged version, let's dig a little deeper and see what's going on behind the scenes. 
  • After Apache is started, the PHP interpreter is also started; 
  • The PHP startup process has two steps; 
  • The first step is to initialize some environment variables, which will take effect throughout the SAPI life cycle; 
  • The second step is to generate Some variable settings for the current request.

The first step to start PHP

  Not sure what the first and second steps are? Don't worry, we'll discuss it in detail next. Let's look at the first, and most important, step first. The thing to remember is that the first action happens before any request arrives. 
  • After Apache is started, the PHP interpreter is also started; 
  • PHP calls the MINIT method of each extension to switch these extensions to a usable state. See what extensions are enabled in the php.ini file; 
  • MINIT means "module initialization". Each module defines a set of functions, class libraries, etc. to handle other requests. 
  A typical MINIT method is as follows:

PHP_MINIT_FUNCTION(extension_name){  
/* Initialize functions, classes etc */  
}
  • 1
  • 2
  • 3

The second step of PHP startup

  • When a page request occurs, the SAPI layer transfers control to the PHP layer. So PHP sets the environment variables needed to reply to this request. At the same time, it also establishes a variable table to store the variable names and values ​​generated during the execution. 
  • PHP calls the RINIT method of each module, ie "request initialization". A classic example is the RINIT of the Session module. If the Session module is enabled in php.ini, the $_SESSION variable will be initialized when the RINIT of the module is called, and the relevant content will be read in; 
  • The RINIT method can be seen as A preparation process that starts automatically between program executions. 
  A typical RINIT method is as follows:

PHP_RINIT_FUNCTION(extension_name) {  
/* Initialize session variables, pre-populate variables, redefine global variables etc */  
}  
  • 1
  • 2
  • 3

PHP close first step

Like PHP startup, PHP shuts down in two steps: 
  • Once the page finishes executing (either at the end of the file or aborted with an exit or die function), PHP starts a cleanup routine. It calls the RSHUTDOWN method of each module in sequence. 
  • RSHUTDOWN is used to clear the symbol table generated when the program is running, that is, to call the unset function for each variable. 
  A typical RSHUTDOWN method is as follows:

PHP_RSHUTDOWN_FUNCTION(extension_name) {  
/* Do memory management, unset all variables used in the last PHP call etc */  
}  
  • 1
  • 2
  • 3

PHP close second step

Finally, all requests have been processed, SAPI is ready to close, and PHP begins the second step: 
  • PHP calls each extension's MSHUTDOWN method, which is the last chance for each module to free memory. 
  A typical RSHUTDOWN method is as follows:

PHP_MSHUTDOWN_FUNCTION(extension_name) {  
/* Free handlers and persistent memory etc */  
}  
  • 1
  • 2
  • 3

In this way, the entire PHP life cycle is over. It's important to note that the "start first step" and "shut down second step" will only be performed if there is no request from the server. 
  Below are some illustrations to illustrate!

The underlying working principle of PHP

php bottom layer

  As can be seen from the figure, php is a 4-layer system from bottom to top  ① Zend
  engine 
  Zend is implemented in pure C as a whole, which is the core part of php. Execute opcode processing and implement corresponding processing methods, implement basic data structures (such as hashtable, oo), memory allocation and management, and provide corresponding api methods for external calls, which are the core of everything, and all peripheral functions revolve around zend implementation. 
  ②Extensions 
  revolves around the zend engine. Extensions provide various basic services in a component-based way. Our common built-in functions (such as array series), standard libraries, etc. are all implemented through extensions. Users can also implement their own extensions according to their needs. In order to achieve the purpose of function expansion and performance optimization (for example, the php middle layer and rich text parsing that Tieba is using are typical applications of extension). 
  ③Sapi The 
  full name of Sapi is Server Application Programming Interface, which is the server application programming interface. Sapi enables php to interact with peripheral data through a series of hook functions. This is a very elegant and successful design of php. Through sapi, php itself is successfully integrated. Decoupled and isolated from the upper-layer application, PHP can no longer consider how to be compatible with different applications, and the application itself can implement different processing methods according to its own characteristics. Later, we will introduce it in the sapi chapter. 
  ④ The upper-layer application 
  is the php program we usually write. Various application modes are obtained through different sapi methods, such as implementing web applications through webserver, running in script mode on the command line, etc. .

Architectural ideas:

The mode of engine (Zend) + component (ext) reduces internal coupling 
  and the middle layer (sapi) isolates web server and php 
  **************************** **************************************** 
  If php is a car, then 
  the frame of the car is PHP itself 
  Zend is the engine of the car (engine) 
  The various components under Ext are the wheels of the car. 
  Sapi can be regarded as a road, the car can run on different types of roads, 
  and the execution of a php program is that the car runs on the road. 
  So we need: a good engine + the right wheels + the right track

The relationship between Apache and php

Apache's parsing of php is done through the php Module in many Modules.

diagram

  To finally integrate php into the Apache system, you also need to make some necessary settings for Apache. Here, we will take the mod_php5 SAPI operation mode of php as an example to explain. As for the concept of SAPI, we will explain it in detail later. 
  Assuming that the versions we installed are Apache2 and Php5, then we need to edit Apache's main configuration file http.conf and add the following lines: In 
  Unix/Linux environment: 
  LoadModule php5_module modules/mod_php5.so 
  AddType application/x-httpd -php .php 
  Note: where modules/mod_php5.so is the installation location of the mod_php5.so file in the X system environment. 
  In Windows environment: 
  LoadModule php5_module d:/php/php5apache2.dll 
  AddType application/x-httpd-php .php 
  Note: d:/php/php5apache2.dll is the installation location of php5apache2.dll file in Windows environment. 
  These two configurations are to tell the Apache Server that the php5_module module (mod_php5.so/php5apache2.dll) needs to be called for processing of Url user requests received in the future with php as the suffix.

Apache life cycle

diagram

Apache's request processing flow

diagram

Apache request processing loop in detail

 What do the 11 stages of the Apache request processing cycle do? 
  1. Post-Read-Request stage 
  In the normal request processing flow, this is the first stage where the module can insert hooks. This stage can be exploited for modules that want to get into processing requests very early. 
  2. URI Translation stage 
  The main work of Apache in this stage is to map the requested URL to the local file system. Modules can insert hooks at this stage to perform their own mapping logic. mod_alias uses this stage to work. 
  3. Header Parsing stage 
  The main work of Apache in this stage is to check the header of the request. Since modules can perform the task of checking request headers at any point in the request processing flow, this hook is rarely used. mod_setenvif uses this stage to work. 
  4. 
  The main work of Apache in this stage is to check whether the requested resource is allowed to be accessed according to the configuration file. Apache's standard logic implements allow and deny directives. mod_authz_host uses this phase to work. 
  5. Authentication stage 
  The main work of Apache in this stage is to authenticate the user according to the policy set in the configuration file, and set the user name area. Modules can insert hooks at this stage to implement an authentication method. 
  6. Authorization stage 
  The main work of Apache in this stage is to check whether the authenticated user is allowed to perform the requested operation according to the configuration file. Modules can insert hooks at this stage to implement a method for user rights management. 
  7. MIME Type Checking stage 
  The main work of Apache at this stage is to determine the content processing function to be used according to the relevant rules of the MIME type of the requested resource. The standard modules mod_negotiation and mod_mime implement this hook. 
  8. FixUp stage 
  This is a generic stage that allows the module to run any necessary processing before the content generator. Similar to Post_Read_Request, this is a hook capable of capturing any information and is the most commonly used hook. 
  9. Response stage 
  The main work of Apache in this stage is to generate the content returned to the client, and is responsible for sending an appropriate reply to the client. This stage is the core part of the overall processing flow. 
  10. Logging stage 
  The main work of Apache in this stage is to record the transaction after the reply has been sent to the client. Modules may modify or replace Apache's standard logging. 
  11. CleanUp stage 
  The main work of Apache in this stage: clean up the environment left after the completion of the request transaction, such as file and directory processing or Socket closing, etc. This is the last stage of Apache's request processing.

LAMP Architecture

lamp

 Four layers from bottom to top: 
  ①liunx belongs to the bottom layer of the operating system 
  ②apache server, belongs to the secondary server, communicates linux and PHP 
  ③php: belongs to the server-side programming language, associated with apache through the php_module module 
  ④mysql and other web services: belongs to the application service, through PHP The Extensions plug-in module is associated with mysql

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324660222&siteId=291194637