Home >Backend Development >PHP Tutorial >Detailed explanation of PHP underlying principles examples
I have used LAMP or LNMP for a long time, so how do lamps work or how are they connected? I usually just write programs and rarely think about their working principles: This article mainly shares with you the bottom layer of PHP Detailed explanation of the principle and examples, I hope it can help everyone.
The underlying working principle of PHP
Figure 1 PHP structure
As can be seen from the figure, PHP is a 4-layer system from bottom to top
①Zend Engine
Zend is implemented entirely in pure C and is the core part of PHP. It translates PHP code (lexical, syntax analysis and other compilation processes) into executable opcode processing and implementation Corresponding processing methods, implementing basic data structures (such as hashtable, oo), memory allocation and management, and providing corresponding api methods for external calls are the core of everything. All peripheral functions are implemented around zend.
②Extensions
Around the zend engine, extensions provide various basic services in a component-based manner. Our common various built-in functions (such as array series), standard libraries, etc. are all through extensions To achieve this, users can also implement their own extensions as needed to achieve function expansion, performance optimization and other purposes (for example, the PHP middle layer and rich text parsing currently used by Tieba are typical applications of extensions).
③Sapi
The full name of Sapi is Server Application Programming Interface, which is the server application programming interface. Sapi allows PHP to interact with peripheral data through a series of hook functions. This is PHP's very elegant and A successful design successfully decouples and isolates PHP itself from upper-layer applications through SAPI. PHP can no longer consider how to be compatible with different applications, and the application itself can also implement different processing methods according to its own characteristics. We will introduce it later in the sapi chapter
④Upper-layer application
This is the PHP program we usually write. We can obtain various application modes through different sapi methods, such as implementing web through webserver Apply, run as a script from the command line, etc.
Architectural idea:
The engine (Zend) + component (ext) model reduces internal coupling
The middle layer (sapi) isolates the web server and PHP
If php is a car, then
The framework of the car is php itself
Zend is the engine (engine) of the car
The various components below Ext It is the wheel of a car
Sapi can be regarded as a road, and cars can run on different types of roads
And the execution of a php program means that the car runs on the road.
Therefore, we need: excellent performance engine + suitable wheels + correct runway
The relationship between Apache and php
Apache parses php through many Modules This is done using the php Module in it.
To finally integrate php into the Apache system, you need to make some necessary settings for Apache. Here, we will take the mod_php5 SAPI operating mode of php as an example to explain. As for the concept of SAPI, we will explain it in detail later.
Assume that the versions we install are Apache2 and Php5, then you need to edit Apache’s main configuration file http.conf and add the following lines to it:
In Unix/Linux environment:
LoadModule php5_module modules/mod_php5.so
AddType application/x-httpd-php .php
Note: modules/mod_php5.so is mod_php5.so in the X system environment The installation location of the file.
In Windows environment:
LoadModule php5_module d:/php/php5apache2.dll
AddType application/x-httpd-php .php
Note: Among them, d:/php/php5apache2.dll is the installation location of the php5apache2.dll file in the Windows environment.
These two configurations tell Apache Server that any URL user request received in the future with php as the suffix needs to be processed by calling the php5_module module (mod_php5.so/ php5apache2.dll).
Apache’s life cycle
Apache’s request processing process
Apache request processing cycle detailed explanation
What are done in the 11 stages of the Apache request processing cycle?
1. Post-Read-Request phase
In the normal request processing process, this is the first stage where the module can insert hooks a stage. This stage can be exploited by modules that want to get into processing requests very early.
2. URI Translation Phase
Apache’s main job in this phase is to map the requested URL to the local file system. Modules can insert hooks at this stage to execute their own mapping logic. mod_alias uses this phase to work.
3. Header Parsing phase
Apache’s main job in this phase is to check the header of the request. Since the module 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 phase to work.
4. Access Control stage
Apache’s main job at this stage: Check whether access to the requested resource is allowed 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 users 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 phase
The main work of Apache in this phase is to check whether authenticated users are allowed to perform the requested operation according to the configuration file. The module can insert hooks at this stage to implement a user rights management method.
7. MIME Type Checking Phase
The main work of Apache in this phase is to determine the content processing function to be used based on 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 general stage that allows the module to run any necessary processing before the content generator. Similar to Post_Read_Request, this is a hook that can capture any information and is also the most commonly used hook.
9. Response phase
The main work of Apache in this phase is to generate content returned to the client and be responsible for sending an appropriate reply to the client. This stage is the core part of the entire process.
10. Logging phase
The main work of Apache in this phase: recording transactions after the reply has been sent to the client. Modules may modify or replace Apache's standard logging.
11. CleanUp Phase
The main work of Apache in this phase: clean up the environment left after the completion of this request transaction, such as the processing of files and directories or the closing of Socket, etc. This It is the last stage of a request processing by Apache.
LAMP architecture:
Four layers from bottom to top:
①liunx belongs to the bottom layer of the operating system
②apache server, belongs to Secondary server, communicates with Linux and PHP mysql association
Android system architecture diagram
Compare the architecture diagram of lamp and Android. It seems to be somewhat similar to the lamp architecture. I don’t understand Android, but it feels a bit similar. Experts can point out the difference. Thank you very much
From top to bottom:
Android architecture——————Explanation————LAMP architecture
1. Application Program——Specific application——web application
2. Application framework——java————PHP language and library
3. System runtime library:——Virtual machine ——WEB Server
⒋Linux Kernel: —Operating System——-L
in the lamp architecture has been using LAMP or LNMP for a long time, so how does the lamp work, or How are they connected? I usually just write programs and rarely think about the working principles between them:
PHP underlying working principle
Figure 1 PHP structure
As can be seen from the picture, PHP is a 4-layer system from bottom to top
①Zend engine
Zend is implemented in pure C and is the core part of PHP. It will Code translation (a series of compilation processes such as lexical and syntax analysis) processes executable opcodes and implements corresponding processing methods, implements basic data structures (such as hashtable, oo), memory allocation and management, and provides corresponding API methods. For external calls, it is the core of everything, and all peripheral functions are implemented around zend.
②Extensions
Around the zend engine, extensions provide various basic services in a component-based manner. Our common various built-in functions (such as array series), standard libraries, etc. are all through extensions To achieve this, users can also implement their own extensions as needed to achieve function expansion, performance optimization and other purposes (for example, the PHP middle layer and rich text parsing currently used by Tieba are typical applications of extensions).
③Sapi
The full name of Sapi is Server Application Programming Interface, which is the server application programming interface. Sapi allows PHP to interact with peripheral data through a series of hook functions. This is PHP's very elegant and A successful design successfully decouples and isolates PHP itself from upper-layer applications through SAPI. PHP can no longer consider how to be compatible with different applications, and the application itself can also implement different processing methods according to its own characteristics. We will introduce it later in the sapi chapter
④Upper-layer application
This is the PHP program we usually write. We can obtain various application modes through different sapi methods, such as implementing web through webserver Apply, run as a script from the command line, etc.
Architectural idea:
The engine (Zend) + component (ext) model reduces internal coupling
The middle layer (sapi) isolates the web server and PHP
If php is a car, thenThe framework of the car is php itself
Zend is the engine of the car
The various components below Ext are the wheels of the car
Sapi can be seen as Highways, cars can run on different types of roads
And the execution of a php program means the car runs on the road.
Therefore, we need: excellent performance engine + suitable wheels + correct runway
The relationship between Apache and php
Apache parses php through many Modules This is done using the php Module in it.
To finally integrate php into the Apache system, you need to make some necessary settings for Apache. Here, we will take the mod_php5 SAPI operating mode of php as an example to explain. As for the concept of SAPI, we will explain it in detail later.
Assume that the versions we install are Apache2 and Php5, then you need to edit Apache’s main configuration file http.conf and add the following lines to it:
In Unix/Linux environment:
LoadModule php5_module modules/mod_php5.so
AddType application/x-httpd-php .php
Note: modules/mod_php5.so is mod_php5.so in the X system environment The installation location of the file.
In Windows environment:
LoadModule php5_module d:/php/php5apache2.dll
AddType application/x-httpd-php .php
Note: Among them, d:/php/php5apache2.dll is the installation location of the php5apache2.dll file in the Windows environment.
These two configurations tell Apache Server that any Url user requests received in the future, with php as the suffix, need to call the php5_module module (mod_php5.so/php5apache2.dll) for processing.
Apache’s life cycle
Apache’s request processing process
Apache request processing cycle detailed explanation
What are done in the 11 stages of the Apache request processing cycle?
1. Post-Read-Request phase
In the normal request processing process, this is the first stage where the module can insert hooks a stage. This stage can be exploited by modules that want to get into processing requests very early.
2. URI Translation Phase
Apache’s main job in this phase is to map the requested URL to the local file system. Modules can insert hooks at this stage to execute their own mapping logic. mod_alias uses this phase to work.
3. Header Parsing phase
Apache’s main job in this phase is to check the header of the request. Since the module 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 phase to work.
4. Access Control Phase
The main work of Apache in this phase is to check whether access to the requested resources is allowed 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 users 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 phase
The main work of Apache in this phase is to check whether authenticated users are allowed to perform the requested operation according to the configuration file. The module can insert hooks at this stage to implement a user rights management method.
7. MIME Type Checking Phase
The main work of Apache in this phase is to determine the content processing function to be used based on 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 general stage that allows the module to run any necessary processing before the content generator. Similar to Post_Read_Request, this is a hook that can capture any information and is also the most commonly used hook.
9. Response phase
The main work of Apache in this phase is to generate content returned to the client and be responsible for sending an appropriate reply to the client. This stage is the core part of the entire process.
10. Logging phase
The main work of Apache in this phase: recording transactions after the reply has been sent to the client. Modules may modify or replace Apache's standard logging.
11. CleanUp Phase
The main work of Apache in this phase: clean up the environment left after the completion of this request transaction, such as the processing of files and directories or the closing of Socket, etc. This It is the last stage of a request processing by Apache.
LAMP architecture:
Four layers from bottom to top:
①liunx belongs to the bottom layer of the operating system
②apache server, belongs to Secondary server, communicates between Linux and PHP
③php: It is a server-side programming language and is associated with apache through the php_module module
④mysql and other web services: belong to application services, and are associated with mysql through PHP's Extensions plug-in module
Android system architecture diagram
Compare lamp with Android's architecture diagram, it seems to be similar to lamp The architecture is somewhat similar. I don’t understand Android, but it feels a bit similar. Experts can point out the differences. I would be very grateful.
From top to bottom:
Android architecture ——————Description——–LAMP architecture
1. Application——–Specific application——–Web application
2. Application framework——java————- PHP language and library
3. System runtime library: --Virtual machine --WEB server
⒋Linux kernel: --Operating system ---L
## in lamp architecture #Related recommendations:Explanation of the underlying operating mechanism and principles of PHP
#PHP underlying analysis: About copy-on-write cow cow plural win color forum ww7349cow Japan cow Cow Milk Stone
In-depth understanding of the underlying mechanism of PHP_PHP Tutorial
The above is the detailed content of Detailed explanation of PHP underlying principles examples. For more information, please follow other related articles on the PHP Chinese website!