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/02 Report--
This article introduces the knowledge of "what is the relationship between CGI, FastCGI and PHP-FPM". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
When building a LAMP/LNMP server, you often encounter the concepts of PHP-FPM, FastCGI, and CGI. If you have little knowledge of them, it is difficult to build a high-performance server. Next, we will graphically explain the relationship between these concepts. Basics
In the entire site architecture, Web Server (such as Apache) is just a content distributor. Take Chestnut, for example, if the client requests index.html, then Web Server will find the file in the file system and send it to the browser, where static data is distributed.
If the request is index.php, and according to the configuration file, Web Server knows that this is not a static file and needs to go to the PHP parser to process it, then he will simply process the request and 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.
This is the end of the content of "what is the relationship between CGI, FastCGI and PHP-FPM". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.