FastCGI incomplete Advanced Guide (PHP version, Windows platform)

Source: Internet
Author: User
FastCGI is a CGI open extension in a language-independent and scalable architecture. its main behavior is to maintain the CGI interpreter process in the memory and thus achieve high performance. As we all know, repeated loading of CGI interpreters is the main cause of low CGI performance.

I. What is FastCGI?

FastCGI is a CGI open extension in a language-independent and scalable architecture. its main behavior is to maintain the CGI interpreter process in the memory and thus achieve high performance. As we all know, repeated loading of the CGI interpreter is the main cause of low CGI performance. if the CGI interpreter is kept in the memory and is scheduled by the FastCGI Process Manager, it can provide good performance, scalability, Fail-Over features, and so on.

FastCGI site: http://www.fastcgi.com

FastCGI works as follows:

1. when the Web Server is started, load the 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 a 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.

2. why use FastCGI instead of multi-thread CGI interpreter?

This may be due to multiple considerations, such:

1. you cannot use the multi-threaded CGI interpreter on windows platform in any way. whether it is iis isapi or APACHE Module, they will 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.

III. Reasons for not using FastCGI

1 Gbit/s multi-path consumes more memory than multi-thread. the php-cgi.exe interpreter consumes 7 to 25 Mbit/s of memory per process. Multiply this number by 50 or 100.

2. performance. Sometimes the multi-thread CGI interpreter is faster, and sometimes it is stable.

3. CGI? It sounds pretty earthy, huh, huh

IV. IIS FastCGI configuration method

1. First, make sure that you have correctly installed PHP 4.3.x and the updated version. Earlier versions of PHP are not supported by FastCGI by default. if you want to work in earlier versions, you need to recompile it. Suppose PHP is installed in c: PHP, and the executable file supporting fastfcgi.exe is php-cgi.exe.

Note: We recommend that you disable cgi. force_redirect in Php. ini, enable fastcgi. impersonate, and enable cgi. rfc2616_header.

2. Download The Secret (not in this directory, but for convenience ).

3rd, use regedit.exe to create the following Registry key:

HKEY_LOCAL_MACHINE: SoftwareFASTCGI. php (required)

4. create the following key values under this item:

String type: AppPath; value: c: phpphp-cgi.exe (required)

String type: BindPath; value: php-fcgi (required)

The following are optional configuration key values:

DWORD type: StartServers. the number of interpreters started by default at startup. the default value is 5.

DWORD type: MaxServers. The maximum number of interpreters. the default value is 25.

DWORD type: IncrementServers. the number of increments when the interpreter is insufficient. the default value is 2.

DWORD type: Timeout, incremental interpreter (beyond the number of StartServers) survival time, default 600 (seconds)

DWORD type: ThreadPoolSize, thread pool size, valid only under IIS, default 10

DWORD type: Impersonate, which is valid only for IIS. if it is set to 1, use the IIS security flag. if it is set to 0, disable this feature. Do not close it unless you are not worried about security issues. Default value: 1

DWORD type: MaxPostData, Post data pre-read Byte limit, default 0

DWORD type: BypassAuth, which is valid only for IIS. if it is set to 1 and isapi_fcgi.dll is configured as IIS Filter, and IIS is configured to use BASIC Authentication, all Authentication requests are forced to use IIS anonymous users. This option aims to allow scripts to implement their own security mechanisms. Default value: 0

BINARY type: CustomVars, additional environment variable values, new lines separated, Null ended

5. if it is IIS6, add a Web service extension pointing to c: phpisapi_fcgi.dll and allow. The maximum number of working processes in the application pool must be 1.

6. add the extended ing relationship of the application:

1) in Internet information service manager, select the root directory of the website or application.

2) open the directory properties Page (right-click and select "properties"), and then select "main directory ".

3) click "configure" and select the "ing" Tab.

4 ). click "Add... ", set the executable file to c: phpisapi_fcgi.dll, and the extension. php, be sure to select "check whether the file exists", and then "OK" to save the settings.

