How PHP links to Web servers

Source: Internet
Author: User
Tags sapi
What is fastcgi and php-fpm in PHP?

Php

Recently in the study and study of PHP performance knowledge, see the factcgi and php-fpm, found that I am a little less understanding of them, can be said to be almost ignorant, it is quite scary to think about. decided to study carefully about this knowledge.

The following articles are referenced and studied:
1. Introduction, comparison, and performance data of mod_php and mod_fastcgi and PHP-FPM
2. Actual combat nginx_ replaced

In order to step by step to lead fastcgi and PHP-FPM, I first say 1.1 point about PHP around. Hey. Suddenly feel that people live very tired!

Let's talk about Web servers.

PHP is a web-born back-end language, our PHP dog is of course the clearest. So PHP is just a back-end language, so it has to be web-server to provide Web functionality. Of course, other backend languages, if you do Web applications, must also be Web servers. OK, PHP leads the Web server, good!

So what are the common Web servers? The most PHP dogs use is Apache, and there are other:

  • Apache

  • Nginx

  • Iis

  • Lighttpd

  • Tomcat

Basically is the above several, with PHP associated with the most used is Apache and Nginx.

Let's start with an example of Apache as a Web server to illustrate a full PHP access situation:

The picture is a good explanation of PHP and Apache in conjunction with the MySQL database of a completed Web Access flowchart

mod_php mode

It's clear that PHP has to use a Web server to provide the functionality of the Web, and now look at how they became friends.

The most we have used is Apache. So recall, how can Apache be able to recognize the PHP code? is not the Apache configuration file httpd.conf Add or modify such a few words:

Add the following 2 sentences loadmodule php5_module d:/php/php5apache2_2.dlladdtype application/x-httpd-php. php//
 
  
   
      the following DirectoryIndex index.html
 
  //Modify it to:
 
  
   
      directoryindex index.html index.htm index.php index.phtml
 
  

The above Windows under the installation of PHP and Apache environment after the manual configuration, under the Linux source installation is basically configured like this:

./configure--with-mysql=/usr/local--with-apache=/usr/local/apache--enable-track-vars

So, in this way, their common essence is to use LoadModule to load php5_module, that is, PHP as a sub-module of Apache to run. When PHP files are accessed through the Web, Apache invokes Php5_module to parse the PHP code.

So how does php5_module send the data to the PHP parser to parse the PHP code?

The answer is through SAPI.

Let's take a look at a picture, in detail about the relationship between Apache and PHP and SAPI:

From the above picture, we see that SAPI is such an intermediate process, SAPI provides an interface with the external communication, a bit similar to the socket, so that PHP can interact with other applications data (APACHE,NGINX,CLI, etc.). PHP provides many kinds of SAPI by default, common to Apache and Nginx php5_module,cgi, to IIS ISAPI, and to the CLI of the shell.

So, the above Apache calls PHP to execute the process as follows:

PHP sapi, Php5_module, httpd, Apache

All right. Apache and PHP through the Php5_module way to find out!

We call this mode of Operation Mod_php.

mod_fastcgi mode

Above we have carefully said PHP and Apache through the Php5_module,php5_module through the SAPI way to access PHP, to achieve the entire PHP Web process.

It also says that SAPI,SAPI is a unified interface provided by PHP, which is provided to Php5_module and CGI for Web servers to link and parse PHP code. The above mentioned Php5_module loading mode, which we call mod_php mode.

So! When when to be! I'm going to say fastcgi mode now. hahaha haha, too much.

Then another way PHP SAPI is to provide CGI mode, because the CGI is older so there is a fastcgi to replace it.

So, hey. There is no way, but also to say what is CGI?

CGI (Common Gateway Interface). 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.

The official explanation is that the egg hurts, simply put, is: CGI is specifically used to deal with Web servers. When the Web server receives a request from the user, it submits the request to the CGI program (PHP fastcgi), and the CGI program processes the requested parameters (parsing PHP), then outputs the standard HTML statement back to the Web server and returns it to the client. This is how the common CGI works.

