PHP FastCGI and mod_php

Source: Internet
Author: User
Tags sapi

On the internet for fastcgi and mod_php knowledge is messy and not comprehensive, so in this collation, so that beginners to access the convenience of learners. PHP is the most commonly used in the mode of the module (mod_php) run in Apache, is also the default way Apache run PHP, but in Nginx, Nginx is also used php-fpm, but php-fpm what is a what? What's the relationship with PHP? Today we will explore together.

On the internet for fastcgi and mod_php knowledge is messy and not comprehensive, so in this collation, so that beginners to access the convenience of learners.

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? Today we will explore together.

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, images, 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 to the user by the Web server.

Most Web servers cannot parse PHP code, so it requires a program that can parse the 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 that PHP's most commonly used SAPI provide: 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:

Add to

Loadmodulephp5_module modules/libphp5.so

addtypeapplication/x-httpd-php.php

Modify

directoryindexindex.php index.html index.htm index.html

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.

To be blunt, 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:

Load the FASTCGI Process Manager (IIS ISAPI or Apache Module) when Web server starts.

The FASTCGI process Manager itself initializes, launches multiple CGI interpreter processes (visible multiple php-cgi) and waits for webserver connections.

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.

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, php-cgi exits.

In other words, 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 any program written in any language can provide Web services through fastcgi.

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 after completion it will not exit, but continue to wait for the next Web request.

php-fpm

PHP-FPM is an implementation of PHP's 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.

More PHP Related technologies Please search thousand-front php, do the real self, with conscience to do education.

Internet + times, to keep learning, hand-on Qian Feng Php,dream

It Possible.




PHP FastCGI and mod_php

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.