Home  >  Article  >  Backend Development  >  Analysis of cgi, fastcgi, php-cgi, php-fpm

Analysis of cgi, fastcgi, php-cgi, php-fpm

不言
不言Original
2018-04-20 10:52:451477browse

The content of this article is about the analysis of cgi, fastcgi, php-cgi, and php-fpm. It has a certain reference value. Now I share it with you. Friends in need can refer to it

Definition


First of all, what is CGI for? CGI is to ensure that the data passed by the web server is in a standard format, which is convenient for writers of CGI programs.

The web server (such as nginx) is just a distributor of content. For example, if you request /index.html, the web server will find this file in the file system and send it to the browser. What is distributed here is static data. Okay, if the request now is /index.php, according to the configuration file, nginx knows that this is not a static file and needs to find a PHP parser to process it, then it will simply process the request and hand it over to PHP parser. What data will Nginx pass to the PHP parser? The URL must be present, the query string must be present, the POST data must be present, and the HTTP header must be present. Well, CGI is the protocol that stipulates what data is to be transmitted and in what format to be passed to the backend for processing the request. Think carefully about where the users you use in your PHP code come from.

When the web server receives the request /index.php, it will start the corresponding CGI program, which is the PHP parser. Next, the PHP parser will parse the php.ini file, initialize the execution environment, process the request, return the processed result in the format specified by CGI, and exit the process. The web server then returns the results to the browser.

Okay, CGI is a protocol and has nothing to do with processes or anything like that. So what is fastcgi? Fastcgi is used to improve the performance of CGI programs.

Improve performance, so what are the performance problems of CGI programs? "The PHP parser will parse the php.ini file and initialize the execution environment", that's it. Standard CGI will perform these steps for each request (don't be tired! Starting the process is very tiring!), so the time to process each time will be relatively long. This is obviously unreasonable! So how does Fastcgi do it? First, Fastcgi will start a master, parse the configuration file, initialize the execution environment, and then start multiple workers. When a request comes in, the master passes it to a worker and can immediately accept the next request. This avoids duplication of work and is naturally highly efficient. And when there are not enough workers, the master can pre-start several workers according to the configuration and wait; of course, when there are too many idle workers, some will be stopped, which improves performance and saves resources. This is fastcgi's process management.


cgi

  • Universal gateway interface, the interface standard between external programs and web servers, is the process of transferring information between cgi programs and web servers

  • Every request will generate a cgi process. After the cgi program is executed, the process exits

  • Independent of the server and independent programming language

FastCgi

  • FastCgi is like a resident Cgi, it can always execute this, as long as it is activated, no need It forks once every time and also supports distributed computing, that is, the FastCgi program can be executed on a host other than the website server and accept requests from other website servers

  • can handle multiple requests at the same time

  • Long-term memory usage

php-cgi

  • php official After the built-in FastCGI process manager

  • php.ini is modified, you must kill php-cgi and then start php.ini for it to take effect. Cannot restart smoothly

  • Memory cannot be allocated dynamically

php-fpm

  • The unofficial fastCgi process manager was officially included starting from php5.4. When compiling php, you only need –enable-fpm to enable php-fpm

  • Can smoothly restart php

  • Dynamic scheduling process




Nginx is only responsible for reverse proxy/request forwarding and is not responsible for managing the php-cgi process. Therefore, Nginx is generally used with php-fpm, which can manage the working process (child process) by itself. .
It should be noted that php-fpm is an independent SAPI, which does not manage php-cgi. In other words, php-fpm has nothing to do with php-cgi. php-fpm has a built-in php interpreter. php-fpm The child process is forked by itself and will not call php-cgi. If you delete php-cgi in the system, it will not affect the normal operation of the php-fpm service.

php-fpm is in pm = static configuration, the working process resides in the background, that is, if you configure 5 working processes pm.max_children = 5, then when the php-fpm service starts, it will automatically fork out 5 child processes and reside in the background, and will not be requested. Exit after the processing is completed, and will not exit when idle. If you use a database persistent connection in the php script, these 5 worker processes will also establish and maintain 5 persistent connections to the database to achieve processing of multiple Reuse database connection resources when making requests to avoid establishing/releasing a database connection for each request. Persistent connections can also automatically reconnect after timeout, which is completely transparent to the script in php-fpm. The script only needs to be started when Just specify to use persistent connection.

php-fpm under the pm = dynamic configuration, the working process [part] is resident in the background, that is, a certain number of resident processes are maintained. When the service is busy, more processes are forked, and some processes are automatically shut down when the service is idle. Return memory resources to the operating system. Virtual host providers should prefer this method.

In short, the PHP-FPM operating mode is similar to Apache's prefork MPM, a static and dynamic multi-process network Service.




##php-cgi is an early fastcgi manager produced by PHP official and is not supported For smooth restart, if you change php.ini, you must kill the original php-cgi and restart it to take effect; dynamic worker scheduling is not supported, and you can only specify how many workers to start at the beginning.

php-fpm is a fastcgi process manager added since 5.3.3. It has added a dynamic scheduling function, which can dynamically increase or decrease the number of worker processes according to changes in request pressure; it supports the reload command to allow the worker process to complete Restarts after the current request and applies the new php.ini configuration.





#php54 was a relationship before, Another relationship after php54.


Before php54, php-fpm (third-party compilation) is the manager, php-cgi is the interpreter

After php54, php-fpm (official Comes with), master and pool modes. php-fpm and php-cgi have nothing to do with each other. php-fpm is both an interpreter and a manager

Reference website https://www.zhihu.com/question/55835080



https://segmentfault .com/q/1010000000256516


Related recommendations:

Illustration of the relationship between CGI, FastCGI and PHP-FPM

linux view Directory where nginx, apache, php, php-fpm, mysql and configuration items are located

The above is the detailed content of Analysis of cgi, fastcgi, php-cgi, php-fpm. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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