The advantage of CGI is that it is completely independent of any server, just as an intermediate molecule. provides interfaces to Apache and PHP. They use the CGI line to complete the base action. The benefits of doing this are to minimize the association of 2 and make them 2 more independent.

But CGI has a place where the egg hurts, that is, every Web request will have the start and exit process, that is, the most people criticized the Fork-and-execute mode, so that a large-scale concurrency, it is dead-cocked.

So. This time fastcgi the use of the birth. It started early in advance well, and can start a number of CGI modules, there has been running waiting, waiting for the web to send a request, and then to the PHP parsing operation finished generating HTML to the Web, will not quit, and continue to wait for the next Web request. And these CGI module start-up is controllable and can be monitored. This technology also allows Web server and PHP to be run on different hosts to scale up and improve security without losing productivity.

So now the general operating system is fastcgi mode. Cig mode also slowly withdrew from the historical stage! In our article, it is said that CGI generally refers to fastcgi.

So call this mode of Operation mod_fastcgi

I'll talk about how to use fastcgi mode to connect PHP and Apache (or Nginx) in the next paragraph.

To summarize: Php with Apache or ngix combination, will use SAPI to provide 2 ways to connect: mod_php and mod_fastcgi. The mod_php mode installs the PHP module under Apache to run, with a 2 degree of bonding. mod_fastcgi mode is as an intermediate process, Apache introduced user request, sent to fastcgi, and then connect PHP to complete the visit.

The graph shows these 2 modes

mod_php mode

mod_php mode is to install the PHP module into Apache, so every request for Apache to end, will produce a process, the process is complete including all kinds of PHP operation calculation and so on.


We can see very clearly, Apache every request, will produce a process to connect PHP through the SAPI to complete the request, it is conceivable that if the user too many, the number of concurrent, the server will not bear.

Moreover, when the mod_php into Apache, it is difficult to locate the problem is PHP or Apache problem.

mod_fastcgi mode

mod_fastcgi mode is just the opposite, fastcgi is a standalone individual with Apache and PHP, it starts with Apache, generates multiple cig modules, waits for Apache request:

Figure fastcgi Early start good, quietly where to wait, has Apache sent to the HTTPD request immediately received over, by calling Sapi to PHP, complete the operation. and will not quit. This allows for large-scale concurrent requests, because the Web server has fewer things to do, so it is faster to process the next request, which is much more concurrent.

Because Apache and PHP are independent. Problem, very good positioning in the end where the problem. This is one of the reasons why this model is popular.

php-fpm

I'm a big php-fpm, and I'm finally going to talk about it. ^....^

Let's get straight to the point and say PHP-FPM is doing well. It is specifically designed to assist in mode_fastcgi mode.

Well. Very good, first know what it is to do after we go back to mode_fastcgi mode. I've figured out how this pattern is going to look like in the front of a bunch of blind dicks.

FastCGI is a platform-independent, language-independent, any language as long as its interface to implement, you can achieve their own language fastcgi ability and Web server communication.

PHP-CGI is the PHP implementation of the own FASTCGI manager.

Although it is the official PHP production, comes with, but this AH is not a bit of force, performance is too poor, but also very cumbersome and humane, mainly reflected in:

  • php-cgi after changing the php.ini configuration, you need to restart the php-cgi for the new Php-ini to take effect, not to smooth the restart.

  • Directly kill the php-cgi process, PHP will not be able to run.

  • The above 2 questions, has been a lot of people have been sick for a long time, so many people have been still in the mode_php way.

    Until 2004 (sure it was so early?) A call Andrei Nigmatulin Silk invented the php-fpm, the appearance of this artifact completely broke this situation, this is a PHP dedicated fastcgi manager, it is cool to overcome the above 2 problems, but also performance in other aspects more strong. Please stamp the official website

    I wipe, this seems to be blind to say that the time-out AH. All right. The installation configuration for Windows and Linux is php-fpm in the next section. Anyway, I've already made php-fpm and fastcgi clear.

    Source: https://www.zybuluo.com/phper/note/50231

  • Related Article

    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.