Programmer-cannot figure out the relationship between FastCgi and PHP-fpm

Source: Internet
Author: User
I checked the relationship between fastcgi and php-fpm on the Internet for nearly a week. I basically read it all over again. There are different opinions and there is no authoritative definition. Some on the Internet say that fastcgi is a protocol, and php-fpm implements this Protocol; some say that php-fpm is the manager of the fastcgi process, which is used to manage... I checked the relationship between fastcgi and php-fpm on the Internet for nearly a week. I basically read it all over again. There are different opinions and there is no authoritative definition.

Some on the Internet say that fastcgi is a protocol, and php-fpm implements this Protocol; some say that php-fpm is the manager of the fastcgi process to manage fastcgi processes; some say, php-fpm is a patch of the php kernel. Some say that php has been modified. ini configuration file, there is no way to smooth restart, so the birth of the php-fpm; also said that the PHP-CGI is the FastCGI manager of PHP, then why do you get a php-fpm out, I will be dizzy;

I posted a post and want to hear your understanding. I have read it all over the Internet, because I have been checking it for a week, haha, so I want to hear the original understanding.

Reply content:

I checked the relationship between fastcgi and php-fpm on the Internet for nearly a week. I basically read it all over again. There are different opinions and there is no authoritative definition.

Some on the Internet say that fastcgi is a protocol, and php-fpm implements this Protocol; some say that php-fpm is the manager of the fastcgi process to manage fastcgi processes; some say, php-fpm is a patch of the php kernel. Some say that php has been modified. ini configuration file, there is no way to smooth restart, so the birth of the php-fpm; also said that the PHP-CGI is the FastCGI manager of PHP, then why do you get a php-fpm out, I will be dizzy;

I posted a post and want to hear your understanding. I have read it all over the Internet, because I have been checking it for a week, haha, so I want to hear the original understanding.

At the beginning, I was quite confused about this issue. After reading the HTTP authoritative guide, I felt a lot clearer.

First, what does CGI do? CGI is used to ensure that the data transmitted by the web server is in the standard format, so that the compilation of CGI programs is convenient.

Web server (such as nginx) is only the content distributor. For example, if the request/index.htmlThe web server will find the file in the file system and send it to the browser. static data is distributed here. Okay, if the request is/index.phpAccording to the configuration file, nginx knows that this is not a static file and needs to be handled by the PHP parser. Then nginx will simply process this request and hand it over to the PHP parser. What data will Nginx transmit to the PHP parser? There must be a url, a query string, or a POST data, and a few HTTP headers. Okay, CGI is the protocol that specifies the data to be transmitted and the format in which the request is sent to the backend. Think about where the users you use in PHP code come from.

When the web server receives/index.phpAfter this request, the corresponding CGI program will be started. Here is the PHP parser. Next, the PHP parser parses the php. ini file, initializes the execution environment, processes the request, returns the processed result in the format specified by CGI, and exits the process. The web server then returns the result to the browser.

Okay, CGI is a protocol. It has nothing to do with the process. What is fastcgi? Fastcgi is used to improve CGI program performance.

To improve the performance, what is the problem with the CGI program performance? "The PHP parser parses the php. ini file and initializes the execution environment. The Standard CGI performs these steps for each request! I am tired of starting the process !), Therefore, the processing time is long. This is obviously unreasonable! So how does Fastcgi do it? First, Fastcgi will first start a master, parse the configuration file, initialize the execution environment, and then start multiple workers. When the request comes, the master will pass it to a worker and then immediately accept the next request. In this way, repetitive work 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. Of course, when there are too many idle workers, some will also be stopped, which improves performance and saves resources. This is how fastcgi manages processes.

What is PHP-FPM? It is a program that implements Fastcgi and is officially accepted by PHP.

As we all know, the interpreter for PHP is php-cgi. Php-cgi is just a CGI program. It can only parse requests, return results, and no process management. (Huang Shang, Chen really cannot do it !) So there are some programs that can schedule the php-cgi process, such as spawn-fcgi separated by lighthttpd. Good PHP-FPM is such a Dongdong, after a long period of development, gradually get everyone's approval (you know, a few years ago, but complained that PHP-FPM stability is too bad), is becoming more and more popular.

Okay. Come back to 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 processes and is used to manage fastcgi processes.

Yes. 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 fastcgi is a protocol, and it seems that no such process exists, even if php-fpm exists, it cannot be managed (at least currently ). Some say that php-fpm is a patch of the php kernel.

Previously, it was correct. 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 version as the source code to patch the kernel and then compile it. Later PHP kernel integrated with the PHP-FPM will be more convenient to use--enalbe-fpmCompile the parameter.

Some say that the php. ini configuration file cannot be restarted smoothly, so php-fpm is born.

Yes. After modifying php. ini, the php-cgi process cannot be restarted smoothly. The Processing Mechanism of php-fpm is that the new worker uses a new configuration, and the existing worker can stop working after it completes processing. This mechanism is used to smooth the process.

There is also said that the PHP-CGI is the FastCGI manager of PHP, So why then get a php-fpm out

