Analysis of cgi, fastcgi, php-cgi, php-fpm
The content of this article is about the analysis of cgi, fastcgi, php-cgi, and php-fpm. It has a certain reference value. Now I share it with you. Friends in need can refer to it
Definition
First of all, what is CGI for? 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 you request
/index.html
, 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 now is/index.php
, according to the configuration file, nginx knows that this is not a static file and needs to find a PHP parser to process it, then it will simply process the request and hand it over to 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 users you use in your PHP code come from.When the web server receives the request
/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.
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.
cgi
Universal gateway interface, the interface standard between external programs and web servers, is the process of transferring information between cgi programs and web servers
Every request will generate a cgi process. After the cgi program is executed, the process exits
-
Independent of the server and independent programming language
FastCgi
FastCgi is like a resident Cgi, it can always execute this, as long as it is activated, no need It forks once every time and also supports distributed computing, that is, the FastCgi program can be executed on a host other than the website server and accept requests from other website servers
can handle multiple requests at the same time
Long-term memory usage
php-cgi
php official After the built-in FastCGI process manager
php.ini is modified, you must kill php-cgi and then start php.ini for it to take effect. Cannot restart smoothly
Memory cannot be allocated dynamically
php-fpm
The unofficial fastCgi process manager was officially included starting from php5.4. When compiling php, you only need –enable-fpm to enable php-fpm
Can smoothly restart php
Dynamic scheduling process
Nginx is only responsible for reverse proxy/request forwarding and is not responsible for managing the php-cgi process. Therefore, Nginx is generally used with php-fpm, which can manage the working process (child process) by itself. .
It should be noted that php-fpm is an independent SAPI, which does not manage php-cgi. In other words, php-fpm has nothing to do with php-cgi. php-fpm has a built-in php interpreter. php-fpm The child process is forked by itself and will not call php-cgi. If you delete php-cgi in the system, it will not affect the normal operation of the php-fpm service.
php-fpm is in pm = static configuration, the working process resides in the background, that is, if you configure 5 working processes pm.max_children = 5, then when the php-fpm service starts, it will automatically fork out 5 child processes and reside in the background, and will not be requested. Exit after the processing is completed, and will not exit when idle. If you use a database persistent connection in the php script, these 5 worker processes will also establish and maintain 5 persistent connections to the database to achieve processing of multiple Reuse database connection resources when making requests to avoid establishing/releasing a database connection for each request. Persistent connections can also automatically reconnect after timeout, which is completely transparent to the script in php-fpm. The script only needs to be started when Just specify to use persistent connection.
php-fpm under the pm = dynamic configuration, the working process [part] is resident in the background, that is, a certain number of resident processes are maintained. When the service is busy, more processes are forked, and some processes are automatically shut down when the service is idle. Return memory resources to the operating system. Virtual host providers should prefer this method.
In short, the PHP-FPM operating mode is similar to Apache's prefork MPM, a static and dynamic multi-process network Service.
#php54 was a relationship before, Another relationship after php54.
Before php54, php-fpm (third-party compilation) is the manager, php-cgi is the interpreter
After php54, php-fpm (official Comes with), master and pool modes. php-fpm and php-cgi have nothing to do with each other. php-fpm is both an interpreter and a manager
Reference website https://www.zhihu.com/question/55835080
https://segmentfault .com/q/1010000000256516
Related recommendations:
The above is the detailed content of Analysis of cgi, fastcgi, php-cgi, php-fpm. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use php-fpm for high-performance tuning PHP is a very popular server-side scripting language that is widely used to develop web applications and dynamic websites. However, as traffic increases, the performance of your PHP application may suffer. In order to solve this problem, we can use php-fpm (FastCGIProcessManager) for high-performance tuning. This article will introduce how to use php-fpm to improve the performance of PHP applications and provide code examples. one,

How to use PHP-FPM optimization to improve the performance of PrestaShop applications. With the rapid development of the e-commerce industry, PrestaShop has become the e-commerce platform chosen by many merchants. However, as the size of the store increases and the number of visits increases, the PrestaShop application may encounter performance bottlenecks. In order to improve the performance of the PrestaShop application, a common method is to use PHP-FPM to optimize and improve the application's processing capabilities. PHP-FPM (FastCGI

How to Improve the Performance of WooCommerce Applications Using PHP-FPM Optimization Overview WooCommerce is a very popular e-commerce plugin for creating and managing online stores on WordPress websites. However, as your store grows and traffic increases, WooCommerce apps can become slow and unstable. To solve this problem, we can use PHP-FPM to optimize and improve the performance of WooCommerce applications. What is PHP-FP

Overview of using php-fpm connection pool to improve database access performance: In web development, database access is one of the most frequent and time-consuming operations. The traditional method is to create a new database connection for each database operation and then close the connection after use. This method will cause frequent establishment and closing of database connections, increasing system overhead. In order to solve this problem, you can use php-fpm connection pool technology to improve database access performance. Principle of connection pool: Connection pool is a caching technology that combines a certain number of databases

PHP-FPM is a commonly used PHP process manager used to provide better PHP performance and stability. However, in a high-load environment, the default configuration of PHP-FPM may not meet the needs, so we need to tune it. This article will introduce the tuning method of PHP-FPM in detail and give some code examples. 1. Increase the number of processes. By default, PHP-FPM only starts a small number of processes to handle requests. In a high-load environment, we can improve the concurrency of PHP-FPM by increasing the number of processes

How to use PHP-FPM to optimize and improve the performance of Phalcon applications. Introduction: Phalcon is a high-performance PHP framework. Combining with PHP-FPM can further improve the performance of applications. This article will introduce how to use PHP-FPM to optimize the performance of Phalcon applications and provide specific code examples. 1. What is PHP-FPMPHP-FPM (PHPFastCGIProcessManager) is a PHP process independent of the web server

What is php-fpm? The following article will take you to understand php-fpm and introduce what we need to optimize when optimizing php-fpm. I hope it will be helpful to everyone!

Solution for Ubuntu without php-fpm: 1. Add the source address of PHP by executing the "sudo apt-get" command; 2. Check whether there is a php7 package; 3. Install PHP by executing the "sudo apt-get install" command; 4. , modify the configuration to listen on port 9000 to handle nginx requests; 5. Start "php7.2-fpm" through "sudo service php7.2-fpm start".
