_php example based on PHP's operating mode in various Web servers

Source: Internet
Author: User
Tags data structures fpm php script php source code zend
First, PHP operating mode in Apache

PHP in the Apache a total of three ways of working:CGI mode, fastcgi mode, Apache module DLL
The following are compared separately:
1. CGI mode compared to module mode:
The difference between the two ways PHP works in Apache (CGI mode, Apache module DLL)
These two ways of working are installed:
The CGI way of PHP in Apache 2.0
AddType application/x-httpd-php. php
# Use this line for PHP 4
Action application/x-httpd-php "/php/php.exe"
# Use this line for PHP 5
Action application/x-httpd-php "/php/php-cgi.exe"
The modular approach of PHP in Apache 2.0
# Use these two lines for PHP 4:
LoadModule php4_module "C:/php/php4apache2.dll"
# Don't forget to copy the Php4apache2.dll from the SAPI catalogue!
AddType application/x-httpd-php. php
# Use these two lines for PHP 5:
LoadModule php5_module "C:/php/php5apache2.dll"
AddType application/x-httpd-php. php
# Configure the php.ini path
Phpinidir "c:/php"
The difference between the two modes of work:
In CGI mode, if a client requests a PHP file, the Web server invokes Php.exe to interpret the file and then returns the result of the explanation to the client as a Web page;
In a modular (DLL), PHP is started and run with the Web server.
So in a way, the PHP4 installed in the Apache module has better security than the CGI model and better execution efficiency and speed.
2. fastcgi operation mode Analysis:
The working principle of fastcgi is:
(1), when the Web Server starts loading FastCGI process Manager "PHP FastCGI process Manager is PHP-FPM (php-fastcgi process Manager)" (IIS ISAPI or Apache Module);
(2), fastcgi the process manager itself, initiates multiple CGI interpreter processes (visible in Task Manager Php-cgi.exe) and waits for a connection from the Web server.
(3) When the client requests to reach 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 subprocess Php-cgi.exe.
(4) The FASTCGI process completes processing and returns standard output and error information from the same connection to the Web Server. When the fastcgi child process closes the connection, the request is processed. The fastcgi process then waits and processes the next connection from the FASTCGI process Manager (running in webserver). In the normal CGI mode, Php-cgi.exe quits here.
In the above scenario, you can imagine how slow CGI is usually. Each Web request PHP must reparse php.ini, reload all DLL extensions, and reinitialize all data structures. With fastcgi, all of these occur only once when the process is started. An additional benefit is that persistent database connections (persistent DB connection) can work.

3. Why use FASTCGI instead of multithreaded CGI interpreter?
This may be a multifaceted consideration, for example:
(1) You cannot, in any case, stabilize the use of multithreaded CGI interpreters on Windows platforms, whether IIS ISAPI or Apache module, they always run for a period of time and crash. Is that weird? But it does exist!
Of course, there are many times when you can stabilize the use of multithreaded CGI interpreter, but you may find that the Web page sometimes error, in any case can not find the reason, and the fastcgi way to change the probability of this error will be greatly reduced. I'm not sure why, but I think the CGI interpreter for the standalone address space may eventually be a little bit more stable than the form of the shared address space.
(2), Performance! Performance? Is it possible that fastcgi is faster than a multithreaded CGI interpreter? But sometimes this is true, only testing your site, can be final conclusion. Why, I find it difficult to say, but there is information that in the era of Zend Winenabler, Zend is also recommended to use the Windows platform fastcgi instead of IIS ISAPI or Apache Module, but now Zend does not do this product.

4. Advantages of running PHP in FastCGI mode:
There are several main benefits to running PHP in FastCGI mode. The first is that PHP does not break down the Apache when it is wrong, but PHP's own process is lost (but FastCGI will immediately restart a new PHP process to replace the lost process). Second, FastCGI mode runs better than ISAPI mode (I used to test with apachebench, but I forgot to save the results and you are interested in testing it yourself).
Finally, it is possible to run PHP5 and PHP4 at the same time. Refer to the configuration file below to establish two virtual hosts, one using PHP5 and the other using PHP4.
Copy Code code as follows:

