Understanding of CGI fast CGI PHP FPM

Time:2019-12-10

CGI

CGI, Common Gateway Interface, is a tool for HTTP server to contact with programs on other servers, which can be used into any languages with standard input, standard output and environmental variables, such as PHP, Perl, or Tcl.

FastCGI

FastCGI is a kind of CGI which is long-live, which will always be running. With FastCGI, it'll take less time t fork(which is a problem of fork-and-execute mode in CGI). In additional, FastCGI also supports for distributed computing.
It is also not language related, which is an opened extension of CGI, which is used to keep CGI running in the memory. It's well-known that loading of CGI has been the main reason of low performance.
the main process of running FastCGI:
Loading the Process Manager of FastCGI when a Web server has booted(IIS ISAPI or Apache Module)
The Process Manager of FastCGI will initiate itself to create several CGI processes, which are used to wait for connection of Web servers.
When requests from clients have reached the Web server, the Process Manager of FastCGI will select a CGI set up before to connect, whose environmental variables and standard input will be sent to the sub process php-cgi of FastCGI.
This sub process will return standard output and error info to the Web server with the same connection. Requests will be finished when it closes the connection.
Therefore, FastCGI only set once for parsing php.ini, loading extensions and initiating all the data structures.

shortcuts

Because of multi-processes, FastCGI will cost more memory than CGI, whose each process(PHP-CGI) will cost about 7Mb to 25Mb memory.
Data from the article: Nginx 0.8x + PHP 5.2.13(FastCGI) is 10 times better than Apache(Edition 6)
when 30k connection happens in parallel, 10 Nginx processes will only cost 150Mb Mem(15Mb 10), and 64 PHP-CGI will only cost about 1280Mb(20Mb 64).

PHP-CGI

PHP-CGI is one kind of the Process Manager of FastCGI, which is within php itself.
The command to boot is as follow:
php-cgi -b 127.0.0.1:9000
shortcuts

After changing php.ini, you should reboot PHP-CGI to make the new php.ini work.
When a PHP-CGI process is killed, all the PHP code will cannot run.(PHP-FPM and Spawn-FCGI do not have the same problem)

PHP-FPM

PHP-FPM is another kind of the Process Manager of FastCGI, which can be downloaded here.
It's actually a patch for PHP, which is used to integrate the Process Manager of FastCGI into PHP, which should be make into PHP before version 5.3.2.
PHP-FPM can be used to control sub processes of PHP-CGI:
/usr/local/php/sbin/php-fpm [options]

# options –start: start a fastcgi process of php –stop: force to
kill a fastcgi process of php –quit: smooth to kill a fastcgi
process of php –restart: restart a fastcgi process of php –reload:
smooth to reload php.ini –logrotate: enable log files again

Spawn-FCGI Spawn-FCGI is a general kind of the Process Manager of
FastCGI, which is one part of lighttpd.

First of all, what does CGI do? CGI is to ensure that the data transmitted by web server is standard and convenient for the compiler of CGI program.

Web server (such as nginx) is just the distributor of content. For example, if you request / index.html, then the web
The server will find the file in the file system and send it to the browser, where static data is distributed. Well, if the current request 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 he will simply process the request and give it to the PHP parser. What data will nginx send to the PHP parser? URL, query string, post data, http
Well, CGI is a protocol that specifies which data to transmit and what format to transmit to the rear to process this request. Think about where the users you use in your PHP code come from.
When the web server receives / index.php
After this request, the corresponding CGI program will be started, 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. Web
The server then returns the result to the browser.

I’ve learned that CGI is a protocol, which has nothing to do with processes. What is fastcgi? Fasecgi is used to improve the performance of CGI programs.

To improve performance, 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. The standard CGI will perform these steps for each request file (not too tired! Start the process very tired , so it takes a long time to process each request. This is obviously unreasonable! So what does fastcgi do? First, fastcgi will start a master, analyze the configuration environment, initialize the execution environment, and then start multiple workers. When the request comes, the master passes it to a worker, and then immediately accepts the next request. In this way, repeated labor is avoided, and the efficiency is naturally high. In addition, when the worker is not enough, the master can start several workers in advance according to the configuration and wait; of course, when there are too many idle workers, they will also stop some, which improves the performance and saves resources. This is fastcgi’s process management.

So what is php-fpm? It is a program that implements fastcgi, which is officially accepted by PHP.

As you all know, the interpreter of PHP is PHP CGI. PHP CGI is just a CGI program. It can only parse requests and return results by itself, without process management (emperor, I really can’t do it!) So there are some programs that can schedule PHP CGI processes, such as spawn fcgi separated from lighthttpd. Well, PHP FPM is also such a thing. After a long time of development, it has gradually been recognized by everyone (you know, in the past few years, people complained about the poor stability of PHP FPM), and it is becoming more and more popular.

Well, finally I’ll answer your question. Some people on the Internet say that fastcgi is a protocol, which is implemented by PHP FPM.

Yes

Some say that PHP FPM is the manager of fastcgi process, which is used to manage fastcgi process

Yes. The management object of PHP FPM is PHP CGI. But we can’t say that PHP FPM is the manager of fastcgi process, because we said that fastcgi is a protocol, it seems that there is no such process, even if there is PHP FPM, it can’t manage it (at least at present).

Some say that PHP FPM is a patch of PHP kernel

It used to be right. Because PHP FPM was not included in the PHP kernel at the beginning, to use this function, you need to find the same PHP FPM as the source version to patch the kernel, and then compile. Later, the PHP kernel integrated PHP FPM, which is much more convenient. You can use the — enable FPM compiling parameter.

Some say that after modifying the php.ini configuration file, there is no way to restart smoothly, so PHP FPM was born

Yes, after modifying php.ini, the PHP CGI process can’t be restarted smoothly. The processing mechanism of PHP FPM is that the new worker uses new configuration, and the existing worker can rest after handling the work on hand, so as to smooth the transition.

What’s more, PHP CGI is the fastcgi manager of PHP, so why create a PHP FPM
Incorrect. PHP CGI is just a program to interpret PHP scripts.

How to make PHP better support PHP FPM

PHP FPM provides a better way of PHP process management, can effectively control memory and process, and can smoothly overload PHP configuration. When. / configure, PHP FPM can be enabled with the – enable FPM parameter.
Modifying nginx configuration file supports PHP FPM

After the nginx installation, modify the nginx configuration file to nginx.conf
In the server section, add the following configuration, otherwise no input file specified. Error pass the PHP scripts to fastcgi server listening on 127.0.0.1:9000 will appear

location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

Start PHP FPM and nginx

/usr/local/php/sbin/php-fpm

Start mode of manual patching

/usr/local/php/sbin/php-fpm start

sudo /usr/local/nginx/nginx