PHP Bottom-up principle example detailed

Source: Internet
Author: User
Tags app service php language sapi zend

Used for a long time lamp or LNMP, then how exactly is the lamp between the work, or how to connect it? Usually just write procedures, rarely think about their working principle: this article mainly and everyone to share the PHP bottom principle of the example of a detailed explanation, I hope to help everyone.

How PHP works at the bottom


Figure 1 PHP structure

As you can see from the diagram, PHP is a 4-tier system from bottom to top.

①zend engine

Zend Whole with pure C implementation, is the core part of PHP, it will be PHP code translation (lexical, grammatical parsing, such as a series of compilation process) for the execution of opcode processing and implementation of the corresponding processing methods, the implementation of basic data structures (such as Hashtable, OO), memory allocation and management, The corresponding API method is provided for external invocation, which is the core of all, and all peripheral functions are implemented around Zend.

②extensions

Around the Zend Engine, extensions provides a variety of basic services through a component-based approach, our common set of built-in functions (such as the array series), standard libraries, etc. are implemented through extension, and users can implement their own extension as needed to achieve functional expansion , performance optimization and other purposes (such as the PHP middle layer is in use, Rich text parsing is the typical application of extension).

③sapi

SAPI full name is the server application programming Interface, that is, the service-side application programming interface, SAPI through a series of hook functions, so that PHP can interact with the peripheral data, which is a very elegant and successful PHP design, By SAPI successfully decoupling PHP itself from the upper-layer application, PHP can no longer consider how to implement compatibility for different applications, and the application itself can be handled differently for its own characteristics. Later in the SAPI section, we will introduce

④ Upper Application

This is our usual PHP program, through different SAPI way to get a variety of application patterns, such as through the webserver implementation of Web applications, command line under the script to run and so on.

Structure thought:

Engine (Zend) + component (EXT) mode reduces internal coupling

Middle tier (SAPI) isolated from 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 seen as a road, and cars can run on different types of highways.

And once the PHP program is executed is the car running on the road.

So we need: engine with excellent performance + right wheels + right runway

The relationship between Apache and PHP

Apache's parsing of PHP is done through a number of module PHP module.


The eventual integration of PHP into the Apache system also requires some necessary settings for Apache. Here, we take PHP mod_php5 SAPI run mode as an example to explain, as for the concept of SAPI later we will explain in detail.

Assuming that the version we installed is Apache2 and PHP5, then you need to edit the Apache master configuration file http.conf, which includes the following lines:

Under 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 under the X system environment.

In the Windows environment:

LoadModule Php5_module D:/php/php5apache2.dll

AddType application/x-httpd-php. php

Note: Where D:/php/php5apache2.dll is the installation location of the Php5apache2.dll file in a Windows environment.

These two configurations are to tell Apache Server, in the future to receive the URL user request, usually in PHP as a suffix, you need to call the Php5_module module (mod_php5.so/php5apache2.dll) to handle.

The life cycle of Apache


Apach process of request processing

Apache Request Processing Loop detailed

What has been done in the 11 phases of the Apache request processing cycle?

1. Post-read-request Stage

In the normal request processing process, this is the first stage in which a module can insert a hook. This phase can be exploited for modules that want to enter processing requests very early.

2. URI translation phase

Apache's primary work at this stage is to map the requested URL to the local file system. The module can insert hooks at this stage to perform its own mapping logic. Mod_alias is using this phase to work.

3. Header parsing Stage

The main work of Apache at this stage: Check the header of the request. This hook is rarely used because the module can perform the task of inspecting the request header at any point in the request processing process. Mod_setenvif is using this phase to work.

4. Access Control phase

The main work of Apache at this stage is to check whether the requested resource is allowed to be accessed based on the configuration file. The standard logic of Apache implements the Allow and deny directives. Mod_authz_host is using this phase to work.

5. Authentication Stage

Apache's main work at this stage is to authenticate the user according to the policy set by the profile and set the user name area. The module can insert hooks at this stage to implement an authentication method.

6. Authorization Stage

The main work of Apache at this stage is to check whether authenticated users are allowed to perform the requested action based on the profile. The module can insert hooks at this stage to implement a user rights management approach.

7. MIME Type Checking Stage

The main work of Apache at this stage is to determine which content handler will be used, based on the 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 phase that allows the module to run any necessary process before the content generator. Similar to Post_read_request, this is a hook that captures any information and is the most commonly used hook.

9. Response Stage

The main work of Apache at this stage is to generate the content returned to the client, which is responsible for sending an appropriate reply to the client. This phase is a core part of the entire processing process.

10. Logging Stage

Apache's primary work at this stage is to log transactions after a reply has been sent to the client. The module may modify or replace the standard log records of Apache.

11. Cleanup Stage

Apache's main work at this stage is to clean up the environment left behind by the request transaction, such as file, directory processing, or socket closure, which is the last phase of Apache request processing.

Lamp Architecture:


From bottom to top four layers:

①liunx belongs to the bottom of the operating system

②apache server, belonging to secondary server, communication between Linux and PHP

③php: Belongs to the server-side programming language, via Php_module module and Apache Association

④mysql and other Web services: belong to app service, via PHP's Extensions plug-in module and MySQL Association

Android System architecture diagram

Lamp and Android architecture diagram comparison, looks like and lamp architecture a bit similar, I do not understand the Android, just feel a bit similar, the master can point out the difference, the younger brother here greatly thanks


From top down:

Android Architecture ———— – Description--–lamp Architecture

1. Application--– specific application--–web application

2. Application Framework--java ————-PHP language and libraries

