Home > Article > Backend Development > What is the difference between cgi and fcgi in php
The difference between cgi and fcgi in php: cgi is the abbreviation of "common gateway interface", which refers to the common gateway interface, while fcgi is the abbreviation of "fast common gateway interface", which refers to the fast common gateway interface . "php-cgi" is the implementation of the CGI protocol, and "PHP-FPM" is the implementation of the FCGI protocol.
The operating environment of this article: Windows 10 system, PHP version 8.1, Dell G3 computer
CGI:common gateway interface Common gateway interface
FCGI:fast common gateway interface Fast common gateway interface
php-cgi is the implementation of CGI protocol, PHP-FPM is the implementation of FCGI protocol .
CGI is the protocol interface for communication between web servers such as nginx and back-end dynamic scripting languages such as PHP.
Expand knowledge
1. Scenario:
The browser requests the web server For example, if nginx requests static index.html, nginx can return it directly. But if the request is idex.php, nginx finds that it is not a static resource according to the configuration, and needs to request the back-end service. So, when nginx requests the back-end service, in what format and what data does it need to send to the back-end service? CGI is a protocol that defines formats and data.
2. Disadvantages of the CGI protocol:
The CGI protocol must start the program that implements the CGI protocol every time it requests a back-end service. Here is the PHP parser. The PHP parser will parse the PHP.ini file, perform initialization work, process requests, and return results. Each request must be reinitialized, which is too redundant work. Therefore, FCGI came into being.
Because PHP is an interpreted language, an interpreter is needed to interpret the PHP code.
3. Advantages of FCGI:
The FCGI protocol first starts a master, which is used to parse the PHP.ini file and perform initialization work. Start multiple workers, and workers are used to process specific requests. This separates initialization and processing specific requests. This also requires a program to manage the master and workers, so PHP-FPM appeared.
FCGI adopts C/S architecture. Separate the web server from the script parsing server. Make the web server only process static resources and return the data returned by the back-end script parsing server to the browser.
The web server nginx does not support direct calling or parsing of external programs such as PHP programs, so it must be called through the CGI interface. This actually requires encapsulation of CGI to form a wrapper, which is actually PHP-FPM. nginx calls PHP-FPM, PHP-FPM then calls the back-end PHP code, and then PHP-FPM returns the result to nginx.
Now PHP-FPM has been integrated into the PHP kernel.
PHP-FPM and nginx are often deployed on different servers to reduce the pressure on front-end nginx to process static resources and forward dynamic requests.
And FCGI is safer than CGI.
4. Implementation of CGI protocol PHP-CGI
If the php.ini file is modified, PHP-CGI cannot transition smoothly. That is, the PHP-CGI program must be restarted to use these new configurations.
5. Implementation of FCGI protocol PHP-FPM
If the php.ini file is modified, PHP-FPM can achieve a smooth transition. For the worker before modification, after processing the request, it will be destroyed, and the newly generated worker can use the new configuration. This also achieves a smooth transition.
Moreover, if the PHP-CGI process hangs, PHP will not be able to run. PHP-FPM does not have this problem, the daemon will smoothly restart a child process.
Before PHP-FPM appeared, there was actually spawn-fcgi, which was developed from lighthttpd. PHP-FPM can create multiple workers during initialization, and when a worker is idle, the worker can be recycled.
spawn-fcgi may cause memory leaks or even automatically restart FCGI when concurrency is high.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What is the difference between cgi and fcgi in php. For more information, please follow other related articles on the PHP Chinese website!