CGI, fastcgi, and php-fpm relationship plots

Source: Internet
Author: User
Tags fpm php web server sapi windows manual
This article mainly introduces the content is about CGI, fastcgi and php-fpm relationship diagram, has a certain reference value, now share to everyone, the need for friends can refer to


Directory

    • Basis

    • Module mode

    • Cgi

    • fastcgi Introduction

      • FastCGI Brief Introduction

      • How the FastCGI works

    • PHP-FPM Introduction

    • Summarize

    • Resources

The concepts of PHP-FPM, fastcgi, and CGI are often encountered when building LAMP/LNMP servers. If they are smattering, it is difficult to build a high-performance server. Next, we will explain the relationship between these concepts graphically.

Basis

In the entire site architecture, Web Server (such as Apache) is simply the publisher of the content. For a chestnut, if the client is requesting index.html, then Web server will go to the file system to find the file and send it to the browser, where static data is distributed.

If the request is index.php, according to the configuration file, Web server know that this is not a static file, need to find a PHP parser to handle, then he will make this request simple processing, and then hand over to the PHP parser.

When the Web server receives the index.php 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, returns the processed results in a specified CGI format, exits the process, and the WEB server returns the results to the browser. This is a complete dynamic PHP Web Access process, and then bring these concepts, it is much better to understand,

    • cgi: is a protocol for data exchange between Web Server and Web application.

    • fastcgi: with CGI , is a communication protocol, but it is more efficient than CGI to do some optimization. Similarly, the SCGI protocol is similar to FastCGI.

    • php-cgi: is PHP (Web application) an interface program for the CGI protocol provided by Web Server.

    • PHP-FPM: Yes PHP (Web application) provides an interface program to the FastCGI protocol provided by Web Server, and additionally provides some task management with relative intelligence.

WEB,

    • Web server generally refers to Apache, Nginx, IIS, LIGHTTPD, Tomcat and other servers,

    • Web application generally refer to applications such as PHP, Java, ASP.

Module mode

Before we get to know CGI, let's look at another way that Web server passes data:PHP module loading . To Apache, for example, in the PHP module mode, is not in the Apache configuration file httpd.conf Add such a few words:

# Add the following 2 sentences loadmodule php5_module d:/php/php5apache2_2.dlladdtype application/x-httpd-php. php# Modify the following content <ifmodule Dir_ module>    DirectoryIndex index.php index.html</ifmodule>

Above is the installation of the PHP and Apache environment under Windows manual configuration, under the Linux source installation is basically configured like this:

#./configure--with-mysql=/usr/local--with-apache=/usr/local/apache--enable-track-vars

So, in this way, their common essence is to use LoadModule to load php5_module, that is, PHP as a sub-module of Apache to run . When PHP files are accessed through the Web, Apache invokes Php5_module to parse the PHP code.

So how does php5_module send the data to the PHP parser to parse the PHP code? The answer is through SAPI.

Let's take a look at a picture, in detail about the relationship between Apache and PHP and SAPI:

From the above picture, we see that SAPI is such an intermediate process, SAPI provides an interface with the external communication, a bit similar to the socket, so that PHP can interact with other applications data (Apache,nginx, etc.). PHP provides many kinds of SAPI by default, common to Apache and Nginx Php5_module, CGI, FastCGI, ISAPI for IIS, and CLI for the shell.

So, the above Apache calls PHP to execute the process as follows:

PHP sapi, Php5_module, httpd, Apache

All right. Apache and PHP through the Php5_module way to find out!

This mode installs the PHP module into Apache, so every Apache end request generates a process that includes all of the operations of PHP.

In, we can clearly see, Apache every request, will produce a process to connect PHP through the SAPI to complete the request, it is conceivable that if the user too many, the number of concurrent, the server will not bear.

Moreover, when the mod_php into Apache, it is difficult to locate the problem is PHP or Apache problem.

Cgi

The CGI (Common Gateway Interface) Full name is a " Common Gateway Interface ", a WEB server and PHP application "chat" of a tool, its programs must be run on the 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.

What data will the Web server send to the PHP parser? URLs, query strings, post data, HTTP headers all have. So, CGI is the protocol that specifies what data to pass, in what format, to the rear to handle the request. Think carefully about where the users you are using in your PHP code come from.

