Technical experts talk about the underlying working principles of PHP. Recently, when I set up a server, I suddenly felt how lamp works or how it works? I usually only write programs, but I never thought about how they work: I recently set up a server in PHP, and suddenly felt how lamp works or how it works? I usually write programs and never think about how they work:
Working Principle of PHP underlying layer
498) this. width = 498; 'onmousewheel = 'javascript: return big (this) 'style = "width: 486px; height: 310px "alt =" wps_clip_image-29471 "src =" http://www.bkjia.com/uploadfile/2013/0904/20130904095355654.png "width =" 752 "height =" 467 "/>
Php structure
From the figure, we can see that php is a layer-4 system from bottom to top.
① Zend Engine
Zend is implemented in pure c and is the kernel part of php. it translates php code (a series of compilation processes such as lexical and syntax parsing) it can process and implement corresponding processing methods, implement basic data structures (such as hashtable and oo), allocate and manage memory, and provide corresponding api methods for external calls, is the core of everything, and all peripheral functions are implemented around zend.
② Extensions
Around the zend Engine, extensions provides various basic services in a component-based manner. our common built-in functions (such as the array series) and standard libraries are implemented through extension, you can also implement your own extension as needed to achieve function expansion and performance optimization (for example, the php middle layer being used by the Post Bar and rich text parsing are typical applications of extension ).
③ Sapi
The full name of Sapi is the Server Application Programming Interface, that is, the Server Application Programming Interface. sapi enables php to interact with peripheral data through a series of hook functions, which is a very elegant and successful design of php, through sapi, php itself and upper-layer applications are successfully decoupled and isolated. php can no longer consider how to be compatible with different applications, and the application itself can implement different processing methods based on its own characteristics. This topic will be introduced later in sapi
④ Upper-layer applications
This is the php program we usually write and various application modes are obtained through different sapi methods, such as implementing web applications through webserver and running scripts under the command line.
Architecture:
Engine (Zend) + component (ext) mode reduces internal coupling
The middle layer (sapi) isolates web server and php
**************************************** **********************************
If php is a car
The car's framework is php itself.
Zend is a car engine)
The components below Ext are the wheels of a car.
Sapi can be seen as a highway, and vehicles can run on different types of roads
The execution of a php program is that a car runs on the road.
Therefore, we need an engine with excellent performance + a proper wheel + a correct runway.
Relationship between Apache and php
Apache's php parsing is done through the php Module in many modules.
498) this. width = 498; 'onmousewheel = 'javascript: return big (this) 'alt = "wps_clip_image-31721" src = "http://www.bkjia.com/uploadfile/2013/0904/20130904095355122.png" width = "378" height = "299"/>
To integrate php into Apache, you also need to make some necessary settings for Apache. Here, we will take the mod_php5 SAPI running mode of php as an example. the SAPI concept will be explained in detail later.
Assume that the installed versions are Apache2 and Php5. edit the main configuration file http. conf of Apache and add the following lines to it:
In Unix/Linux:
LoadModule php5_module modules/mod_php5.so
AddType application/x-httpd-php. php
Note: modules/mod_php5.so is the installation location of the mod_php5.so file in the X system environment.
In Windows:
LoadModule php5_module d:/php/php5apache2. dll
AddType application/x-httpd-php. php
Note: d:/php/php5apache2. dll is the installation location of the php5apache2. dll file in Windows.
These two configurations tell the Apache Server that any Url user request received later uses php as the suffix and needs to be processed by calling the php5_module module (mod_php5.so/php5apache2. dll.
Apache lifecycle
498) this. width = 498; 'onmousewheel = 'javascript: return big (this) 'style = "width: 458px; height: 260px "alt =" wps_clip_image-8490 "src =" http://www.bkjia.com/uploadfile/2013/0904/20130904095355730.png "width =" 504 "height =" 356 "/>
Apach request processing process
498) this. width = 498; 'onmousewheel = 'javascript: return big (this) 'style = "width: Pixel px; height: 398px "alt =" wps_clip_image-17917 "src =" http://www.bkjia.com/uploadfile/2013/0904/20130904095355268.png "width =" 512 "height =" 448 "/>
Detailed explanation of Apache request processing cycle
What have been done in the 11 phases of Apache request processing cycle?
1. Post-Read-Request stage
In the normal request processing process, this is the first stage in which the module can insert hooks. This phase can be used by modules that want to process requests very early.
2. URI Translation stage
Apache's main work in this phase: map the request URL to the local file system. The module can insert hooks at this stage to execute its own ing logic. Mod_alias uses this phase of work.
3. Header Parsing stage
In this phase, Apache checks the request header. This hook is rarely used because the module can execute the task of checking the request header at any point in the request processing process. Mod_setenvif uses this phase of work.
4. Access Control stage
Apache's main work in this phase: Check whether the requested resources are allowed to be accessed based on the configuration file. Apache's standard logic allows and denies commands. Mod_authz_host is used in this phase.
5. Authentication stage
Apache's main work in this phase: authenticate users according to the policies set in the configuration file, and set the user name area. The module can insert hooks at this stage to implement an authentication method.
6. Authorization stage
Apache's main work in this phase: checks whether authenticated users are allowed to perform request operations based on the configuration file. The module can insert hooks at this stage to implement a user permission management method.
7. MIME Type Checking stage
Apache's main work in this phase: determine the content processing functions to be used based on the rules related to the MIME type of the requested resource. The standard modules mod_negotiation and mod_mime implement this hook.
8. FixUp stage
This is a common stage that allows a module to run any necessary processing flow 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 stage
Apache's main work in this phase: generate the content returned from the client and send an appropriate response to the client. This stage is the core part of the entire processing process.
10. Logging stage
Apache's main work in this phase: record transactions after replying to a client. The module may modify or replace Apache standard logging.
11. CleanUp stage
The main work of Apache in this phase: clean up the environment left after the transaction processing of this request is completed, such as processing files, directories, or closing Socket, etc, this is the last stage of Apache request processing.
LAMP architecture:
498) this. width = 498; 'onmousewheel = 'javascript: return big (this) 'alt = "wps_clip_image-24435" src = "http://www.bkjia.com/uploadfile/2013/0904/20130904095355638.png" width = "399" height = "394"/>
From bottom to top 4:
① Liunx is the underlying operating system
② Apache server, which belongs to the secondary server, communicates with linux and PHP
③ Php: it belongs to the server programming language and is associated with apache through the php_module module.
④ Mysql and other web services: it is an application service. it is associated with mysql through the Extensions external modules of PHP.
Android architecture
The lamp and Android architecture diagram is similar to the lamp architecture. I don't know about Android, but I feel a little similar. experts can point out the differences. thank you very much.
498) this. width = 498; 'onmousewheel = 'javascript: return big (this) 'style = "width: 464px; height: 320px "alt =" wps_clip_image-27187 "src =" http://www.bkjia.com/uploadfile/2013/0904/20130904095355158.png "width =" 478 "height =" 344 "/>
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
Customized Linux kernel: --- operating system ------- L in lamp architecture
Original article: http://www.cnblogs.com/phphuaibei/archive/2011/09/13/2174927.html
Why? At ordinary times, I only write a program, but I never thought about how they work: PHP bottom...