1024programmer PHP Thoroughly understand the operating principles of cgi, fastcgi, phpcgi, phpfpm and php in php

Thoroughly understand the operating principles of cgi, fastcgi, phpcgi, phpfpm and php in php

1.CGI

The earliest web servers only processed static html files. With the continuous development of technology, websites become more and more complex, and then dynamic websites appear, but the service cannot directly run dynamic language files such as php and asp, and then cgi appears, which is just an interface protocol .

The full name of CGI (Common Gateway Interface) is “Common Gateway Interface“, which is passed between the Web server and external applications (CGI programs). Information interface standard. Through the CGI interface, the Web server can obtain the information submitted by the client, and transfer it to the CGI program on the server side for processing, and finally return the result to the client. CGI can be written in any language, as long as the language has standard input, output, and environment variables. Such as php, perl, tcl, etc.

The web server receives the request, and then forks a new process according to the content. This process will return the processed data to the web server, and finally the web server sends the content to the user. The fork process just now follows After exiting, if the user also requests to change the dynamic script next time, the web server will fork a new process again.

For example, if /index.html is requested, the Web Server will find this file in the file system and send it to the browser, where static data is distributed.

If the current request is /index.php, according to the configuration file, Web Server knows that this is not a static file and needs to go to the PHP parser to process it, then he will simply process the request and hand it over to PHP parser. What data will the WEB server pass to the PHP parser? URL, query string, POST data, HTTP headerThere will be,So, CGI is to specify which data to pass, to What format is passed to the protocol that handles this request. When the Web Server receives the request of /index.php, it will start the corresponding CGI program, which is the parser of PHP. Next, the PHP parser will parse the php.ini file, initialize the execution environment, then process the request, return the processed result in the format specified by CGI, and exit the process. The web server then returns the result to the browser.

In other words, CGI is actually an interface standard. The CGI we usually refer to refers to the CGI program, that is, the program that implements the CGI interface standard.

The benefits of CGI: completely independent of any server, just as an intermediate molecule. Provide interface to Web Server and php. They use CGI to wire to complete the data transfer. The advantage of this is to minimize the association of the two and make them more independent.

Deficiencies of CGI: Every web request will have a startup and exit process, which is the most criticized fork-and-execute mode, so Once under large-scale concurrency, it will die.

2. FastCGI

Fastcgi is an upgraded version of cgi, which is used to improve the performance of CGI programs (also a protocol).

FastCGI is like a resident (long-live) CGI, it can be executed all the time, as long as it is activated, it will not take time to fork every time. It also supports distributed computing, that is, the FastCGI program can be executed on a host other than the web server and accept requests from other web servers.

FastCGI is an open CGI extension with a language-independent, scalable architecture. Its main behavior is to keep the CGI interpreter process in memory and thus obtain higher performance. As we all know, the repeated loading of the CGI interpreter is the main reason for the poor performance of CGI.

FastCGI is a process management tool that can manage CGI processes in memory.

FastCGI process manager needs to be started separately. After starting FastCGI, a FastCGI main process and multiple sub-processes will be generated (sub-processes are actually CGI interpreter processes).

When the client requests the dynamic script on the Web server, the Web server will pass the dynamic script to the FastCGI main process through the TCP protocol, and the FastCGI main process will arrange an idle sub-process to parse the dynamic script according to the situation After the processing is completed, the result is returned to the Web server, and the Web server returns the result to the client. After the client request is processed, the FastCGI child process will not be closed, but will continue to wait for the main process to arrange work tasks.

FastCGI benefits: The work efficiency is very high.

Deficiencies of FastCGI: Because it is multi-process, it consumes more server memory than CGI multi-threading. The PHP-CGI interpreter consumes 7 to 25 megabytes of memory per process, which will This number multiplied by 50 or 100 is a very large memory number.

3.PHP-CGI

PHP-CGI is the FastCGI manager that comes with PHP. It is a program that implements the CGI protocol and is used to interpret PHP scripts.

Shortcomings of PHP-CGI:

After php-cgi changes php.ini configuration, you need to restart php-cgi to make the new php-ini take effect. Graceful restart is not possible.

If you kill the php-cgi process directly, php will not be able to run. (PHP-FPM and Spawn-FCGI do not have this problem, and the daemon process will smoothly regenerate new child processes.)

4.PHP-FPM

PHP-FPM is a PHP FastCGI manager, only for PHP. It is a program that implements the FastCGI protocol.

That is to say, PHP-FPM is for the FastCGI protocolThe specific implementation of �, it is responsible for managing a process pool to handle requests from the Web server. Currently, after PHP5.3, PHP-FPM is built into PHP.

Because PHP-CGI is just a CGI program, it can only parse the request itself, return the result, and will not manage the process, so there are some programs that can schedule the PHP-CGI process, such as lighthttpd Separated spawn-fcgi. Similarly, PHP-FPM is also a management program for scheduling and managing the PHP parser PHP-CGI.

PHP-FPM can achieve a smooth restart after php.ini modification by generating a new child process.

5. PHP operating principle

PHP is a back-end language born for the web, so it must rely on a web server to provide web functions. Of course, if other back-end languages ​​are used as web applications, they must also rely on web servers. Common web servers include: Apache, nginx, IIS, lighttpd, tomcat. As a phper, the most familiar ones are of course Apache and nginx.

