Php-based running modes on various web servers _ PHP Tutorial

Source: Internet
Author: User
Tags apache php
Detailed description of the php-based running modes on various web servers. I. php running mode in apache php has three working modes in apache: CGI mode, FastCGI mode, and Apache module DLL: 1. Comparison of CGI mode and module mode: I. php running mode in apache

Php is common in apache. Three working modes: CGI mode, FastCGI mode, and Apache module DLL
Comparison:
1. Comparison between CGI mode and module mode:
Difference between the two working methods of php in apache (CGI mode and Apache module DLL)
Install these two methods:
CGI mode of PHP in Apache 2.0
ScriptAlias/php/"c:/php /"
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"
Module mode of PHP in Apache 2.0
# Use these two lines for PHP 4:
LoadModule php4_module "c:/php/php4apache2. dll"
# Do not forget to copy php4apache2. dll from the sapi directory!
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 ways of work:
In the cgimode, if the client requests a php file, the webserver calls php.exe to explain the file, and then returns the result to the client in the form of a webpage;
In modularization (DLL), PHP is started and run together with the Web server.
From a certain point of view, the PHP4 installed using the apache module has better security and better execution efficiency and speed than the CGI mode.
2. FastCGI running mode analysis:
FastCGI works as follows:
(1) load FastCGI Process Manager when Web Server is started [PHP FastCGI Process Manager is a PHP-FPM (php-FastCGI Process Manager)] (iis isapi or Apache Module );
(2) FastCGI Process Manager initializes itself, starts multiple CGI interpreter processes (multiple php-cgi.exe in the Task Manager), and waits for a connection from the Web Server.
(3) when the client request arrives at the Web Server, the FastCGI process manager selects and connects to a CGI interpreter. Web server sends the cgienvironment variable and standard input to php-cgi.exe.
(4) after the FastCGI sub-process completes processing, the standard output and error messages are returned from the same connection to the Web Server. When the FastCGI sub-process closes the connection, the request processing is complete. The FastCGI sub-process then waits for and processes the next connection from the FastCGI Process Manager (running in WebServer. In the normal cgimode, php-cgi.exe exits.
In the above cases, you can imagine how slow CGI is. Every Web request PHP must re-parse php. ini, re-load all dll extensions, and initialize all data structures. With FastCGI, all of these occur only once when the process starts. An additional benefit is that Persistent database connection can work.

3. Why use FastCGI instead of multi-thread CGI interpreter?
This may be due to multiple considerations, such:
(1) you cannot use a multi-threaded CGI interpreter on windows platform in any way. whether it is iis isapi or APACHE Module, they always crash after a period of running. Strange? But this is indeed the case!
Of course, you may also be able to use the multi-thread CGI interpreter in a stable manner. However, you may find that web pages sometimes encounter errors and you cannot find the cause in any case, the probability of such errors is greatly reduced when FastCGI is used. I don't know why. I think the CGI interpreter of an independent address space may be a little more stable than the shared address space.
(2) performance! Performance? Is it possible that FastCGI is faster than the multi-thread CGI interpreter? However, sometimes this is the case. only by testing your website can you draw a conclusion. I think it is difficult to explain the reason, but I have some information that Zend, in the Zend WinEnabler era, also suggested using FastCGI on Windows platforms rather than IIS ISAPI or Apache Module, however, Zend is no longer using this product.

4. advantages of running PHP in FastCGI mode:
There are several main advantages of running PHP in FastCGI mode. First, Apache will not be destroyed when PHP errors occur, but PHP's own processes will be destroyed (but FastCGI will immediately restart a new PHP process to replace the failed process ). Second, FastCGI runs PHP in better performance than ISAPI (I used apacheloud for testing, but forgot to save the results. if you are interested, you can test it on your own ).
Finally, you can run PHP5 and PHP4 at the same time. Refer to the following configuration file to create two virtual hosts, one using PHP5 and the other using PHP4.

The code is as follows:


LoadModule fastcgi_module modules/mod_fastcgi-2.4.2-AP13.dll
ScriptAlias/fcgi-php5/"d:/usr/local/php-5.0.4 /"
FastCgiServer "d:/usr/local/php-5.0.4/php-cgi.exe"-processes 3
ScriptAlias/fcgi-php4/"d:/usr/local/php-4.3.11 /"
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


Use the above configuration to access http: // localhost/and use PHP5, while access http: // localhost: 8080/uses PHP4. Therefore, different virtual hosts can use different versions of PHP.
Some disadvantages of FastCGI mode:
Let's talk about the advantages and disadvantages. In my practical use, FastCGI is more suitable for servers in the production environment. But it is not suitable for development machines. When Zend Studio is used to debug a program, FastCGI considers that the PHP process times out and returns error 500 on the page. This is annoying, so I switched back to the ISAPI mode on the development machine.
Finally, there is a potential security problem in FastCGI mode in Windows.

2. php running mode in nginx (nginx + PHP-FPM) is now an ideal choice

The FastCGI method is commonly used in two types of stack: ligthttpd + spawn-fcgi; the other is nginx + PHP-FPM (spawn-fcgi can also be used ).
(1) As mentioned above, both structures adopt FastCGI to support PHP. Therefore, HTTPServer is completely freed to better respond to and concurrently process requests. Therefore, both lighttpd and nginx have the reputation of small, but powerful and efficient.

(2) the two can also distinguish between good and bad. since spawn-fcgi is a part of lighttpd, lighttpd is installed with spawn-fcgi to support php, however, some users say that when the spwan-fcgi of ligttpd is accessed in high concurrency, the memory leakage mentioned above may even automatically restart fastcgi. That is, if the PHP script processor is on the machine and the user accesses it at this time, there may be white pages (that is, PHP cannot be parsed or error ).

Another: nginx does not have lighttpd including fastcgi (spawn-fcgi). Therefore, it is completely lightweight and can be parsed using a third-party FastCGI processor, as a result, nginx is very flexible. it can be connected to any third-party processor for parsing to achieve PHP parsing (in nginx. conf ).

Nginx can use spwan-fcgi (lighttpd needs to be installed together, but the Port needs to be avoided for nginx. some earlier blogs have Installation Tutorials in this regard ), but since spawn-fcgi has the defects gradually discovered by users described above, it is now slowly reducing the use of nginx + spawn-fcgi combinations.

C. due to spawn-fcgi defects, a new third-party PHP FastCGI processor (currently, I heard it is working hard to join PHP core in the near future) has emerged, is called PHP-FPM (can google ). Compared with spawn-fcgi, it has the following advantages:

Because it is developed as a PHP patch, it needs to be compiled together with the php source code during installation, that is, compiled into the php core, so it should be better in terms of performance;
At the same time, it is superior to spawn-fcgi in terms of processing high concurrency, at least it will not automatically restart the fastcgi processor. Google can understand the specific algorithms and designs used.

Therefore, as mentioned above, nginx is simple and flexible, so the current performance is superior, more and more people gradually use this combination: nginx + PHP/PHP-FPM
III. The IIS + ISAPI mode is suitable for the development environment, but is rarely used in the production environment.

IV. Summary
Currently, three types of stacks are quite popular in HTTPServer:
(1) Apache + mod_php5
(2) lighttp + spawn-fcgi
(3) nginx + PHP-FPM
The performance may be slightly better between the two, but Apache is still the boss because of its rich modules and functions. Some people test nginx + PHP-FPM in high concurrency may reach Apache + mod_php5 5 ~ 10 times, now more and more people use nginx + PHP-FPM.

Php works in three ways in apache: CGI mode, FastCGI mode, and Apache module DLL: 1. Comparison of CGI mode and module mode :...

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.