Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between FastCGI and mod_php in PHP

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

What is the difference between FastCGI and mod_php in PHP? aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Background

The most common way for PHP to run in Apache is mod_php, which is also the default way for Apache to run PHP; but in Nginx, Nginx uses PHP-FPM, but what exactly is PHP-FPM? What does it have to do with php? Today, let's explore.

PHP processor (PHP handlers)

The first thing to keep in mind is that any Web server (Apache, Nginx, etc.) is designed to send static resources such as html, pictures, etc., to the user, and the Web server itself cannot interpret any dynamic scripts (PHP, Python, etc.).

The PHP processor is used to interpret the PHP code in the Web application, and interpret it as HTML or other static resources, and then transmit the parsed result to the Web server, and finally send it to the user by the Web server.

Most Web servers cannot parse PHP code, so it needs a program that can parse PHP code, which is the PHP processor.

Now that we know that both Apache and Nginx need a PHP processor to process php code, how do you connect the server to the php processor? In other words, how does the server communicate with the php processor?

The answer is through SAPI (Server Application Programming Interface server-side application programming port). Simply put, SAPI refers to the programming interface of PHP specific applications, just like PC, no matter which operating system is installed, as long as it meets the interface specification of PC, it can run normally on PC. PHP scripts can be executed in many ways, through the Web server, or directly under the command line, or can be embedded in other programs. If you are interested, you can study the PHP kernel.

Let's continue to discuss the two connection methods provided by SAPI, which is most commonly used by PHP: mod_php and mod_fastcgi.

Mod_php mode

In retrospect, how can Apache recognize php code? Is it possible to add or modify the following sentences in Apache's configuration file httpd.conf:

/ / add LoadModule php5_module modules/libphp5.soAddType application/x-httpd-php .php / / modify DirectoryIndex index.php index.html index.htm index.html

That is, php runs as a submodule of Apache, and when the php file is accessed through web, Apache will call php5_module to parse the php code.

After the configuration loads the mod_php module, php is part of the Apahce process itself, and each new Apache child process loads the module.

Mod_fastcgi mode

Let's first take a look at the instructions on PHP-FPM 's official website:

PHP-FPM-A simple and robust FastCGI Process Manager for PHP

PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites.

PHP-FPM is a FastCGI process manager for PHP, which is very simple to explain. This shows that PHP-FPM works with the mod_fastcgi mode, but what is FastCGI? What process is being managed?

What is CGI?

CGI (Common Gateway Interface) is one of the most important technologies in WWW technology, which has an irreplaceable important position.

CGI is the interface standard between external applications (CGI programs) and Web servers, and it is the procedure for transmitting information between CGI programs and Web servers.

The CGI specification allows Web servers to execute external programs and send their output to Web browsers, and CGI turns a simple set of static hypermedia documents from Web into a complete new interactive media.

To put it bluntly, CGI is a protocol between external applications (CGI programs) and Web servers, and CGI is to ensure that the data passed by Server is in a standard format.

What is FastCGI?

FastCGI is like a resident (long-live) CGI that can be executed all the time, as long as it is activated and does not take the time to fork every time (this is the most criticized fork-and-execute mode of CGI). 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 a language-independent, scalable architecture open extension of CGI, 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.

In general, the entire workflow of FastCGI is as follows:

Load the FastCGI process Manager (IIS ISAPI or Apache Module) when Web Server starts.

The FastCGI process manager initializes itself, starts multiple CGI interpreter processes (see multiple php-cgi) and waits for a connection from WebServer.

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.

The FastCGI child process returns standard output and error messages from the same connection to Web Server after processing. When the FastCGI child process closes the connection, the request is processed, and the FastCGI child process then waits and processes the next connection from the FastCGI process manager (running in Web Server), where php-cgi exits in FastCGI mode.

In other words, FastCGI is an upgraded version of CGI, a language-independent protocol used to communicate 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 completing multiple requests in a process in order to improve efficiency, and most FastCGI implementations maintain a process pool.

Popular explanation: FastCGI needs to be started in advance, and multiple CGI modules can be started, where it has been running waiting for web to send a request, and then parsing operation to php. After completion, it generates html and returns it to web, but it will not quit after completion, but will continue to wait for the next web request.

PHP-FPM

PHP-FPM is an implementation of FastCGI for PHP, which manages a process pool to process requests from the Web server.

But PHP-FPM is just a "PHP FastCGI process manager". It still calls the PHP interpreter itself to process the request, and the PHP interpreter (under Windows) is php-cgi.exe.

The answer to the question about the difference between FastCGI and mod_php in PHP is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report