CGI, FastCGI, php-fpm detailed introduction

Source: Internet
Author: User
Tags fpm ini server memory


If you have to toss over Lnmp's friends certainly to these few names will not be unfamiliar, this article wants to introduce in a simple way what they mean, and what role.

CGI Introduction

The CGI full name is the "Public Gateway Interface" (Common Gateway Interface), a tool that HTTP servers "talk" to programs on your or other machines, and their programs must run on a network server. CGI can be written in any language, as long as the language has standard input, output, and environment variables. such as PHP,PERL,TCL and so on.

Web servers, such as Nginx, are just the distributors of content. Like what
1 if the request is/index.html, then the Web server will go to the file system to find this file, sent to the browser, where the distribution is static data.

2 If the request is/index.php, according to the configuration file, Nginx know this is not a static file, need to find a PHP parser to deal with, then he will be simple processing of this request to the PHP parser.

What data will nginx pass to the PHP parser? URLs, query strings, post data, and HTTP headers are all available. So, CGI is the protocol that prescribes which data to pass and in what format it is passed to the rear to process the request. Think carefully about where the users you're using in your PHP code come from.

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

fastcgi Introduction

1, fastcgi Simple Introduction

Fundamentally, fastcgi is used to improve the performance of CGI programs. Similar to cgi,fastcgi can be said to be an agreement.

FastCGI is like a resident (long-live) CGI, which can be executed all the time, and it won't take a while to fork once (this is CGI's most criticized Fork-and-execute model). It also supports distributed operations, where the FastCGI program can execute on a host other than the Web server and accept requests from other Web servers.
  
The fastcgi is a language-independent, scalable, CGI-open extension that primarily acts to keep the CGI interpreter process in memory and thus achieve higher performance. As we all know, the repeated loading of CGI interpreters is the main reason for the low performance of CGI, and can provide good performance, scalability, fail-over characteristics, and so on if the CGI interpreter remains in memory and accepts the FASTCGI process Manager schedule.

2. Working principle of fastcgi

The FastCGI interface adopts the C/s structure, which separates the HTTP server from the script resolution server, and initiates one or more script resolution Daemons on the script resolution server. Each time the HTTP server encounters a dynamic program, it can be delivered directly to the fastcgi process to execute, and the resulting results are returned to the browser. This approach allows HTTP servers to handle static requests in a single-minded manner or to return the results of a dynamic script server to the client, which greatly improves the performance of the entire application system.

1 Load fastcgi Process Manager (IIS ISAPI or Apache Module) when Web server starts
  
2 fastcgi The Process Manager itself, initiates multiple CGI interpreter processes (visible multiple php-cgi) and waits for a connection from the Web server.
  
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 subprocess php-cgi.
  
4 after the fastcgi process completes 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. The fastcgi process then waits and processes the next connection from the FASTCGI process Manager (running in Web server). In the CGI mode, php-cgi quits here.

3, fastcgi and CGI features

  
1 for CGI, each Web request PHP must reparse php.ini, reload all extensions, and reinitialize all data structures. With fastcgi, all of these occur only once when the process is started. An additional benefit is that persistent database connections (persistent DB connection) can work.

2 because fastcgi is more than a process, so more server memory than CGI multithreading, the PHP-CGI interpreter consumes 7 to 25 megabytes per process, multiplying this number by 50 or 100 is a large amount of memory.

PHP-FPM Introduction

PHP-FPM is a concrete implementation of the FASTCGI protocol, which manages a process pool to handle requests from Web servers. At present, after the PHP5.3 version, PHP-FPM is placed inside PHP.

PHP-CGI is actually an interpreter for PHP. And php-cgi is only a CGI program, he himself can only parse the request, return results, will not process management. So there are some programs that can dispatch php-cgi processes, such as the spawn-fcgi separated by LIGHTHTTPD. Similarly, PHP-FPM is also used for scheduling management of the PHP parser php-cgi management program.

PHP-FPM can achieve php.ini modified smooth restart by generating new child processes

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.