So how do PHP and web servers hook up? Two modes

1) mod_php mode

Taking Apache as an example, in the PHP Module mode, should you add Here are a few sentences:

Apache is the one we use the most. So recall, how can Apache recognize the php code? Do you add or modify the following sentences in the Apache configuration file httpd.conf:

//Add the following 2 sentences
LoadModule php5_module D:/php/php5apache2_2.dll
AddType application/x-httpd-php .php
//The following
DirectoryIndex index.html

//Modify it to:
DirectoryIndex index.html index.htm index.php index.phtml

Manual configuration after installing php and Apache environment under windows above , the source code installation under linux is roughly configured like this:

./configure --with-mysql=/usr/local --with-apache=/usr/local/apache --enable-track-vars

In this way, their common essence is to use LoadModule to load php5_module, which is Run php as a submodule of Apache. When accessing php files through the web, Apache will call php5_module to parse the php code.

So how does php5_module pass the data to the php parser to parse the php code? The answer is through sapi.

From the above picture, We can see that sapi is such an intermediate process. SAPI provides an interface for external communication, which is somewhat similar to socket, so that PHP can exchange data with other applications (apache, nginx, etc.). PHP provides many kinds of SAPI by default, such as php5_module, CGI, and FastCGI for apache and nginx, ISAPI for IIS, and CLI for Shell.

So, the above process of apache calling php is as follows:

apache -> httpd -> php5_module -> sapi -> php

OK. Let’s figure out apache and php through php5_module! We call this mode of operation mod_php mode.

This mode installs the php module into apache, so every time apache ends a request, a process will be generated, which completely includes various calculations and calculations of php.

In the picture above, we can clearly see that every time apache receives a request, it will generate a process to connect php to complete the request through sapi. It is conceivable that if there are too many users, If the number of concurrency is too much, the server will not be able to bear it.

Moreover, when mod_php is compiled into apache, it is difficult to locate the problem of php or apache when there is a problem.

2) mod_fastcgi mode

The above also mentioned sapi, sapi is a unified interface provided by php, which provides php5_module and cgi are used by the web server to link and parse the php code. The php5_module loading mode mentioned above is called the mod_php mode.

mod_fastcgi mode is just the opposite, fastcgi is an independent entity independent of apache and php, it starts together with apache, generates multiple cig modules, and waits for apache’s request:

In the figure, fastcgi has started early, quietly Where is waiting, the httpd request sent by apache will be received immediately, and the calculation will be completed by calling sapi to php. And won’t quit. In this way, large-scale concurrent requests can be dealt with, because the web server has fewer things to do, so it can process the next request faster, so the concurrency is greatly increased.

Because apache and php are independent. If there is a problem, it is easy to locate where the problem is. This is one of the reasons why this model is popular.

PHP-CGI is the built-in FastCGI manager implemented by PHP. Although it is an official php product, but this one is not powerful at all, the performance is too poor, and it is also very troublesome and inhumane, mainly reflected in:

  1. php- After cgi changes the php.ini configuration, you need to restart php-cgi to make the new php-ini take effect, and smooth restart is not possible.
  2. If you kill the php-cgi process directly, php will not be able to run.

The above two questions have been making many people sick for a long time, so many people are still using the Module method. Until 2004, a dick named Andrei Nigmatulin invented PHP-FPM. The appearance of this artifact completely broke this situation. This is a fastcgi manager dedicated to PHP. It overcomes the above two problems very coolly, and, It also showed stronger performance in other aspects.

3) Summary:

Finally, let’s summarize, directly above the picture:

So, if you want to build a high-performance PHP WEB server, the best way is Apache/Nginx + FastCGI + PHP-FPM(+PHP-CGI), don’t use Module loading or CGI anymore:)

Reference:

CGI、FastCGI和PHP-FPM关系图解

https://www.zybuluo.com/phper/note/50231

CGI、FastCGI和PHP-FPM关系图解

Ineffective, graceful restart is not possible.

  • If you kill the php-cgi process directly, php will not be able to run.
  • The above two questions have been making many people sick for a long time, so many people are still using the Module method. Until 2004, a dick named Andrei Nigmatulin invented PHP-FPM. The appearance of this artifact completely broke this situation. This is a fastcgi manager dedicated to PHP. It overcomes the above two problems very coolly, and, It also showed stronger performance in other aspects.

    3) Summary:

    Finally, let’s summarize, directly above the picture:

    So, if you want to build a high-performance PHP WEB server, the best way is Apache/Nginx + FastCGI + PHP-FPM(+PHP-CGI), don’t use Module loading or CGI anymore:)

    Reference:

    CGI、FastCGI和PHP-FPM关系图解

    https://www.zybuluo.com/phper/note/50231

    CGI、FastCGI和PHP-FPM关系图解

    This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/39840

    author: admin

    Previous article
    Next article

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Contact Us

    Contact us

    181-3619-1160

    Online consultation: QQ交谈

    E-mail: [email protected]

    Working hours: Monday to Friday, 9:00-17:30, holidays off

    Follow wechat
    Scan wechat and follow us

    Scan wechat and follow us

    Follow Weibo
    Back to top
    首页
    微信
    电话
    搜索