5). also add support for. php3 or. phtml extensions (optional ).

6). Save the settings and restart IIS.

In the 7th Test, multiple web pages are requested at the same time. check the progress in the Task Manager. after the page is complete, the php-cgi.exe process continues to run and does not exit.

V. Apache configuration method

1. First, make sure that you have correctly installed PHP 4.3.x and the updated version. Earlier versions of PHP are not supported by FastCGI by default. if you want to work in earlier versions, you need to recompile it. Suppose PHP is installed in c: PHP, and the executable file supporting fastfcgi.exe is php-cgi.exe.

Note: We recommend that you enable cgi. force_redirect in Php. ini, disable fastcgi. impersonate, and disable cgi. rfc2616_headers.

2. download http://www.fastcgi.com/dist/mod_fastcgi-2.4.2-ap1_dllto the Modules directory of Apache 2.x.

3. make sure Apache 2.x can run PHP normally in CGI mode. Httpd. conf contains the following lines:

  1. ScriptAlias/php/"c:/php /"
  2. Action application/x-httpd-php "/php/php-cgi.exe"
  3. SetEnv PHPRC "C:/php"
  4. AddType application/x-httpd-php. php

4. add the following in httpd. conf:

  1. LoadModule fastcgi_module modules/mod_fastcgi-2.4.2-AP20.dll
  2. # Note: Here-processes 3 indicates to start three php-cgi.exe processes,
  3. # For detailed FastCgiServer parameters, see The FastCGI document.
  4. FastCgiServer "c:/php/php-cgi.exe"-processes 3

5. restart Apache. the test is the same as above.

VI. Advanced Configuration

Imagine a scenario where your server runs Apache 2 and IIS 6 at the same time, and both Web servers run php applications. There are three possibilities:

A. php is installed using iis isapi and apache module, and runs in multiple threads. This is irrelevant to FastCGI.

B. One server uses FastCGI and the other uses multithreading. This operation is normal.

C. Both use the FastCGI method, which is often abnormal. Generally:

The Progress (terrible ).

Why? I think it is probably because the author of the above two things (iis isapi and apache module) has never thought about using them with the other party. Think about it. if Apache 2 and IIS 6Can use a batch of php-cgi.exe interpreters, there will be no memory waste or scheduling problems.

Research and tests have proved that this idea is feasible. However, because Shane Caraveo does not use the fastcgiservice feature in ISAPI dll, iisis responsible for starting and managing php-cgi.exe, and then configuring Apache to use these iismanaged php-cgi.exe processes.

Configuration method:

1. configure IIS FastCGI as follows.

2. configure Apache FastCGI in the preceding five steps, and add the following in httpd. conf:

  1. LoadModule fastcgi_module modules/mod_fastcgi-2.4.2-AP20.dll
  2. # Use the external FastCGI server. For more information, see The FastCGI document.
  3. FastCgiExternalServer "c:/php/php-cgi.exe"-socket "php-fcgi"

Note: the value after-socket must be HKEY_LOCAL_MACHINE: success.

Note: In this configuration, the php-cgi.exe process is only managed by the FastCGI Process Manager in IIS. the busy request of apachedoes not allow fastcgi.exe in iisto dispatch more php-cgi.exe processes. Therefore, when FastCGI is configured in IIS, the StartServers value should be large enough to avoid insufficient php interpreters. If IIS is disabled, Apache cannot find the Php interpreter.

The following problem occurs: in php. ini, how should cgi. force_redirect, fastcgi. impersonate, and cgi. rfc2616_headers be set? Let's think about this ...... Haha

Another question that can be encountered is that iisis very idle, and after a while, php-cgi.exe started by iishas exited, apache cannot explain Php. what should I do? At this time, you can initialize the iis, and php-cgi.exe will get up again, dizzy. We recommend that you use the process pool management of IIS 6 to disable "idle timeout" in the application pool, in the application pool ID, set the running account to be consistent with the Apache service startup account.


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.