The full name of CGI is "Common Gateway Interface", which 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 the client and the program. One of the purposes of CGI is to be independent of any language, so 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.
FastCGI is a protocol for communication between Web servers and handlers. It is an improved solution for CGI. FastCGI is like a long-live CGI. It can be executed all the time. When a request arrives, it does not take time to fork a process to handle it (this is the most criticized fork-and-execute mode of CGI). Precisely because it is just a communication protocol, it also supports distributed computing, that is, FastCGI programs can be executed on hosts other than the website server and accept requests from other website servers.
FastCGI is a language-independent, scalable architecture CGI open extension that keeps the CGI interpreter process in memory to obtain higher performance. Repeated loading of CGI programs is the main reason for low CGI performance. If CGI programs are kept in memory and accepted to be scheduled by the FastCGI process manager, it can provide good performance, scalability, Fail-Over features, etc.
Generally, the entire workflow of FastCGI is as follows:
The FastCGI process manager (IIS ISAPI or Apache Module) is loaded when the Web Server starts.
FastCGI process The manager initializes itself, starts multiple CGI interpreter processes (visible multiple php-cgi) and waits for connections from the Web Server.
When a client request arrives at the Web Server, the FastCGI process manager selects and connects to a CGI interpreter. The web server sends CGI environment variables and standard input to the FastCGI subprocess php-cgi.
After the FastCGI sub-process completes processing, it returns standard output and error information to the Web Server from the same connection. When the FastCGI child process closes the connection, the request is processed. The FastCGI child process then waits for and handles the next connection from the FastCGI process manager (running in the Web Server). In CGI mode, php-cgi exits at this point.
PHP's CGI implements the Fastcgi protocol. It is a TCP or UDP protocol server that accepts requests from the Web server. When it is started, it creates a socket listener for the TCP/UDP protocol server and receives related requests for processing. Then the life cycle of PHP is entered: module initialization, sapi initialization, processing of PHP requests, module closing, sapi closing, etc. constitute the entire CGI life cycle.
Taking TCP as an example, on the TCP server side, the following steps are generally performed:
Call the socket function to create a streaming socket for TCP;
Call the bind function to bind the local address of the server to the previously created socket;
Call the listen function to use the newly created socket as a listener, waiting for the connection initiated by the client. When the client has When multiple connections are connected to this socket, they may need to be queued;
The server process calls the accept function and enters the blocking state until a client process calls the connect function to establish a connection;
When a connection is established with the client, the server calls the read_stream function to read the client's request;
After processing the data, the server calls the write function to send a response to the client.
FastCGI for PHP enables all your PHP applications to run through mod_fastci instead of mod_phpsusexec. FastCGI applications are fast because they are durable and stable and do not have to be started and initialized for every request. This enables the development of applications that would otherwise be impractical in the CGI paradigm (such as a large script, or an application that requires connection to a single or multiple databases).
Advantages of FastCGI:
PHP scripts run faster (3 to 30 times). The PHP interpreter is loaded into memory instead of being read from memory each time it is needed, greatly improving the performance of sites that rely on scripts.
Need to use less system resources. Since the server does not have to load the PHP interpreter every time it is needed, you can increase the transmission speed of the site very much without increasing the load on the CPU.
No need to make any changes to the existing code. Everything out there works with PHP's FastCGI.
But there are also potential problems:
You only have one available php.ini file for all subdirectories (/home/USERNAME/public_html/php.ini). This is necessary to optimize your website code. If you need multiple php.ini files to accommodate different scripting needs, you can disable PHP's fast CGI in any subdirectory and leave it active elsewhere. Please contact support if you need to do this.
Any upgrades you make to your PHP environment (such as changes to the php.ini file) will have a delay of several minutes. This is because your php.ini file has been loaded into memory for faster speed, rather than being re-read from memory each time it is needed.