FastCGI and mod_php in PHP

Source: Internet
Author: User
Tags sapi

FastCGI and mod_php in PHP

The knowledge about FastCGI and mod_php on the internet is messy and incomplete, so you can sort it out here for the convenience of beginners.

 

Background

The most common way of PHP is to run the module (mod_php) in Apache, which is also the default way of Apache to run PHP; but in Nginx, Nginx uses PHP-FPM, but what exactly is PHP-FPM? What is the relationship with php? Let's explore it together today.

 

PHP handlers)

Remember that any Web server (such as Apache and Nginx) is designed to send html, images, and other static resources to users, the Web server itself cannot interpret any dynamic scripts (such as PHP and Python ).

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

Most Web servers cannot parse PHP code, so they need a program that can parse PHP code. This is the PHP processor.

Now we know that Apache and Nginx both require PHP processors to process php code. How can we connect servers to php processors? That is to say, how does the server communicate with the php processor?

The answer is through SAPI (Server Application Programming Interface Server-side Application Programming port). In short, SAPI refers to the Programming Interface of a specific PHP Application, just like a PC, no matter which operating systems are installed, PHP scripts can run normally on the PC as long as they meet the interface specifications of the PC. There are many ways to execute PHP scripts through the Web server, you can also embed it in other programs directly in the command line. If you are interested, you can study the PHP kernel.

We will continue to discuss the two connection methods provided by the most common SAPI in PHP: mod_php and mod_fastcgi.

 

Mod_php Mode

Let's review how Apache can recognize php code? Add or modify the following statements in the Apache configuration file httpd. conf:

  1. // Add
  2. LoadModule php5_module modules/libphp5.so
  3. AddType application/x-httpd-php .php
  4. // Modify
  5. <IfModule dir_module>
  6. DirectoryIndex index.php index.html index.htm index.html
  7. </IfModule>

That is, php runs as a submodule of Apache. When you access the php file through the web, Apache calls php5_module to parse the php code.

After the mod_php module is configured and loaded, php is part of the Apahce process. This module is loaded by each new Apache sub-process.

 

Mod_fastcgi Mode

We look at the PHP-FPM official site of the instructions:

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.

The PHP-FPM is a PHP FastCGI Process Manager that explains very simple. This shows that the PHP-FPM is to assist mod_fastcgi mode to work, but what is FastCGI? What processes are managed?

 

What is CGI?

CGI (Common Gateway Interface) is one of the most important technologies in WWW technology and plays an irreplaceable role.

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

CGI specifications allow the Web server to execute external programs and send their output to the Web browser. CGI converts a simple set of static hypermedia documents on the Web into a complete new interactive media.

To put it bluntly, CGI is a protocol between an external application (CGI program) and a Web Server. CGI is used to ensure that the data transmitted by the Server is in a standard format.

 

What is FastCGI?

FastCGI is like a long-live CGI. It can be executed all the time, it will not take time to fork every time (this is the most criticized fork-and-execute mode for CGI ). It also supports distributed operations, that is, FastCGI programs can run on hosts other than website servers and receive requests from other website servers.

FastCGI is a CGI open extension in a language-independent and scalable architecture. Its main behavior is to maintain the CGI interpreter process in the memory and thus achieve high performance. As we all know, repeated loading of the CGI interpreter is the main cause of low CGI performance. If the CGI interpreter is kept in the memory and is scheduled by the FastCGI Process Manager, it can provide good performance, scalability, Fail-Over features, and so on.

Generally, FastCGI's entire workflow is like this:

  1. When the Web Server starts, load the FastCGI Process Manager (iis isapi or Apache Module ).
  2. FastCGI Process Manager initializes itself, starts Multiple CGI interpreter processes (multiple php-cgi are visible), and waits for the WebServer connection.
  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 sub-process php-cgi.
  4. After the FastCGI sub-process completes processing, the standard output and error messages are returned from the same connection to the Web Server. When the FastCGI sub-process closes the connection, the request is processed completely. The FastCGI sub-process then waits for and processes the next connection from the FastCGI Process Manager (running on the Web Server, in CGI Mode, php-cgi has exited.

That is to say, FastCGI is an upgraded CGI version. It is a language-independent protocol used to communicate with 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 the completion of multiple requests in one process in sequence to improve efficiency. Most FastCGI implementations maintain a process pool.

Common Explanation: FastCGI needs to be started in advance, and Multiple CGI Modules can be started, where it will continue to run and wait for the web to send requests, and then parse the computation for php, after html is generated and returned to the web, it does not exit, but continues to wait for the next web request.

 

PHP-FPM

PHP-FPM is an implementation of FastCGI for PHP, which is responsible for managing a process pool to process requests from Web servers.

But the PHP-FPM is just a "PHP FastCGI Process Manager", the handler still calls the PHP interpreter to handle the request, the PHP interpreter ( php-cgi.exe under windows.

 

Conclusion

If you have said so much, you do not know whether to express it clearly. If there are any errors, please correct them. Thank you.

LNMP + FastCGI platform build script

Who is Nginx + FastCGI affecting the timeout?

PHP + FastCGI + Nginx Dynamic Request Processing Configuration

Build a LAMP environment for FastCGI-based host service separation

Compile and install LAMP + XCache in FastCGI Mode

This article permanently updates the link address:

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.