This is the 8th day of my participation in the August More text Challenge. For details, see: August More Text Challenge

The online knowledge of FastCGI and mod_php is somewhat cluttered and incomplete, so here is a list for beginners.

background

The most common way PHP is run in a module (mod_php) in Apache, which is the default way Apache runs PHP;

But in Nginx, Nginx uses phP-fpm, but what exactly is phP-fpm? What does IT have to do with PHP? Today we’re going to find out.

PHP handlers

The first thing to remember is that any Web server (Apache, Nginx, etc.) is designed to send HTML, images, and other static resources to the user, and the Web server itself cannot interpret any dynamic scripts (PHP, Python, etc.).

A PHP processor interprets PHP code in a Web application, interprets it as HTML or other static resources, and then parses the result to a Web server, which then sends it to the user.

Most Web servers can’t parse PHP code, so they need a program that can parse PHP code, which is a PHP processor.

Now that we know that Apache and Nginx both require a PHP processor to handle PHP code, how do we connect the server to the PHP processor? That is, how does the server communicate with the PHP processor?

The answer is the Server Application Programming Interface (SAPI). In simple terms, SAPI is the Programming Interface for the specific Application of PHP, just like a PC, no matter which operating system is installed. PHP scripts can be executed in a variety of ways, either via a Web server, directly from the command line, or embedded in other programs. You can explore the PHP kernel if you are interested

Let’s continue with the two most common SAPI connections in PHP: mod_php and mod_fastCGI

Mod_php mode

Let’s review, how does Apache recognize PHP code? Add or modify the Apache configuration file httpd.conf:

// Add LoadModule php5_Module modules/libphp5.so AddType application/ x-httpD-php.php // add LoadModule php5_Module modules/libphp5.so AddType application/ x-httpd-php.php<IfModule dir_module>
    DirectoryIndex index.php index.html index.htm  index.html
</IfModule>
Copy the code

PHP runs as a submodule of Apache, and when PHP files are accessed through the Web, Apache calls php5_module to parse the PHP code

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

Mod_fastcgi mode

Let’s start with the instructions on the PHP-FPM website

PHP-FPM (FastCGI Process Manager) – A simple and robust FastCGI Process Manager for PHP

Php-fpm is a PHP FastCGI process manager that is very simple to explain. Php-fpm works with mod_fastCGI, but what is FastCGI? What processes are being managed?

What is CGI?

  • CGI (Common Gateway Interface) is one of the most important technologies in WWW technology, and has an irreplaceable important position.
  • CGI is the interface standard between external applications (CGI programs) and Web servers, and is the discipline for passing information between CGI programs and Web servers.
  • The CGI specification allows a Web server to execute external programs and send their output to a Web browser, turning the Web’s simple set of static hypermedia documents into a complete new interactive medium.
  • In plain English, CGI is a protocol between an external application (CGI program) and a Web Server to ensure that the data passed by the Server is in a standard format.

What is FastCGI?

FastCGI is like a long-live CGI, it can be executed forever, as long as it is activated, and it doesn’t take time to fork every time (the most notorious fork-and-execute mode of CGI). It also supports distributed computing, where FastCGI programs can perform on hosts other than the web server and accept requests from other web servers

FastCGI is a language-independent, scalable architecture open extension of CGI, whose main behavior is to keep the CGI interpreter process in memory and thus achieve high performance. It is well known that repetitive loading of CGI interpreters is a major cause of poor CGI performance, and if CGI interpreters stay in memory and are scheduled by the FastCGI process manager, they can provide good performance, scalability, fail-over features, and so on

In general, the entire FastCGI workflow looks like this:

  • Web Server startup loads the FastCGI process manager (IIS ISAPI or Apache Module)
  • The FastCGI process manager initializes itself, starts multiple CGI interpreter processes (multiple PHP-CGI visible), and waits for the WebServer to connect
  • When a client request arrives on 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 child process phP-CGI
  • When the FastCGI child finishes processing, it returns standard output and error information from the same connection to the Web Server. When the FastCGI child closes the connection, the request is processed, and the FastCGI child then waits and processes the next connection from the FastCGI process manager (running in the Web Server), where phP-CGI exits in CGI mode
    • FastCGI is an update to CGI, a language-independent protocol, FastCGI is used to communicate between programs (such as PHP, Python, Java) and Web servers (Apache2, Nginx). In theory, programs written in any language can provide Web services using FastCGI
    • FastCGI features multiple requests in a process in order to improve efficiency, and most FastCGI implementations maintain a pool of processes
    • FastCGI needs to be started in advance, and you can start multiple CGI modules, running there waiting for the Web to make a request, then parsing it to PHP, generating HTML and returning it to the Web, but it doesn’t quit when it’s done, waiting for the next Web request

PHP-FPM

Php-fpm is an implementation of FastCGI for PHP that manages a pool of processes to handle requests from the Web server.

But PHP-fpm is just the “PHP FastCGI process manager”, it still calls the PHP interpreter itself, which (under Windows) is PHP-cgi.exe, to handle the request.

5. Conclusion

Said so much, also do not know whether to express clearly, if there is not correct, please correct.