In other words, CGI is designed to work with Web servers. When the Web server receives a request from the user, it submits the request to a CGI program (such as PHP-CGI), the CGI program should process the requested parameters (parsing PHP), then output the standard HTML statement, return it to the Web server, and return the Web server to the client. This is how the common CGI works.

The advantage of CGI is that it is completely independent of any server, just as an intermediate molecule. provides interfaces to Apache and PHP. They pass through the CGI line to complete the data transfer. The benefits of doing this are to minimize the association of 2 and make them 2 more independent.

But CGI has a place where the egg hurts, that is, every Web request will have the start and exit process, that is, the most people criticized the fork-and-execute mode, so that a large-scale concurrency, it is dead-cocked.

fastcgi Introduction

FastCGI Brief Introduction

Fundamentally, fastcgi is used to improve the performance of CGI programs. Similar to CGI,fastcgi can also be said to be a protocol .

FastCGI is like a resident (long-live) CGI, which can be executed all the time, so long as it is activated, it will not take a moment to fork it every time. It also supports distributed operations where the FastCGI program can execute on hosts other than the Web server and accept requests from other Web servers.

FastCGI is a language-independent, extensible architecture for CGI open extensions whose main behavior is to keep the CGI interpreter process in memory and thus achieve high performance. As we all know, the repeated loading of CGI interpreter is the main reason of poor CGI performance, if the CGI interpreter remains in memory and accepts the FASTCGI process manager scheduling, it can provide good performance, scalability, fail-over characteristics and so on.

How the FastCGI works

FastCGI interface mode uses the C/s structure, can separate the HTTP server and the script parsing server, and start one or more script parsing daemon on the script parsing 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 the HTTP server to handle static requests exclusively, 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 the FASTCGI Process Manager (Apache module or IIS ISAPI, etc.) when Web server starts

    2. The FASTCGI process Manager itself initializes, starts multiple CGI interpreter processes (multiple php-cgi can be built), 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. WEB server sends CGI environment variables and standard input to the FASTCGI child process php-cgi.

    4. After the fastcgi child process finishes processing, the standard output and error information is 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.

FastCGI and CGI features:

    1. For CGI, every Web request PHP must re-parse php.ini, reload all extensions, and reinitialize all data structures. With fastcgi, all of this occurs only once when the process is started. An additional benefit is that the persistent database connection (persistent connection) can work.

    2. Since FastCGI is a multi-process, it consumes more server memory than CGI multithreading, and 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

To understand PHP-FPM, you have to first talk about php-cgi.

php-cgi is the PHP implementation of the own FASTCGI manager . Although it is the official PHP production, but this AH is not to give strength, performance is too poor, but also very cumbersome and humane, mainly reflected in:

    1. php-cgi after changing the php.ini configuration, it is necessary to restart the php-cgi for the new Php-ini to take effect and not to smooth the restart.

    2. Directly kill the php-cgi process, PHP will not be able to run.

The above 2 questions, has been a lot of people have been sick for a long time, so many people have been using Module mode. Until 2004 a php-fpm called Andrei Nigmatulin, the appearance of this artifact has completely broken this situation, this is a PHP dedicated fastcgi manager, it is cool to overcome the above 2 problems, but also performance in other aspects of the performance is more strong.

In other words, PHP-FPM is a concrete implementation of the FastCGI protocol, which manages a process pool to handle requests from the Web server. Currently, after the PHP5.3 version, PHP-FPM is built into PHP .

Because php-cgi is just a CGI program, he can only parse the request itself, return the results, and not process management. So there are some programs that can dispatch the php-cgi process, such as the spawn-fcgi separated by LIGHTHTTPD. Similarly, PHP-FPM is also a management program for scheduling and managing PHP parser php-cgi.

PHP-FPM can implement php.ini modified smooth restart by generating a new child process.

Summarize

Finally, let's conclude that these technologies are constantly being upgraded to solve any problems (or not upgrade).

Therefore, if you want to build a high-performance PHP Web server, the best way now is Apache/nginx + FastCGI + php-fpm (+php-cgi) way, do not use Module load or CGI mode:)

This article picture is made with Visio, source file: php-fpm

Original address: https://www.awaimai.com/371.html

Related recommendations:

PHP7 Kernel anatomy 1 cgi and fastcgi

What exactly does CGI, FastCGI, and php_fpm matter?

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.