Home > Backend Development > PHP Tutorial > Technical experts talk about the underlying working principle of PHP_PHP tutorial

Technical experts talk about the underlying working principle of PHP_PHP tutorial

WBOY
Release: 2016-07-20 10:58:02
Original
829 people have browsed it

I recently built a server, and suddenly I felt how the lamps work, or how they are connected? I usually just write programs, and I have never thought about the working principle of them:

The underlying working principle of PHP

Technical experts talk about the underlying working principle of PHP_PHP tutorial

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 (a series of compilation processes such as lexical and syntax analysis) into executable opcode processing and implements corresponding processing methods and implementations. It provides 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

Revolving around the zend engine, extensions provide various basic services in a component-based manner, including our common built-in functions (such as the array series), standards Libraries, etc. are all implemented through extensions. 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-side application programming interface. Sapi allows 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 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 also implement different processing according to its own characteristics. Way. We will introduce the upper layer application

④ in the sapi chapter later

This is the PHP program we usually write, and we can get various application modes through different sapi methods , such as implementing web applications through webserver, running them in script mode on the command line, etc.

Architectural ideas:

The engine (Zend) + component (ext) model reduces internal coupling

The middle layer (sapi) isolates the web server and php

************************************************ *******************************

If php was a car, then

The 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 regarded as a road. 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.

Technical experts talk about the underlying working principle of PHP_PHP tutorial

To finally integrate php into the Apache system, you still need 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:

Unix/Linux environment Next:

LoadModule php5_module modules/mod_php5.so

AddType application/x-httpd-php .php

Note: where modules/mod_php5.so is X The installation location of the mod_php5.so file in the 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 the php5apache2.dll file in Windows environment.

These two configurations tell the 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.

The life cycle of Apache

Technical experts talk about the underlying working principle of PHP_PHP tutorial

Apach's request processing flow

Technical experts talk about the underlying working principle of PHP_PHP tutorial

Apache request processing loop 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 a module that can be inserted The first stage of the hook. This stage can be exploited for 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

Apache’s main work in this phase: 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 phase

Apache’s main work in this phase: 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

Apache’s main job in this phase is to check whether authenticated users are allowed to perform the requested operation based on the configuration file. The module can insert hooks at this stage to implement a user rights management method.

7. MIME Type Checking Phase

Apache’s main work in this phase: 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 phase

This is a general phase 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

Apache’s main job 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

Apache’s main job 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

Apache’s main work 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:

wps_clip_image-24435

From below Four levels up:

①liunx belongs to the bottom layer of the operating system

②apache server, which is a secondary server and communicates with Linux and PHP

③php: belongs to the server-side programming language, through php_module The module is associated with apache

④mysql and other web services: it is an application service and is associated with mysql through PHP's Extensions plug-in module

Android system architecture diagram

Lamp and Android architecture Comparing the pictures, it seems to be somewhat similar to the lamp architecture. I don’t know Android, but it feels a bit similar. Experts can point out the differences. I would be very grateful

Technical experts talk about the underlying working principle of PHP_PHP tutorial

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 Original text: http:// www.cnblogs.com/phphuaibei/archive/2011/09/13/2174927.html


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445719.htmlTechArticleI recently built a server and suddenly felt how the lamps work or how they are connected? I usually just write Program, I have never thought about the working principle between them: PHP base...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template