In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly talks about "what is the relationship of cgi,fastcgi,php-fpm,php-cgi in PHP". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what is the relationship of cgi,fastcgi,php-fpm,php-cgi in PHP"?
Basics:
In the entire website, web server (such as apache) is only the distributor of content. For example, if the customer requests index.html, web server will find the file in the file system and send it to the browser, where static data is distributed, as shown in the figure:
If the request is index.php, according to the configuration file, Web Server needs to find a PHP parser to process, then he will simply process the request and then give it to the PHP parser.
When Web Server receives the request for index.php, it starts the corresponding CGI program, which is the parser of PHP. Next, the PHP parser parses the php.ini file, initializes the execution environment, then processes the request, returns the processed result in the format specified by CGI, exits the process, and Web server returns the result to the browser. This is a complete dynamic PHP Web access process, and then lead to these concepts, it is much easier to understand
CGI: a protocol for data exchange between Web Server and Web Application.
FastCGI: with CGI, it is a communication protocol, but it does some optimization in efficiency than CGI. Similarly, the SCGI protocol is similar to FastCGI.
PHP-CGI: is the interface program of PHP (Web Application) to the CGI protocol provided by Web Server.
PHP-FPM: it is the interface program of PHP (Web Application) to the FastCGI protocol provided by Web Server. It also provides some relatively intelligent task management.
In WEB
Web Server generally refers to Apache, Nginx, IIS, Lighttpd, Tomcat and other servers
Web Application generally refers to PHP, Java, Asp.net and other applications.
Module mode:
Before we look at CGI, let's take a look at another way for Web server to pass data: the PHP Module loading method. Take Apache as an example, in PHP Module mode, do you add the following sentences to the configuration file httpd.conf of Apache:
# add the following 2 sentences LoadModule php5_module D:/php/php5apache2_2.dllAddType application/x-httpd-php .php # modify the following content DirectoryIndex index.php index.html
The above is manually configured after installing php and apache environment under Windows. The source code installation under linux is roughly configured as follows:
. / 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, which is to run php as a submodule of apache. When the php file is accessed through web, apache calls php5_module to parse the php code.
So how does php5_module pass data to the php parser to parse the php code? The answer is through sapi.
Let's take another look at the picture and talk in detail about the relationship between apache and php and sapi:
From the figure above, we can see that sapi is such an intermediate process. SAPI provides an interface to communicate with the outside world, which is similar to socket, so that PHP can exchange data with other applications (apache,nginx, etc.). Php provides many kinds of SAPI by default, including php5_module, CGI, FastCGI for apache and nginx, ISAPI for IIS, and CLI for Shell.
Therefore, the above apache calls php to perform the following process:
Apache-> httpd-> php5_module-> sapi-> php
Okay. Apache and php can figure it out through php5_module.
This mode installs the php module into the apache, so each time the apache ends the request, there will be a process, which completely includes various operations such as the calculation of the php.
In the figure above, we can clearly see that every time apache receives a request, a process will be generated to connect to php to complete the request through sapi. It is conceivable that if there are too many users and too many concurrency, the server will not be able to bear it.
Moreover, when compiling mod_php into apache, it is difficult to locate whether it is the problem of php or apache when something goes wrong.
CGI:
The full name of CGI (Common Gateway Interface) is "Universal Gateway Interface". It is a tool for WEB server to "talk" with PHP applications, and its program must be run on the network server. CGI can be written in any language, as long as it has standard input, output, and environment variables. Such as php, perl, tcl and so on.
What data does the WEB server pass to the PHP parser? URL, query string, POST data, HTTP header will be available. So, CGI is the protocol that specifies what data to pass and in what format to pass it to the rear to process the request. Think about where the users you use in PHP code come from.
In other words, CGI is specifically used to deal with web servers. When the web server receives the user's request, it will submit the request to the cgi program (such as php-cgi). The cgi program will handle the request according to the submitted parameters (parsing php), and then output the standard html statement and return it to the web server, and the WEB server will return it to the client. This is how the ordinary cgi works.
The advantage of CGI is that it is completely independent of any server, just as an intermediary. Provides interfaces to apache and php. They connect the data through cgi. This has the advantage of minimizing the association of 2, making them more independent.
But there is a pain in CGI, that is, every web request will have a start and exit process, that is, the most criticized fork-and-execute mode, so it will die in large-scale concurrency.
Introduction to FastCGI A brief introduction to FastCGI
Basically, FastCGI is used to improve the performance of CGI programs. Similar to CGI,FastCGI, it can also be said to be a protocol.
FastCGI is like a resident (long-live) CGI, it can be executed all the time, as long as it is activated, it does not take the time to fork every time. It also supports distributed computing, that is, FastCGI programs can be executed on hosts other than web servers and accept requests from other web servers.
FastCGI is an open extension of CGI that is language-independent and scalable, and its main behavior is to keep the CGI interpreter process in memory and thus achieve high performance. As we all know, the repeated loading of the CGI interpreter is the main reason for the poor performance of CGI. If the CGI interpreter remains in memory and accepts scheduling by the FastCGI process manager, it can provide good performance, scalability, Fail- Over features, and so on.
How FastCGI works
The FastCGI interface adopts Cramp S structure, which can separate the HTTP server from the script parsing server, and start one or more script parsing daemons on the script parsing server. Every time the HTTP server encounters a dynamic program, it can deliver it directly to the FastCGI process for execution, and then return the results to the browser. This approach allows the HTTP server to handle static requests exclusively, or returns the results of the dynamic script server to the client, which greatly improves the performance of the entire application system.
Load the FastCGI process Manager (Apache Module or IIS ISAPI, etc.) when Web Server starts
The FastCGI process manager initializes itself, starts multiple CGI interpreter processes (which can build multiple php-cgi), and waits for a connection from Web Server.
When the 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.
After the FastCGI child process finishes processing, the standard output and error message are returned to Web Server from the same connection. When the FastCGI child process closes the connection, the request is processed. 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.
Features of FastCGI and CGI:
For CGI, each Web request PHP must reparse the php.ini, reload all the extensions, and reinitialize the entire data structure. With FastCGI, all of this happens only once when the process starts. An added benefit is that persistent database connections (Persistent database connection) work.
Because FastCGI is multi-process, it consumes more server memory than CGI multithreading, and the php-cgi interpreter consumes 7 to 25 megabytes of memory 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 talk about PHP-CGI.
PHP-CGI is the FastCGI manager that comes with the PHP implementation. Although it is officially produced by php, it is not powerful at all, the performance is too poor, and it is also very troublesome and impersonal, mainly reflected in:
After php-cgi changes the php.ini configuration, you need to restart php-cgi for the new php-ini to take effect, and cannot restart smoothly.
Kill the php-cgi process directly and php will not run.
The above two questions have made a lot of people sick for a long time, so many people have been using Module. Until a loser named Andrei Nigmatulin invented PHP-FPM in 2004, the emergence of this artifact completely broke the situation, this is a PHP dedicated fastcgi manager, it is very comfortable to overcome the above two problems, but also more powerful in other ways.
In other words, PHP-FPM is a concrete implementation of the FastCGI protocol and is responsible for managing a process pool to process requests from the Web server.
Currently, after the PHP5.3 version, PHP-FPM is built into PHP.
Because PHP-CGI is only a CGI program, it can only parse the request and return the result, not process management. So there are some programs that can schedule php-cgi processes, such as spawn-fcgi separated by lighthttpd. Similarly, PHP-FPM is the hypervisor used to schedule and manage the PHP parser php-cgi.
PHP-FPM can achieve a smooth restart after php.ini modification by generating new child processes.
At this point, I believe you have a deeper understanding of "what is the relationship of cgi,fastcgi,php-fpm,php-cgi in PHP". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.