In apache, PHP can be called using the php5_module module. In nginx, php-fpm is required. What are the differences between the two call methods? What are their relationships with cgi and fastcgi? In apache, PHP can be called using the php5_module module. In nginx, php-fpm is required. What are the differences between the two call methods? What are their relationships with cgi and fastcgi? Reply content: the above answers have some questions.
CGI is the protocol between HTTP Server and an independent process. It sets the HTTP Request Header as the environment variable of the process, and the HTTP Request body as the standard input of the process, the standard output of the process is that the HTTP Response includes the Header and body.
FASTCGI is similar to HTTP. It defines how to transmit multiple HTTP connections at the same time in the same TCP connection. This actually caused a problem. An HTTP connection sent a large file and refused to let out the FASTCGI connection. It would be silly for other HTTP connections in the same FASTCGI connection. So Lighttpd? Introduced X-SENDFILE.
Php-fpm is equivalent to Apache + mod_php. Php-fpm comes with FASTCGI Server, while Apache is an HTTP Server.
The WSGI has nothing to do with this problem. WSGI is only an internal Python interface. Whether you use FASTCGI, HTTP, SCGI, uWSGI, or other protocols, your FASTCGI/HTTP/SCGI/uWSGI Server calls a function in the same parameter format, in this way, the Web applications you write in Python can run behind different servers without modifying the code. The CGI protocol is inter-process, while WSGI is intra-process. Bben likes silk road too, and it's just a blow.
1. when a web server receives a browser request, if it is a static resource, it will directly return it to the browser. If it is a dynamic resource, no ready-made resources will be returned, then cgi will be available.
2. cgi can be understood as a protocol or a type of processing program, that is, to dynamically generate a file. From the program perspective, it is a process like exec on the web server, and then some input parameters are given to it, after processing the results, the system returns the results to the web server. At the protocol level, the cgi Protocol regulates the meanings of some input and output parameters of the web server and cgi program.
3. therefore, there may be many different cgi programs. Do not execute php scripts or execute python scripts. As long as they comply with these specifications, they can be called by web servers, of course, its disadvantage is that the cgi program needs to be started every time, which will make the processing very slow.
4. the improvement to this defect becomes fastcgi, which can also be understood as a protocol or a program. The difference between it and cgi is that it does not need to go to exec every time and it will be started in advance, as a cgi management server, a series of sub-processes are started in advance to wait for processing, and then wait for the requests sent by the web server. Once the requests are received, they are handled by the sub-process, as a result, it is much faster to start cgi after receiving the request.
5. phpfpm is a specific php implementation of fastcgi. After it is started, Multiple cgi sub-processes will be created, and the main process is responsible for managing sub-processes. At the same time, it provides a socket to external users, when the web server needs to forward a dynamic request, it only needs to send the data to the socket in the format required by the fastcgi protocol. Then, the child process created by phpfpm will compete for the socket connection, who grabbed who handled the request and returned the result to the web server? What did the main phpfpm process do? For example, if one of the sub-processes exits unexpectedly, phpfpm will monitor the sub-process. Once a cgi sub-process is found, another sub-process will be started, and many other management functions will be available.
6. As an independent process, phpfpm establishes a connection with nginx through socket, while mod_php is loaded into the apache server as a module and serves as the cgi scheduling manager, they manage them in different ways.
Simply put, the server can be seen as a restaurant, user requests as a dining customer, and servers can process requests as a solution to the customer's dining problem (response to the output of a meal ).
Static resources on the server are regarded as ready meals, which can be returned to the customer as long as they are put in the lunch box. Dynamic resources need to be prepared by the kitchen chef and then put in the lunch box to return to the customer.
Php_mod has a special character. It is a waste of resources no matter whether the customer wants to do what he wants.
Php_fpm, a chef with many younger siblings, is always on fire (multiple processing processes). When some customers say they want to do it now, the chef will arrange for the younger brother to make a copy and return it to the customer.
Cgi is also a chef, but he waits until the customer is ready to cook, then ignition, cooking, and flameout. Wait for the next one to be done
Fastcgi is a cook who hired a group of younger siblings to cook the meals on the scene. The chefs only need to assign tasks. The problem that the younger brother really runs the cooking pot can be discussed in two aspects:
1. Whether the PHP interpreter is embedded into the Web server for internal execution
Mod_php can only be used with Apache by embedding the PHP interpreter into the Apache process, while cgi and fast-cgi appear in the form of independent processes, as long as the corresponding Web server implements cgi or fast-cgi protocol, PHP requests can be processed.
The biggest drawback of mod_php embedding is the high memory usage, which is loaded into the memory no matter whether the PHP interpreter is used or not, typically, there is no need to load interpreters to process static files such as CSS and JS.
2. Number of requests processed by a single process
The mod_php and fast-cgi modes can process multiple requests within the lifecycle of each process, while the cgi Mode can process one request and destroy the process immediately, cgi performance is very bad in high concurrency scenarios.
In summary, if you have high performance requirements, you can separate static requests from dynamic requests. Nginx + php-fpm is a good choice.
PS: cgi and fastcgi usually refer to the protocol specification for the Web server to communicate with the interpreter, while php-fpm is an implementation of the fastcgi protocol.
CGI (Common Gateway Interface)
Initially, CGI was developed for the NCSA HTTPd Web server by the National Super Computer Application Center (NCSA) in 1993.
This Web server usesUNIX shell Environment VariablesTo save the parameters passed from the Web server, and then generate a CGI-runningIndependent Process. The first implementation of CGI is written in Perl [1].
- Low Efficiency: Each connection fork is processed by a process.
- Function is very limited: CGI can only receive one request and output one response. It is difficult to control Web requests in the CGI system, such as user authentication.
Because of these problems, many Web servers still support Web development using the API-based strong binding method for a long time after CGI was born. Apache mod_php belongs to this method. As a result, some great gods have proposed the FastCGI standard.
FastCGI (Fast Common Gateway Interface)
FastCGI uses the process/thread pool to process a series of requests. These processes/threads are managed by the FastCGI server, rather than the Web server. When a request comes in, the Web server sends the environment variable and the page request to the FastCGI process through a Socket persistent connection. FastCGI has the following advantages:
- Performance: the overhead of new processes opened by CGI is avoided through the process/thread pool.
- Compatible: it is very easy to modify the existing CGI standard programs.
- Language independence: FastCGI is a set of standards. Theoretically, any language capable of standard output (stdout) can be used as the Web backend of FastCGI standards.
The following is a simple pseudo-code for 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 common gateway interface. It is a communication protocol that enables scripting languages (such as PHP) to interact with HTTP servers.
FastCGI is an improvement on CGI. It solves some performance problems of CGI Protocol and is widely used on the PHP platform. Similar things include WSGI.
Php-fpm is an implementation of FastCGI with the process management function. CGI to see RFC3875: https://www.ietf.org/rfc/rfc3875
FastCGI: FastCGI Specification
In general, FastCGI improves the CGI performance. There are some differences in specifications, but they are not big. They are consistent with the browser communication specifications.
PHP does not understand.
PHP interpreter execution mainly involves three modes: mod_php, CGIFastCGI.
- Mode_php is a module of Apache. It embeds the PHP interpreter into the Apache process.
- CGI and FastCGI are two protocols. The Web Server implements the corresponding applications of CGI or FastCGI protocol (CGI or FastCGI), You can start the PHP interpreter to process PHP requests. They all exist in the form of independent processes.
- Mode_php and FastCGI can process multiple requests in a single process. CGI can only process one request in a single process.
Php-cgi is
CGI protocol implementation.
- Php-cgi is actually a PHP parser.
- In CGI Mode, when the Web Server receives xx/index. when a php request is sent, php-cgi is started and php-cgi is parsed. ini file, initialize the environment, process according to the request parameters, and then return the processed results. (Both are based on CGI protocol specifications)
- Php-cgi starts a process for each request and then reads php. ini for parsing. it is conceivable that the efficiency is relatively low.
- Php-cgi cannot be restarted smoothly. After modifying the php. ini configuration, the php-cgi program started later will not be aware of it.
Php-fpm
FastCGI Process Management is
FastCGI protocol implementation.
- 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 workers. When the request comes, the master will pass it to a worker and wait for the next request. Php-fpm dynamically configures the number of workers.
- A php-fpm process can process multiple requests and start multiple php-cgi programs.
- Php-fpm can achieve balanced restart. After modifying php. ini, the new configuration will be used when the new worker is enabled.
CGI, Common Gateway Interface, and general Gateway Interface. Send the user's command to the background for processing. It can be understood as a general protocol, which can standardize user instructions and facilitate subsequent processing by background programs.
FastCGI encapsulates CGI based on the protocol and communicates with external programs such as nginx. Generally, a master-slave process is available. FastCGI can manage CGI in multiple languages, such as PHP mentioned by the subject, and Python and Ruby. It can be understood as a mature CGI model.
While PHP-FPM is a good implementation of FastCGI program, and later by PHP revenue in the official project. It can be understood as the manager of FastCGI. Manage FastCGI processes stably.