3. System Runtime:--Virtual machine ——— Web server

⒋linux Kernel:-Operating system ——-lamp architecture L

With a long lamp or LNMP, how does the lamp work, or how is it connected? Usually just write programs, rarely think about how they work:

How PHP works at the bottom


Figure 1 PHP structure

As you can see from the diagram, PHP is a 4-tier system from bottom to top.

①zend engine

Zend Whole with pure C implementation, is the core part of PHP, it will be PHP code translation (lexical, grammatical parsing, such as a series of compilation process) for the execution of opcode processing and implementation of the corresponding processing methods, the implementation of basic data structures (such as Hashtable, OO), memory allocation and management, The corresponding API method is provided for external invocation, which is the core of all, and all peripheral functions are implemented around Zend.

②extensions

Around the Zend Engine, extensions provides a variety of basic services through a component-based approach, our common set of built-in functions (such as the array series), standard libraries, etc. are implemented through extension, and users can implement their own extension as needed to achieve functional expansion , performance optimization and other purposes (such as the PHP middle layer is in use, Rich text parsing is the typical application of extension).

③sapi

SAPI full name is the server application programming Interface, that is, the service-side application programming interface, SAPI through a series of hook functions, so that PHP can interact with the peripheral data, which is a very elegant and successful PHP design, By SAPI successfully decoupling PHP itself from the upper-layer application, PHP can no longer consider how to implement compatibility for different applications, and the application itself can be handled differently for its own characteristics. Later in the SAPI section, we will introduce

④ Upper Application

This is our usual PHP program, through different SAPI way to get a variety of application patterns, such as through the webserver implementation of Web applications, command line under the script to run and so on.

Structure thought:

Engine (Zend) + component (EXT) mode reduces internal coupling

Middle tier (SAPI) isolated from 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 seen as a road, and cars can run on different types of highways.

And once the PHP program is executed is the car running on the road.

So we need: engine with excellent performance + right wheels + right runway

The relationship between Apache and PHP

Apache's parsing of PHP is done through a number of module PHP module.


The eventual integration of PHP into the Apache system also requires some necessary settings for Apache. Here, we take PHP mod_php5 SAPI run mode as an example to explain, as for the concept of SAPI later we will explain in detail.

Assuming that the version we installed is Apache2 and PHP5, then you need to edit the Apache master configuration file http.conf, which includes the following lines:

Under 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 under the X system environment.

In the Windows environment:

LoadModule Php5_module D:/php/php5apache2.dll

AddType application/x-httpd-php. php

Note: Where D:/php/php5apache2.dll is the installation location of the Php5apache2.dll file in a Windows environment.

These two configurations are to tell Apache Server, in the future to receive the URL user request, usually in PHP as a suffix, you need to call the Php5_module module (mod_php5.so/php5apache2.dll) to handle.

The life cycle of Apache


Apach process of request processing

Apache Request Processing Loop detailed

What has been done in the 11 phases of the Apache request processing cycle?

1. Post-read-request Stage

In the normal request processing process, this is the first stage in which a module can insert a hook. This phase can be exploited for modules that want to enter processing requests very early.

2. URI translation phase

Apache's primary work at this stage is to map the requested URL to the local file system. The module can insert hooks at this stage to perform its own mapping logic. Mod_alias is using this phase to work.

3. Header parsing Stage

The main work of Apache at this stage: Check the header of the request. This hook is rarely used because the module can perform the task of inspecting the request header at any point in the request processing process. Mod_setenvif is using this phase to work.

4. Access Control phase

The main work of Apache at this stage is to check whether the requested resource is allowed to be accessed based on the configuration file. The standard logic of Apache implements the Allow and deny directives. Mod_authz_host is using this phase to work.

5. Authentication Stage

Apache's main work at this stage is to authenticate the user according to the policy set by the profile and set the user name area. The module can insert hooks at this stage to implement an authentication method.

6. Authorization Stage

The main work of Apache at this stage is to check whether authenticated users are allowed to perform the requested action based on the profile. The module can insert hooks at this stage to implement a user rights management approach.

7. MIME Type Checking Stage

The main work of Apache at this stage is to determine which content handler will be used, based on the 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 phase that allows the module to run any necessary process before the content generator. Similar to Post_read_request, this is a hook that captures any information and is the most commonly used hook.

9. Response Stage

The main work of Apache at this stage is to generate the content returned to the client, which is responsible for sending an appropriate reply to the client. This phase is a core part of the entire processing process.

10. Logging Stage

Apache's primary work at this stage is to log transactions after a reply has been sent to the client. The module may modify or replace the standard log records of Apache.

11. Cleanup Stage

Apache's main work at this stage is to clean up the environment left behind by the request transaction, such as file, directory processing, or socket closure, which is the last phase of Apache request processing.

Lamp Architecture:


From bottom to top four layers:

①liunx belongs to the bottom of the operating system

②apache server, belonging to secondary server, communication between Linux and PHP

③php: Belongs to the server-side programming language, via Php_module module and Apache Association

④mysql and other Web services: belong to app service, via PHP's Extensions plug-in module and MySQL Association

Android System architecture diagram

Lamp and Android architecture diagram comparison, looks like and lamp architecture a bit similar, I do not understand the Android, just feel a bit similar, the master can point out the difference, the younger brother here greatly thanks


From top down:

Android Architecture ———— – Description--–lamp Architecture

1. Application--– specific application--–web application

2. Application Framework--java ————-PHP language and libraries

3. System Runtime:--Virtual machine ——— Web server

⒋linux Kernel:-Operating system ——-lamp architecture L

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.