LoadModule Fastcgi_module Modules/mod_fastcgi-2.4.2-ap13.dll
Fastcgiserver "D:/usr/local/php-5.0.4/php-cgi.exe"-processes 3
Fastcgiserver "D:/usr/local/php-4.3.11/php.exe"
Listen 80
Namevirtualhost *:80
DocumentRoot d:/www
Options Indexes followsymlinks MultiViews
ServerName Php5.localhost
AddType application/x-httpd-fastphp5. php
Action application/x-httpd-fastphp5 "/fcgi-php5/php-cgi.exe"

Indexoptions fancyindexing Foldersfirst
Options Indexes followsymlinks MultiViews
AllowOverride None
Order Allow,deny
Allow from all

Listen 8080
Namevirtualhost *:8080

DocumentRoot d:/www
Options Indexes followsymlinks MultiViews
ServerName Php4.localhost
AddType application/x-httpd-fastphp4. php
Action application/x-httpd-fastphp4 "/fcgi-php4/php.exe"

Options Indexes followsymlinks MultiViews
AllowOverride None
Order Allow,deny
Allow from all

Using the configuration above, access to http://localhost/uses PHP5, while access to http://localhost:8080/uses PHP4. So as long as a reasonable configuration, you can have different virtual host to use different versions of PHP.
Some drawbacks of the FastCGI model:
Said the good, also said the shortcoming. From my actual use, the FastCGI mode is more suitable for the server in the production environment. But for the development of the machine is not very appropriate. Because when the Zend Studio debugger is used, FastCGI returns a 500 error on the page because it thinks the PHP process timed out. This is very annoying, so I switched back to ISAPI mode on the development machine.
Finally, there is a potential for an FastCGI mode in Windows

Second, PHP in the Nginx operating mode (NGINX+PHP-FPM) the current ideal choice

The use of FastCGI method is now common in two kinds of stack:ligthttpd+spawn-fcgi; The other is NGINX+PHP-FPM (also can use spawn-fcgi).
(1) As mentioned above, both of these structures are supported by fastcgi for PHP, so the httpserver is completely liberated and can be better responsive and concurrent processing. So lighttpd and Nginx all have small, but powerful and efficient reputation.

(2) The two can also be divided into a good or bad, spawn-fcgi because it is part of the LIGHTTPD, so installed lighttpd will generally use spawn-fcgi to support PHP, But at present, some users say that ligttpd spwan-fcgi in high concurrent access, will appear above the memory leak or even automatically restart fastcgi. That is, the PHP script processor is a machine, this time if the user access, may appear white page (that is, PHP can not be resolved or error).

Another: First nginx is not like the lighttpd itself contains fastcgi (spawn-fcgi), so it is completely lightweight, must rely on Third-party fastcgi processor to be able to parse the PHP, so in fact, it seems that nginx is very flexible, It can be connected to any Third-party provider with a resolver to enable parsing of PHP (easily set in nginx.conf).

Nginx can use spwan-fcgi (you need to install lighttpd together, but you need to avoid ports for Nginx, some older blogs have tutorials on this), but because spawn-fcgi has the bugs that the user described above is gradually discovered, Now slowly reduce the use of nginx+spawn-fcgi combination.

C. As a result of the spawn-fcgi flaw, a new third party (now still, heard that efforts are being made to join PHP core in the near future), is called the FastCGI processor, known as PHP-FPM (specifically Google). Compared with spawn-fcgi, it has the following advantages:

Because it is used as a PHP patch patch to develop, the installation of the need to compile with the PHP source code, that is, compiled into the core of PHP, so in the performance of some good;
It also outperforms spawn-fcgi in handling high concurrency, at least not automatically restarting FASTCGI processors. The specific algorithm and design can be understood by Google.

Therefore, as mentioned above because of the lightweight and flexibility of nginx, so the current performance is superior, more and more people gradually use this combination: NGINX+PHP/PHP-FPM
Iii. iis+ ISAPI Mode This pattern is suitable for the development environment and is less used in the production environment.

Iv. Summary
At present in Httpserver this piece basically can see three kinds of stack more popular:
(1) apache+mod_php5
(2) lighttp+spawn-fcgi
After the three, the performance may be slightly superior, but Apache because of the rich modules and functions, is still the eldest. Some people test nginx+php-fpm in high concurrency conditions may reach apache+mod_php5 5~10 times, now nginx+php-fpm use more and more.

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.