No. Php-cgi is just a program that explains PHP scripts.

You (PHP) are going to talk to the eskimoo people (web servers, such as Apache and Nginx) about business.

If you speak Chinese (PHP code), he speaks eskimoo (C code) and cannot understand each other. What should I do? Then we can convert our speech into English (FastCGI protocol.

How to convert it? You need to use a Translator (PHP-FPM)
(Of course, the other Party also has a translator, which is his own)

Our translation machine is the newest, and the old-fashioned one (PHP-CGI) has been eliminated. But it (PHP-FPM) will only be used by young people (Linux systems), old people (Windows systems) won't play with it, so they have to continue with the old one.

I have read the above three answers, but I always think they are not well described.

Fastcgi is an upgraded CGI version. It is a language-independent protocol used to communicate with programs (such as PHP, Python, Java) and Web servers (Apache2, Nginx ), theoretically, programs written in any language can provide Web services through Fastcgi.
Fastcgi is characterized by the completion of multiple requests in one process in sequence to improve efficiency. Most Fastcgi implementations maintain a process pool.

PHP-fpm is an implementation of Fastcgi for PHP. It manages a process pool to process requests from Web servers. Currently, PHP-fpm is built in PHP.

However, PHP-fpm is only a "PHP Fastcgi Process Manager", and the handler still calls the PHP interpreter to handle the request. The PHP interpreter (in windows, click php-cgi.exe.

CGIIs an interface which tells the webserver how to pass data back and forth to and from an application. more specifically, it describes how request information is passed in environment variables (such as request type, remote IP address), how the reqeust body is passed in via standard input, and how the response is passed out via standard output.

FASTCGI: a protocol for communication between WEB servers and processing programs. It is an improved CGI solution.

Repeated CGI program loading is the main cause of low CGI performance. If the CGI program remains in the memory and is scheduled by the FastCGI Process Manager, it provides good performance, scalability, and Fail-Over features.

FASTCGI is a resident CGI that can run continuously. When a request arrives, it does not take time to fork a process for processing.

FastCGI is a language-independent, scalable CGI open extension that keeps the CGI interpreter process in the memory for high performance.

Generally, FastCGI's entire workflow is like this:

1. Load FastCGI Process Manager (iis isapi or Apache Module) when the Web Server starts)

2. FastCGI Process Manager initializes itself, starts Multiple CGI interpreter processes (multiple php-cgi are visible), and waits for the WebServer connection.

3. When a client request arrives at the Web Server, the FastCGI process manager selects and connects to a CGI interpreter. The Web server sends CGI Environment Variables and standard input to the FastCGI sub-process php-cgi.

4. After the FastCGI sub-process completes processing, the standard output and error messages are returned from the same connection to the Web Server. When the FastCGI sub-process closes the connection, the request processing is complete. The FastCGI sub-process then waits for and processes the next connection from the FastCGI Process Manager (running on the Web Server. In CGI Mode, php-cgi exits here.

Fastcgi is the communication protocol between app server and web server. In normal architecture, the app server is the master, and the web server is the client.

Php-fpm has two functions: 1. Implement a server program that supports the fastcgi Protocol 2. Process Manager

With php-fpm, you can change the php scriptMulti-process mode,Fastcgi ProtocolTo communicate with the web server.

You can refer to the nine deployment methods of python web programs.

CGI => http://www.w3.org/CGI/
FastCGI => http://www.fastcgi.com/drupal/
FPM => FastCGI Process Manager (FastCGI Process Manager)
I can only help you here, students.

So what, php-fpm is packaged and added by default in PHP5.3.

The answer on the first floor is comprehensive.

Introduction: CGI, FastCGI, PHP-CGI and PHP-FPM

Well written on the first floor

Very good.

Cgi is an interface that defines its input and output. php-cgi is an implementation of this interface. fastcgi is a performance optimized version for cgi implementation.

I installed the latest version of PHP 5.6. Why didn't php-fpm have reload restart... and so on? How can I smoothly reload php. ini after modifying it? Can I restart it after kill?

Since PHP5.4 is integrated, php-fpm can be used in windows?

Well written and easy to understand

CGI: Common Gateway Interface Common Network Management Protocol
FastCGI: Fast CGI implementation
Php-fpm: php FastCGI Process Manager (FastCGI Process Manager)

Fastcgi is a protocol. Php-fpm implements this protocol. Does the fastcgi protocol of php-fpm need a Jincheng? The fastcgi process implemented by php-fpm is called php-cgi =. So php-fpm is his own fastcgi or php-cgi process manager. Php-fpm was officially unavailable before 5.2. Php-fpm can be used as a third-party patch. After 5.2, the official website has been added by default, and it is no longer a patch.

====== Update:

References: Workshop. Directly open the php-cgi.exe (win32) and php-cgi processes to process fastcgi requests. Or use fastcgi Process Management Software similar to spawn-fcgi or uwsgi. Php-fpm aims to solve spawn-fcgi and uwsgi and other similar manager defects and integrate them into PHP official website.

In addition, the php-fpm was officially added only after php5.3.

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.