"Turn" PHP fastcgi and mod_php detailed

Source: Internet
Author: User
Tags sapi

Original address: http://article.gitos.cn/2015/Aurthur/PHP-Mod-PHP-And-Fast-CGI-Explain.html

Background

PHP is most commonly used in a modular Way (mod_php) run in Apache, is also the default way Apache runs PHP, but in Nginx, Nginx is also used php-fpm, but what is the php-fpm? What's the relationship with PHP? Let's explore it together today.

PHP Processor (PHP handlers)

The first thing to remember is that any kind of Web server (Apache, nginx, etc.) is designed to send users static resources such as HTML, pictures, Web server itself does not explain any dynamic script (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, then pass the result of the resolution to the Web server, and finally the Web server to the user

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

Now we know that both Apache and Nginx need PHP processor to handle PHP code, then how to connect the server and the PHP processor? How does the server communicate with the PHP processor?

The answer is through the SAPI (server application Programming Interface application Programming port), simply put, SAPI refers to the PHP specific application programming interface, like a PC, regardless of which operating system installed, As long as the interface specifications of the PC can be run properly on the PC, PHP scripts to execute in a number of ways, through the Web server, or directly under the command line, can also be embedded in other programs, interested in you can study the PHP kernel

We continue here to discuss the 2 ways in which PHP's most commonly used SAPI provide connectivity: mod_php and mod_fastcgi

mod_php mode

Let's review how Apache can identify PHP code. is not the Apache configuration file httpd.conf Add or modify such a few words:

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

Also known as PHP as a sub-module of Apache to run, when the Web Access to PHP files, Apache will call Php5_module to parse the PHP code

After configuring the Load mod_php module, PHP is part of the APAHCE process itself, and each new Apache child process will load this module

mod_fastcgi mode

Let's take a look at PHP-FPM's official website.

Php-fpm-a simple and robust FastCGI Process Manager for PHP
PHP-FPM (FastCGI Process Manager) is a alternative PHP FastCGI implementation with some additional features useful for SI TES of any size, especially busier sites.

PHP-FPM is a PHP fastcgi process manager, which is very simple to explain. This shows that PHP-FPM is working in the auxiliary mod_fastcgi mode, but what is fastcgi? What is the management process?

What is CGI?

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

CGI is an interface standard between an external application (CGI program) and a Web server, and is a discipline for passing information between CGI programs and Web servers.

The CGI specification allows the Web server to execute external programs and send their output to a Web browser, CGI turning a simple set of static hypermedia documents from the Web into a complete new interactive media

Plainly, CGI is an external application (CGI program) with the Web server protocol, CGI is to ensure that the data passed by the server is a standard format

What is fastcgi?

FastCGI is like a resident (long-live) CGI, which can be executed all the time, so long as it is activated, it will not take a moment to fork once (this is the most notorious fork-and-execute mode of CGI). It also supports distributed operations where the FastCGI program can execute and accept requests from other Web servers on hosts other than the Web server

FastCGI is a language-independent, extensible architecture for CGI open extensions whose main behavior is to keep the CGI interpreter process in memory and thus achieve high performance. As we all know, the repeated loading of CGI interpreter is the main reason of poor CGI performance, if the CGI interpreter remains in memory and accepts the FASTCGI process manager scheduling, it can provide good performance, scalability, fail-over characteristics and so on.

In general, the whole workflow of fastcgi is this:

    1. Load fastcgi Process Manager (IIS ISAPI or Apache Module) when Web server starts

    2. FASTCGI Process Manager Self-initializes, launches multiple CGI interpreter processes (visible multiple php-cgi) and waits for webserver connections

    3. When a client request arrives at the 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

    4. The FASTCGI child process returns standard output and error information from the same connection to the Web Server after processing is complete. When the fastcgi child process closes the connection, the request is processed and the fastcgi child process waits and processes the next connection from the FASTCGI process Manager (running in the Web server), and in CGI mode, the php-cgi exits

This means that fastcgi is an upgraded version of CGI, a language-agnostic protocol used to communicate programs (such as PHP, Python, Java) and Web servers (Apache2, Nginx), and in theory, programs written in any language can be fastcgi to provide Web services

FastCGI is characterized by a process in which multiple requests are completed sequentially to achieve efficiency gains, and most FASTCGI implementations maintain a process pool

Popular explanation: FastCGI need to start in advance, and can start multiple CGI modules, there has been running waiting for the web to send a request, and then to the PHP parsing operation, after the completion of the HTML returned to the Web, but it will not exit after completion, but continue to wait for the next Web request

php-fpm

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

But PHP-FPM is just a "PHP FastCGI process Manager" that still invokes the PHP interpreter itself to handle the request, and the PHP interpreter (under Windows) is Php-cgi.exe

Original address: http://article.gitos.cn/2015/Aurthur/PHP-Mod-PHP-And-Fast-CGI-Explain.html

"Turn" PHP fastcgi and mod_php detailed

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.