[Go]php running mode on various Web servers

Source: Internet
Author: User
Tags php source code zend

First, PHP in the Apache in run mode

PHP has three modes of operation in Apache: CGI mode, fastcgi mode, Apache module DLL)

The following are respectively compared:

1. CGI mode vs. module mode comparison:

PHP differences between two modes of operation in Apache (CGI mode, Apache module DLL)
Installation of these two modes of operation:

The CGI mode of PHP in Apache 2.0
AddType application/x-httpd-php. php

# for PHP 4 Use this line
Action application/x-httpd-php "/php/php.exe"

# for PHP 5 Use this line
Action application/x-httpd-php "/php/php-cgi.exe"
PHP module mode in Apache 2.0
# for PHP 4 Use these two lines:
LoadModule php4_module "C:/php/php4apache2.dll"
# Don't forget to copy the Php4apache2.dll from the SAPI directory!
AddType application/x-httpd-php. php
# for PHP 5 use these two lines:
LoadModule php5_module "C:/php/php5apache2.dll"
AddType application/x-httpd-php. php

# Configure the path to the php.ini
Phpinidir "c:/php"

The difference between these two ways of working:
In CGI mode, if the client requests a PHP file, the Web server calls 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 from a certain point of view, the PHP4 installed in Apache module has better security and better execution efficiency and speed than CGI mode.

2. FastCGI operation mode Analysis:

How the FastCGI works is:

(1), the Web Server starts loading the FastCGI process Manager "PHP FastCGI process Manager is PHP-FPM (php-fastcgi processes manager)" (IIS ISAPI or Apache Module) ;
(2), the FASTCGI process manager itself initializes, initiates multiple CGI interpreter processes (multiple Php-cgi.exe visible in Task Manager) and waits for a connection from the Web server.
(3), when a client request arrives at 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.exe.
(4) After the fastcgi process finishes processing, the standard output and error messages are returned from the same connection to the Web Server. When the fastcgi child process closes the connection, the request is processed to completion. The fastcgi child process then waits and processes the next connection from the FASTCGI process Manager (running in webserver). In the normal CGI mode, php-cgi.exe exits here.

In the above scenario, you can imagine how slow CGI is usually. Every Web request PHP must re-parse php.ini, reload all DLL extensions, and initialize all data structures. With fastcgi, all of this occurs only once when the process is started. An additional benefit is that the persistent database connection (persistent connection) can work.

3. Why use FastCGIinstead of a multithreaded CGI interpreter?
This may be due to a variety of considerations, such as:
(1), you can not in any case on the Windows platform stable use of multithreaded CGI interpreter, either IIS ISAPI mode or Apache module mode, they always run for a period of time crashed. Strange, huh? But there is such a situation!
Of course, there are a lot of times you can use the multi-threaded CGI interpreter, however, you may find that the Web page sometimes error, no matter how to find the reason, and in the fastcgi way when the probability of this error will be greatly reduced. I don't know why, I think the CGI interpreter for the standalone address space may eventually be a little bit more stable than the shared address space.
(2), Performance! Performance? Is it possible that fastcgi is faster than a multithreaded CGI interpreter? But sometimes it is, only test your site, to conclude. Why, I find it difficult to say, but there is information in the Zend Winenabler era, Zend is also recommended to use the Windows platform under the fastcgi rather than the IIS ISAPI or Apache Module, but now Zend do not do this product.

4. Advantages of running PHP in FastCGI mode:

There are several major benefits of running PHP in FastCGI mode. The first is that PHP will not break down when the error occurs, but PHP's own process when it fell (but FastCGI will immediately restart a new PHP process to replace the lost process). Next FastCGI mode to run PHP better than ISAPI mode performance (I originally tested with apachebench, but forgot to save the results, we are interested to test themselves).

Finally, you can run PHP5 and PHP4 at the same time. Refer to the following configuration file to establish two virtual hosts, one using PHP5 and the other using PHP4.

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

With the above configuration, access to http://localhost/uses PHP5, while access to http://localhost:8080/uses PHP4. So as long as the reasonable configuration, you can have different virtual host to use different versions of PHP.

Some disadvantages of the FastCGI model:

Say the good, also say the shortcomings. From my actual use, using FastCGI mode is more suitable for the production environment of the server. But it's not very suitable for development machines. Because when using the Zend Studio debugger, the PHP process is returned with a 500 error on the page because FastCGI will think it timed out. This was very annoying, so I switched back to ISAPI mode on the development machine.

Finally, there is a potential for FastCGI mode in Windows

two , PHP in the Nginx in run mode (NGINX+PHP-FPM) The ideal choice now

There are two kinds of stack:ligthttpd+spawn-fcgi that are commonly used in fastcgi mode; The other is NGINX+PHP-FPM (can also be used with spawn-fcgi).

(1) As mentioned above, both structures are supported by fastcgi for PHP, so Httpserver is completely free and can be better responsive and concurrent processing. So lighttpd and Nginx have small, but powerful and efficient reputation.

(2) The two can also distinguish a good or bad, spawn-fcgi because it is part of the LIGHTTPD, so installed lighttpd generally will use spawn-fcgi to PHP support, But now there are users said LIGTTPD spwan-fcgi in high concurrent access, the above mentioned memory leak or even automatic restart fastcgi. That is: PHP script processor When the computer, this time if the user access, may appear white page (that is, PHP can not be parsed or error).

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

Nginx can use spwan-fcgi (need to install lighttpd together, but need to avoid the port for Nginx, some earlier blog has this aspect of the installation of the tutorial), but because spawn-fcgi with the above mentioned the user gradually discovered defects, Now slowly reduce the use of nginx+spawn-fcgi combination.

C. Due to SPAWN-FCGI's shortcomings, a new third party (currently known as the one that is trying to join PHP core in the near future) is now appearing as a fastcgi processor called PHP-FPM (specific to Google). Compared with spawn-fcgi, it has the following advantages:

Since it was developed as a patch patch for PHP, the installation needs to be compiled with the PHP source code, that is, compiled into the PHP core, so in terms of performance should be excellent;

It also outperforms spawn-fcgi in handling high concurrency, at least not automatically restarting the FASTCGI processor. The specific algorithm and design can be understood by Google.

Therefore, as mentioned above because of the light weight and flexibility of nginx, so at present superior performance, more and more people gradually use this combination: NGINX+PHP/PHP-FPM

Third,iis+ ISAPIMode

This model is suitable for development environments and is less used in production environments.

Four, Summary

At present, there are three kinds of stacks that can be seen in Httpserver:

(2) lighttp+spawn-fcgi

After all, the performance may be slightly better, but Apache is still the boss because of its rich modules and functions. Some people test that NGINX+PHP-FPM may reach apache+mod_php5 5~10 times in high-concurrency situations, and now nginx+php-fpm use more and more.

Transferred from: http://www.cnblogs.com/jhg123/archive/2011/06/02/2068621.html

[Go]php running mode on various Web servers

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.