What's the relationship between FastCGI and php-fpm?

Source: Internet
Author: User
Tags fpm

Objective: To ascertain the fastcgi and php-fpm of Chu

Article Source: http://segmentfault.com/q/1010000000256516

Problem:

I check the relationship between FastCGI and php-fpm on the Internet, check for a week, the basic look at the times, it is very divergent opinions, there is no authoritative definition.

Online some say, fastcgi is a protocol, PHP-FPM realize this agreement, some say, PHP-FPM is fastcgi process Manager, used to manage the fastcgi process; some say, PHP-FPM is a patch of PHP kernel; Some said that, after modifying the php.ini configuration file, there is no way to smooth restart, so the birth of php-fpm, and some say php-cgi is a PHP fastcgi manager, then why do you get a php-fpm out, I was more dizzy;

Send a paste, want to listen to everyone's understanding, on-line I have already looked at a time, because I looked for a week, haha, so want to listen to the original understanding.

Reply:

1.

At the beginning of this problem I also quite tangled, read the "HTTP authoritative guide" after the feeling clear a lot.

First of all, what does CGI do? CGI is designed to ensure that the data passed by the Web server is in a standard format and facilitates the writer of CGI programs.

Web server, such as Nginx, is simply the publisher of the content. For example, if the request /index.html , then the Web server will go to the file system to find this file, sent to the browser, here is distributed static data. Well, if the request is now /index.php , according to the configuration file, Nginx know that this is not a static file, need to find a PHP parser to deal with, then he will simply handle the request to the PHP parser. What data will the Nginx send to the PHP parser? URL to have it, query string also have to have, post data also have, HTTP header can not be less, good, CGI is to specify what data to pass, in what format to the rear processing the request of the Protocol. Think carefully about where the users you are using in your PHP code come from.

When the Web server receives /index.php this request, it launches the corresponding CGI program, which is the parser for PHP. The PHP parser then parses the php.ini file, initializes the execution environment, then processes the request, and then returns the processed result in a format specified by the CGI, exiting the process. The Web server then returns the results to the browser.

Well, CGI is a protocol, it doesn't matter what the process is. What is that fastcgi? FastCGI is used to improve the performance of CGI programs.

Improve performance, what is the performance problem with CGI programs? "The PHP parser parses the php.ini file, initializes the execution environment," and here it is. The standard CGI performs these steps for each request (no idle fatigue!). Start the process very tired to say! ), so it takes longer to process each time. This is obviously unreasonable! So how did fastcgi do it? First, fastcgi initiates a master, parses the configuration file, initializes the execution environment, and then starts multiple workers. When the request comes in, master passes it to a worker, and then immediately accepts the next request. This avoids duplication of labor, and efficiency is naturally high. And when the worker is not enough, master can pre-boot several workers according to the configuration and so on, of course, the idle worker too much, will also stop some, this improves the performance, also saves the resources. This is FastCGI's management of the process.

What is that php-fpm? is a realization of the FASTCGI program, by the PHP official received.

As we all know, PHP's interpreter is php-cgi. PHP-CGI is just a CGI program, he himself can only parse the request, return the results, will not process management (the emperor, my concubine really do not AH!) So there are some programs that can dispatch php-cgi processes, such as spawn-fcgi separated by LIGHTHTTPD. Well php-fpm is such a thing, after a long period of development, gradually got everyone's approval (to know, in the previous few years everyone complained php-fpm stability is too poor), but also more and more popular.

Well, finally come back to your question.
Online some say that fastcgi is a protocol that PHP-FPM implemented this Protocol

Right.

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

Right. The management object of PHP-FPM is php-cgi. But it cannot be said that PHP-FPM is the manager of the fastcgi process, because the fastcgi is a protocol, it seems that no such process exists, even if there is php-fpm can not manage him (at least for now). Some say that php-fpm is a patch for the PHP kernel

Used to be right. Because PHP-FPM is not included in the PHP kernel at the beginning, to use this function, we need to find the same version of the source code php-fpm the kernel patch, and then compile. Later, after the PHP kernel integrates php-fpm, it is much easier to use --enalbe-fpm this compilation parameter.

Some said that after modifying the php.ini configuration file, there is no way to smooth the restart, so the birth of PHP-FPM

