Cgiphp-cgi: underlying principles of PHP

Source: Internet
Author: User
: This article mainly introduces the principle of cgiphp-cgi and PHP underlying layer. if you are interested in PHP tutorials, please refer to it. Concept 1:

Cgi

Cgi (common gateway interface. The protocol used for interaction between a web server (such as nginx) and a script parser (such as php-cgi. It specifies the format in which data such as the request url, http header, and post data should be transmitted to the script parser, and the format in which the script parser should return http response.
Fastcgi
Fastcgi is an upgraded cgi protocol. The standard cgi will parse each request to the corresponding script configuration file (such as php. ini), load the configuration and expansion, initialize the execution environment, and the performance is very poor. Fastcgi adopts the master-worker mode and resident memory policies, improving the response speed.
Php-cgi
The fastcgi implementation of php is also used as the php script parser to run php scripts. The disadvantage is that the process management scheduling is poor.
Php-fpm

Used to manage and schedule php-cgi processes, similar to spawn-fcgi

Principle 2

First, let's take a look at the general cgi workflow:
The web server receives a user request and submits the request to the cgi program. The cgi program processes the request based on the submitted parameters, and then outputs standard html statements to the web server, the web server returns the result to the client. this is how common cgi works.
From the above, cgi only implements dynamic web pages. this processing method is characteristic that every request is received, the web server needs to fork a separate cgi program process for processing, the advantage of this method is that the web server is independent from the specific program, with a clear structure and strong controllability. The disadvantage is that if the high access requirements are met, the cgi process fork will become a huge burden on the server. imagine that hundreds of concurrent requests cause the Server fork to output hundreds of processes. This is also the reason why cgi has always suffered from poor performance and high resource consumption.
There is a solution to the problem. Currently, the popular solution is to use the module design. Basically, the current web server has a corresponding compaction mechanism to expand its functions, as long as the module is designed according to its design specifications, it can be inserted into the web server's own process, so the performance has greatly improved, such as IIS isapi and apache dso. However, this method does not have no disadvantages. for example, for different web servers, it must be developed according to different standards and cannot be unrelated to webserver; for example, this will transfer the input verification work to the web server, and there is no way to handle it freely; for example, if a problem occurs, the entire web server processing process will be affected; for example, if a web server process is inserted, the system cannot run with multiple user tags and cannot process VM permissions.
Fortunately, we have another option, fastcgi. Fastcgi is an extension based on the cgi architecture. his core idea is to establish an intelligent and sustainable middle layer between the web server and a specific cgi program to manage the running of cgi programs, in this way, the web server only needs to submit requests to this layer, which then generates several reusable cgi program instances and then distributes requests to these instances. these instances are controllable, it is sustainable and reusable. Therefore, on the one hand, it avoids the process repeatedly fork, on the other hand, it can monitor the running status of these instances through the control and detection mechanism of the middle layer, and fork or recycle instances according to different conditions, achieve both flexibility and stability.

How PHP works at the underlying layer

Http://blog.csdn.net/21aspnet/article/details/6973405

Http://www.nowamagic.net/librarys/veda/detail/102/

The above introduces the cgi php-cgi, the underlying principles of PHP, including the content, hope to be helpful to friends who are interested in PHP tutorials.

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.