How to explain the relationship between CGI, FastCGI and php-fpm in layman's way?

Source: Internet
Author: User
Keywords Php fastcgi apache module nginx
In Apache, you can call PHP as a php5_module module, and in Nginx, you need to call through PHP-FPM, what is the difference between the two methods of invocation? Besides, what do they have to do with cgi,fastcgi?

Reply content:

The answer above is somewhat problematic.

CGI is the protocol between HTTP server and a separate process that sets the header of the HTTP request to the environment variable of the process, the body of the HTTP request is set to the standard input of the process, and the standard output of the process is the HTTP Response includes header and text.

FastCGI is a concept similar to the HTTP protocol. It's all about how to pass multiple HTTP connections in the same TCP connection. This actually caused a problem, there is an HTTP connection to send a large file will not let fastcgi connection, in the same fastcgi connection in the other HTTP connection is silly. So lighttpd? introduced the X-sendfile.

PHP-FPM is the equivalent of apache+mod_php. Nothing but php-fpm comes with fastcgi server, and Apache is HTTP server.

That Wsgi has nothing to do with the question. Wsgi This is just an interface within Python. Whether you are in front of FASTCGI,HTTP,SCGI,UWSGI and other protocols, your FASTCGI/HTTP/SCGI/UWSGI server calls a function in the same parameter format, so that your Web app written in Python doesn't need to modify the code, Can run behind a different server. The CGI protocol is the process, and WSGI is in process. B Ben's dick is passing by and blowing it up.

1. When a Web server accepts a request from a browser, it returns it directly to the browser if it is a static resource, and if it is a dynamic resource then there is no ready-made resource to return.

2. CGI can be understood as a protocol or a class of handlers, that is, dynamic to generate files, from the program to understand is the Web server exec such a process, and then give him some input parameters, he slowly finished the results back to the Web server, That is, from the protocol level, the CGI protocol is the meaning of some of the input and output parameters that govern Web servers and CGI programs.

3. So there can be a lot of different CGI programs, do not execute the PHP script or can execute the Python script, as long as the compliance with such specifications can be called by the Web server, of course, the disadvantage is that every time you need to start the CGI program, which will make processing slow

4. To improve on this defect is fastcgi, the same can be understood as a protocol or a program, it is different from the CGI is not required every time to exec, it will start up, as a CGI Management Server exists, pre-start a series of sub-processes to wait for processing, Then wait for the request sent by the Web server, and once the request is accepted, it is processed by the child process, which is much faster because it does not need to start the CGI after accepting the request.

5.PHPFPM is a concrete implementation of PHP for FASTCGI, it will create a number of CGI sub-processes after the start, and then the main process is responsible for managing the child process, and it provides a socket externally, That Web server when you want to forward a dynamic request only need to follow the format required by the FASTCGI protocol to send data to this socket, that PHPFPM created by the child process to scramble for the socket connection, who grabbed who processed and returned the results to the Web server, What did the PHPFPM main process do? Let's say one of the sub-processes exits unexpectedly, and the PHPFPM will monitor him. Once a CGI subprocess is discovered, another one is started, and many other management functions

6 PHPFPM as a standalone process exists through the socket to establish a connection with Nginx, while mod_php is loaded into the Apache server as a module, and both of them as CGI dispatch managers, they are not the same way to manage it

That's all there is to it. The popular can think of the server as a restaurant, the user request as a dining customer, the server processing requests as a solution to the customer's meal problem (response to output a meal).

Static resources on the server as a well-done meal, as long as the food box can be returned to the customer, dynamic resources need kitchen chef ready to put into the box to return to the customer.

Php_mod This chef has a feature, see customers in the door on the ignition, whether customers want to do now, a bit of waste of resources

PHP_FPM This chef has a lot of younger brother has been burning (multiple processing process), and so the customer said to do, the chef will arrange the younger brother to return to the customer

CGI is also a chef, but he waits until the customer has to do it, he fires, cooks, and then stalls. Waiting for the next thing to come

FastCGI is a big chef hired a bunch of younger brother, specifically do need to do on-site cooking, chef just assign tasks, little brother really cooking this problem can be divided into two levels of discussion:

1. Whether the PHP interpreter is embedded in the WEB server process internal execution

Mod_php can only be used in conjunction with Apache by embedding the PHP interpreter into the Apache process, while CGI and fast-cgi appear as separate processes, as long as the corresponding Web server implements the CGI or fast-cgi protocol to handle PHP Please.

mod_php This embedded way the biggest drawback is that the memory occupies a large, regardless of whether the PHP interpreter will be loaded into memory, typically processing CSS, JS and other static files is absolutely no need to load the interpreter.

2. Number of requests processed by a single process

Mod_php and fast-cgi are capable of processing multiple requests during the lifetime of each process, while CGI mode processes a request to destroy the process immediately, and CGI performance is very poor in high concurrency scenarios.