Yes, after modifying php.ini, the php-cgi process does not have the means to smooth the restart. PHP-FPM This mechanism is the new worker with a new configuration, the already existing workers can rest after processing the hands of the work, through this mechanism to smooth over.

Also say php-cgi is PHP comes with the fastcgi manager, then why do you get a php-fpm out

Wrong. PHP-CGI is just a program that explains PHP scripts.

    • Link
    • 18 Reviews
    • More
1Reply Yinguan:

Very good answer, but the resolution of the last question of @ Yinguan is disputed. Php-cgi and PHP-FPM, as well as a fastcgi process manager, php-cgi the problem is 1, php-cgi change php.ini configuration need to restart php-cgi to allow the new Php-ini to take effect, can not smooth restart 2, Directly kill the php-cgi process, PHP will not be able to run. (PHP-FPM and spawn-fcgi do not have this problem, the daemon will smooth the new child process from the newly generated.) In the light of PHP-CGI's shortcomings, PHP-FPM emerged. Reference Link: http://www.mike.org.cn/articles/what-is-cgi-fastcgi-php-fpm-spawn-fcgi/

#1 Ajaxhe April 28, 2014 · Reply

1Reply to Evian:

Oh, thank you for your praise. In Segmentfault's first ' step ' and ' likes ' are you give, haha.

#2 Yinguan · July 30, 2013 · Reply

Answer 2:

You (PHP) go to the Eskimo (Web server, like Apache, Nginx) to talk about business

You speak Chinese (PHP code), he said Eskimo language (c code), do not understand each other, how to do? Then turn your words into English (FastCGI protocol).

How to convert it? You're going to use a translator (PHP-FPM)
(Of course, the other side also has a translation machine, that is his own)

Our translation machine is the newest, the old-fashioned one (php-cgi) was eliminated. But it (PHP-FPM) only the young (Linux system) will be used, the old Men (Windows system) will not play with it, but continue to use the old-fashioned

Answer 3:

  

I read the answers to the top three, but I always thought they were not well described.

FastCGI is an upgraded version of CGI, a language-agnostic protocol used to communicate programs (such as PHP, Python, Java) and Web servers (Apache2, Nginx), and in theory any program written in any language can provide Web services through fastcgi.
FastCGI is characterized by a process in which multiple requests are completed sequentially to achieve efficiency gains, and most FASTCGI implementations maintain a process pool.

And PHP-FPM is an implementation for PHP, FASTCGI, which manages a process pool to handle requests from the Web server. Currently, PHP-FPM is built into PHP.

But PHP-FPM is just a "PHP Fastcgi process Manager" that still invokes the PHP interpreter itself to handle the request, and the PHP interpreter (under Windows) is Php-cgi.exe.

Answer 4:

  

CGI is a interface which tells the webserver how to passes data back and forth to and from an application. More specifically, it describes how to request information is passed in environment variables (such as request type, remote I P address), how the Reqeust body is passed on via standard input, and how the response are passed out via standard output.

A protocol for communication between a Fastcgi:web server and a handler is an improved CGI scheme.

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.

FastCGI is a resident CGI, which can be run all the time, and will not take the hours to fork a process to handle when the request arrives.

FastCGI is a language-independent, extensible architecture for CGI open extensions that keep the CGI interpreter process in memory for high performance.

In general, the whole workflow of fastcgi is this:

1. Load the FASTCGI Process Manager (IIS ISAPI or Apache Module) when Web server starts

2, the FASTCGI process manager itself initializes, starts multiple CGI interpreter processes (visible multiple php-cgi) and waits for webserver connections.

3. When a client request arrives at 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.

4. After the fastcgi process finishes processing, the standard output and error information are returned from the same connection to the Web Server. 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.

Answer 5:

  

FASTCGI is the communication protocol between app server and Web server. Normal Architecture App server is Master,web server is the client

PHP-FPM with two functions: 1. A server program that supports the FASTCGI protocol 2 is implemented. Process Manager

With PHP-FPM, you can turn PHP scripts into multi-process mode , App server with fastcgi protocol , and Web server for communication

You can refer to the 9 methods of deployment of the Python Web program in this text

What's the relationship between FastCGI and php-fpm?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.