Home >Backend Development >PHP Problem >What does cgi mean in php

What does cgi mean in php

WBOY
WBOYOriginal
2022-02-14 10:58:535605browse

In PHP, cgi is the abbreviation of "Common Gateway Interface", which means public gateway interface. cgi allows a client to request data from a web browser to a program executing on a network server. It is a description A standard for transferring data between servers and request handlers.

What does cgi mean in php

The operating environment of this tutorial: windows10 system, PHP7.1 version, DELL G3 computer

What does cgi mean in php

Simply speaking, CGI and FastCGI are just a protocol, php-cgi is a program that implements cgi, and php-fpm is just a manager for managing php-cgi.

The full name of CGI is "Common Gateway Interface" (Common Gateway Interface). It is a tool for the HTTP server to "talk" to your program or that of other machines. The program must run on the network server.

is an important Internet technology that allows a client to request data from a web browser to a program executing on a web server.

CGI describes a standard for transferring data between servers and request handlers.

What does CGI do? 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 /index.html is requested, 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 is now for /index.php, according to the configuration file, nginx knows that this is not a static file and needs to be processed by the PHP parser, then it will simply process the request and hand it over to the 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 user data you use in your PHP code comes from.

When the web server receives the request for /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.

FastCGI is developed and improved from CGI. The main disadvantage of the traditional CGI interface method is poor performance, because every time the HTTP server encounters a dynamic program, the script parser needs to be restarted to perform parsing, and then the results are returned to the HTTP server.

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.

So what is PHP-FPM? It is a program that implements Fastcgi and was officially accepted by PHP.

As we all know, the interpreter of PHP is php-cgi. php-cgi is just a CGI program. It can only parse requests and return results, but does not know how to manage processes (Your Majesty, I really can’t do that!) So there are some programs that can schedule php-cgi processes. For example, spawn-fcgi is separated from lighthttpd. Well, PHP-FPM is also such a thing. After a long period of development, it has gradually been recognized by everyone and has become more and more popular.

PHP-CGI is just a program that interprets PHP scripts.

PHP-FPM is a FastCGI manager

CGI mode operating principle

When Nginx receives the browser/index.php request, First, a process corresponding to the implementation of the CGI protocol will be created, here is php-cgi (PHP parser). Next, php-cgi 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. Finally, Nginx returns the results to the browser. The whole process is a Fork-And-Execute mode. When the number of user requests is very large, a large amount of system resources such as memory and CPU time will be occupied, resulting in low performance. Therefore, under a CGI server, there will be as many CGI sub-processes as there are connection requests. Repeated loading of sub-processes is the main reason for low CGI performance.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What does cgi mean in php. 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