In the comprehensive, if the performance has a high demand, can be static request and dynamic request separate, then Nginx + php-fpm is a better choice.

ps:cgi, fastcgi usually refers to the protocol specification of the WEB server communicating with the interpreter, and PHP-FPM is an implementation of the FASTCGI protocol.

CGI (Common Gateway Interface)

Initially, CGI was developed in 1993 by the National Supercomputer Application Center (NCSA) for the NCSA HTTPd Web server.

The Web server uses the UNIX shell environment variable to hold parameters passed from the Web server, and then generates a separate process to run the CGI. The first implementation of CGI was written by Perl [1].

    • Inefficient: Each connected fork is processed by one process.
    • Very limited functionality: CGI receives only one request and outputs a response. It is difficult to control Web requests in the CGI system, for example: User authentication.

Because of these problems, for a long time after the birth of CGI, various Web servers still use the strong binding way of API to support web development, in which Apache's mod_php is this way. So there's a great God behind the fastcgi standard.

FastCGI (Fast Common Gateway Interface)

FASTCGI uses a process/thread pool to process a series of requests. These processes/threads are managed by the FASTCGI server, not the Web server. When a request comes in, the Web server passes the environment variable and the page request through a long socket connection to the fastcgi process. So fastcgi has the following advantages:

    • Performance: Bypass the overhead of CGI opening up new processes through the process/thread pool.
    • Compatible: Very easy to retrofit existing CGI standard programs.
    • Language-Independent: FastCGI is a set of standards, theoretically speaking, as long as the standard output (STDOUT) language can be used as the FASTCGI standard Web backend.
      The following is a pseudo-code for a simple fastcgi backend
void main(void){int count = 0;  while(FCGI_Accept() >= 0) {    printf(“Content-type: text/html\r\n”);    printf(“\r\n”);    printf(“Hello world!\r\n”);    printf(“Request number %d.”, count++);  }exit(0);}
CGI Chinese Translation is a universal Gateway interface, which is a communication protocol that enables scripting languages (such as PHP) to interact with HTTP Server.

FastCGI is an improvement to CGI, which solves some performance problems of CGI protocol and is widely used in PHP platform. There are similar things and WSGI.

PHP-FPM is an implementation of fastcgi, which comes with the function of process management. CGI See RFC3875: https://www. ietf.org/rfc/rfc3875
FastCGI Look: FastCGI specification
In general, FASTCGI is a performance improvement on CGI, but there are some differences in specifications, which are consistent with the browser's communication specifications.
PHP does not understand.

PHP interpreter implementation, there are three main mode, mod_php, CGI, FastCGI.

    • Mode_php is a module of Apache that embeds the PHP interpreter in the Apache process.
    • CGI and fastcgi are respectively a protocol. Web Server implements the appropriate application for the CGI or FASTCGI Protocol ( hereinafter referred to as CGI or fastcgi), enabling the PHP interpreter to process PHP requests. They all exist in the form of independent processes.
    • Mode_php and FASTCGI can handle multiple requests in a single process, and CGI can handle only one request in a single process.


PHP-CGI is a implementation of CGI protocol
    • php-cgi is actually a PHP parser .
    • When in CGI mode, when Web Server receives a xx/index.php request, php-cgi,php-cgi will parse the php.ini file, initialize the environment, process it according to the request parameters, and return the processed results. (All in the CGI protocol specification)
    • PHP-CGI starts a process at each request, and then reads the php.ini to parse it, which can be imagined to be relatively inefficient.
    • PHP-CGI cannot achieve a smooth restart. After you modify the php.ini configuration, the PHP-CGI program that is started later is still not aware.


php-fpm That is, fastcgi Process Management, is a implementation of the FastCGI protocol
    • When the request arrives, PHP-FPM starts and reads the php.ini file to complete the initialization environment, then starts a master, and then starts multiple worker. When the request comes in, master passes to a worker and waits for the next request. PHP-FPM dynamically configures the number of worker.
    • A PHP-FPM process can process multiple requests and start multiple php-cgi programs.
    • The PHP-FPM can achieve a balanced restart. After you modify php.ini, the new configuration is used when the new worker is enabled.
Cgi,common Gateway Interface, general purpose gateways interface. The user's instructions are submitted to the background for processing. Can be understood as a common protocol, the ability to standardize user directives, to facilitate the subsequent processing of background procedures.

FASTCGI CGI is packaged according to protocol and communicates with external programs such as Nginx. Generally has a master-slave process. FASTCGI can manage CGI in multiple languages, compared to PHP as mentioned by title, plus Python, Ruby, and more. Can be understood as a mature model of CGI.

And PHP-FPM is a good implementation of the FASTCGI program, and was later in the official PHP revenue project. A manager that can be understood as fastcgi. Manage the fastcgi process in a stable manner.
  • 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.