The CGI full name is the Universal Gateway Interface (Common Gateway Interface), which allows a client to request data from a Web browser to a program that executes on a Web server. CGI describes a standard for transferring data between the client and the program. One purpose 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 and so on.
FastCGI is a protocol for communication between Web servers and handlers, an improvement to CGI, fastcgi like a resident (long-live) CGI, which can be executed all the time and will not take a moment to fork a process to handle when the request arrives ( This is the most criticized Fork-and-execute model of CGI). Because he is just a communication protocol, it also supports distributed operations where the FastCGI program can execute and accept requests from other Web servers on hosts other than the Web server.
FastCGI is a language-independent, extensible architecture for CGI open extensions that keep the CGI interpreter process in memory for high performance. CGI program loading is the main reason for the poor CGI performance, if the CGI program is kept in memory and accepted fastcgi process manager scheduling, it can provide good performance, scalability, Fail-over features and so on.
In general, the whole workflow of fastcgi is this:
- Load fastcgi Process Manager (IIS ISAPI or Apache Module) when Web server starts
- The FASTCGI process Manager itself initializes, starts multiple CGI interpreter processes (visible multiple php-cgi) and waits for a connection from the Web server.
- When a client request arrives at the Web server, the FASTCGI process manager selects and connects to a CGI interpreter. WEB server sends CGI environment variables and standard input to the FASTCGI child process php-cgi.
- The FASTCGI child process returns standard output and error information from the same connection to the Web Server after processing is complete. When the fastcgi child process closes the connection, the request is processed to completion. The fastcgi child process then waits and processes the next connection from the FASTCGI process Manager (running in Web server). In CGI mode, php-cgi exits here.
PHP's CGI implements the FASTCGI protocol, which is a TCP or UDP protocol server that accepts requests from a Web server when it starts to create a TCP/UDP protocol on the server's socket listener and receives the relevant request for processing. It then enters the life cycle of PHP: module initialization, SAPI initialization, handling PHP requests, module closing, SAPI closing, etc., which make up the entire CGI life cycle.
In the case of TCP, for example, on the server side of TCP, the following steps are typically performed:
- Call the socket function to create a streaming socket for TCP;
- Call the BIND function to bind the server's local address to the socket created earlier;
- Call the Listen function to listen for the newly created socket and wait for the client-initiated connection, which may need to be queued when the client has multiple connections to the socket;
- The server process calls the Accept function to enter a blocking state until a client process calls the Connect function to establish a connection;
- When the connection is created with the client, the server calls the Read_stream function to read the client's request;
- When the data is processed, the server calls the Write function to send an answer to the client.
PHP fastcgi makes all your PHP applications run through MOD_FASTCI, not mod_phpsusexec. FastCGI applications are fast because they are stable and do not have to be started and initialized for every request. This makes the application development possible, otherwise the CGI paradigm is impractical (such as a large script, or an application that needs to connect to a single database or multiple databases).
Advantages of fastcgi:
- PHP scripts run more quickly (3 to 30 times times). The PHP interpreter is loaded into memory without having to read from memory every time it is needed, greatly improving the performance of the site that relies on the script to run.
- Fewer system resources are required. Because the server does not need to load PHP interpreter every time, you can increase the transfer speed of the site without increasing the CPU burden.
- There is no need to make any changes to the existing code. All that's available is for PHP fastcgi.
But there are also potential problems:
- For all subdirectories (/home/username/public_html/php.ini) you have only one available php.ini file. This is necessary to optimize the site code. If you need multiple php.ini files to accommodate different scripting needs, you can disable PHP's fast CGI in any subdirectory, while the rest of the places continue to work. If you need to do this please contact support.
- Any upgrades you make to the PHP environment, such as changes to the php.ini file, have a few minutes of delay. This is because your php.ini files have been loaded into memory for faster speeds, rather than being re-read from memory every time you need them.
Excerpt from: http://www.nowamagic.net/librarys/veda/detail/1321
PHP's fastcgi