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

Source: Internet
Author: User
Tags zend

PHPInApacheThere are three ways to work:CGIWhat are patterns, FastCGI, FastCGI?

FastCGI is a language-independent, scalable architecture.CGIThe main behavior of an open extension is toCGIThe interpreter process remains in memory and therefore gets high performance. KnownCGIThe repeated loading of the interpreter isCGIThe main reason for poor performance, ifCGIThe interpreter remains in memory and accepts the FASTCGI process Manager schedule, which provides good performance, scalability, fail-over characteristics, and so on.

FastCGI's official site is loaded into the FASTCGI Process Manager (IIS ISAPI or http://www.fastcgi.com 1, Web Server startup).ApacheModule);

2, FASTCGI process Manager itself initialization, start multipleCGIThe interpreter process (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 aCGIInterpreter. Web Server willCGIenvironment variables and standard inputs are sent to the fastcgi child process Php-cgi.exe.

4. After the fastcgi process finishes processing, the standard output and error information 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 normalCGImode, Php-cgi.exe exits here.

In the above scenario, you can imagineCGIHow slow it usually is. Every single Web requestPHPMust 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.

Second, why use fastcgi, rather than multithreadingCGIThe 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 multithreadingCGIInterpreter, either IIS ISAPI mode orAPACHEModule mode, they always run for a period of time to crash. Strange, huh? But there is such a situation!

Of course, there are many times when you can use multithreading in a stableCGIInterpreter, however, you may find that Web pages sometimes have errors that can be found no matter what, and the probability of this error is greatly reduced when swapping the fastcgi method. I don't know why, I think the independent address spaceCGIThe interpreter may eventually be a little bit more stable than the shared address space.

2, Performance! Performance? Maybe, isn't fastcgi more than multithreadingCGIInterpreter faster? 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 under the Windows platform fastcgi instead of IIS ISAPI orApacheModule, but now Zend is not doing this product anymore.

   PHPInApacheThere are three ways to work:CGImode, fastcgi mode,ApacheModule DLL)

The following are respectively compared:

OneCGIMode vs. module mode comparison:

   PHPInApacheThe difference between the two modes of work (CGIModeApacheModule DLL)

Installation of these two modes of operation:

   PHPInApache2.0 in theCGIWay

Scriptalias/PHP/"c:/PHP/"

AddType application/x-httpd-php.PHP

# YesPHP4 Use this line

Action application/x-httpd-php "/PHP/php.exe "

# YesPHP5 Use this line

Action application/x-httpd-php "/PHP/php-cgi.exe "

   PHPInApacheModule Mode in 2.0

# YesPHP4 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

# YesPHP5 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:

InCGImode, if the client requests aPHPfile, 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) format,PHPis started and run with the Web server.

So from a certain point of view, toApacheModule-style InstallationPHP4 has a ratioCGImode for better security and better execution efficiency and speed.

Second, fastcgi operation mode Analysis:

FastCGI mode operationPHPThe advantages of:

Run in FastCGI modePHPThere are several major benefits. The first isPHPWhen things go wrong, they don't break downApacheIt's just thatPHPOwn process when it is lost (but FastCGI will immediately restart a newPHPProcess to replace the lost process). Second FastCGI mode operationPHPBetter performance than ISAPI mode (I used apachebench to test, but forgot to save the results, everyone is interested to test it yourself).

Finally, it's possible to run simultaneouslyPHP5 andPHP4. Refer to the following configuration file to establish two virtual hosts, one of which usesPHP5, another usePHP4.

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

Using the configuration above, access to http://localhost/is usedPHP5, while access to http://localhost:8080/is usedPHP4. So as long as the reasonable configuration, you can let different virtual host use different versions of thePHP。 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 security vulnerability in FastCGI mode in Windows. Because I haven't found a way to implement suexec in a Windows environment, PHP 's process always runs with the highest privileges, which is obviously not good news from a security standpoint. Sorry, because you posted the violation information in NetEase album, the account is blocked. No one else can access your albums while they are blocked.

Go to the Help center and learn how to re-restore the service.

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

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.