Home  >  Article  >  Backend Development  >  FastCGI Incomplete Advanced Guide (PHP Version, Windows Platform)_PHP Tutorial

FastCGI Incomplete Advanced Guide (PHP Version, Windows Platform)_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:52:08786browse



Question
FastCGI Incomplete Advanced Guide (PHP version, Windows platform)
Solution
1. What is FastCGI?
FastCGI is a language-independent, scalable architecture CGI open extension. Its main behavior is to keep the CGI interpreter process in memory and thus obtain higher performance. As we all know, repeated loading of the CGI interpreter is the main reason for low CGI performance. If the CGI interpreter remains in memory and accepts FastCGI process manager scheduling, it can provide good performance, scalability, Fail-Over features, etc.
The official site of FastCGI is at [url=http://www.fastcgi.com/]The link is marked http://www.fastcgi.com[/url]

The working principle of FastCGI is:
1. Load the FastCGI process manager (IIS ISAPI or Apache Module) when the Web Server starts;
2. The FastCGI process manager initializes itself, starts multiple CGI interpreter processes (multiple php-cgi.exe can be seen in the task manager) and waits for the connection from the Web Server.
3. When a client request reaches 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. After the FastCGI sub-process completes processing, it returns standard output and error information to the Web Server from the same connection. When the FastCGI child process closes the connection, the request is processed. The FastCGI child process then waits for and handles the next connection from the FastCGI process manager (running in WebServer). In normal CGI mode, php-cgi.exe exits here.

In the above case, you can imagine how slow CGI is usually. Every web request to PHP must re-parse php.ini, reload all dll extensions and re-initialize all data structures. With FastCGI, all of this happens only once, when the process starts. An added bonus is that persistent database connections work.

2. Why use FastCGI instead of a multi-threaded CGI interpreter?
This may be due to many considerations, such as:
1. You cannot use a multi-threaded CGI interpreter stably on the Windows platform anyway. Whether it is the IIS ISAPI method or the APACHE Module method, they always crash after running for a period of time. Is it strange? But such a situation does exist!
Of course, there are many times when you can use a multi-threaded CGI interpreter stably. However, you may find that the web page sometimes has errors, and you can't find the reason anyway. When you switch to FastCGI, the probability of such errors will be greatly increased. of reduction. I don't know why this is, I think the CGI interpreter with independent address space may be a little more stable than the shared address space form after all.
2. Performance! performance? Is it possible, is FastCGI faster than a multi-threaded CGI interpreter? But sometimes it is true, and you can only make a final conclusion by testing your website. The reason, I find it difficult to explain, but there is information that in the era of Zend WinEnabler, Zend originally recommended using FastCGI instead of IIS ISAPI or Apache Module under the Windows platform, but now Zend no longer makes this product.

3. Reasons not to use FastCGI
1. Multi-process consumes more server memory than multi-thread. The php-cgi.exe interpreter consumes 7 to 25 megabytes of memory per process. Multiply this number by 50 or 100 and try.
2. Performance. It is true that sometimes multi-threaded CGI interpreters are faster, haha, and sometimes, it is also very stable.
3. CGI? It sounds very tacky, haha

4. IIS FastCGI configuration method
1. First make sure you have correctly installed PHP 4.3.x and newer versions. Earlier versions of PHP did not include FastCGI support by default. If you want to work in an earlier version, you need to recompile it. We assume that PHP is installed in c:php and the executable file name that supports FastFCGI is php-cgi.exe.
Note: It is recommended to turn off cgi.force_redirect in Php.ini, enable fastcgi.impersonate, and enable cgi.rfc2616_header

2. Download [url=http://www.caraveo.com/fastcgi/fastcgi-0.6.zip] link tag http://www.caraveo.com/fastcgi/fastcgi-0.6.zip[/url] and put it in Unzip the isapi_fcgi.dll to the c:php directory (it does not have to be in this directory, it is just described here for convenience).

3. Use regedit.exe to create the following registry keys:
​HKEY_LOCAL_MACHINE:SoftwareFASTCGI.php​ (required)

4. Create the following key value under this item:
String type: AppPath, value is c:phpphp-cgi.exe (required)
String type: BindPath, value is php-fcgi (required)
The following are optional configuration key values:
DWORD type: StartServers, the number of interpreters started by default at startup, default value 5
DWORD type: MaxServers, maximum number of interpreters, default 25
DWORD type: IncrementServers, the number of increments when the interpreter is not enough, default 2
DWORD type: Timeout, incremental interpreter (exceeding the number of StartServers) survival time, default 600 (seconds)
DWORD type: ThreadPoolSize, thread pool size, only valid under IIS, default 10
DWORD type: Impersonate, only valid for IIS. If it is 1, use the IIS security flag. If it is 0, turn off this feature. Do not turn it off unless you are not concerned about security issues. Default 1
DWORD type: MaxPostData, Post data pre-read Byte limit, default 0
DWORD type: BypassAuth, only valid for IIS. If it is 1 and isapi_fcgi.dll is configured as IIS Filter, and IIS is configured to use BASIC Authentication, this will force all authentication requests to use IIS anonymous users. The purpose of this option is to allow scripts to implement their own security mechanisms. Default 0
BINARY type: CustomVars, additional environment variable values, separated by new lines, terminated by Null

5. If it is IIS6, add a Web service extension pointing to c:phpisapi_fcgi.dll and allow it. Please keep the "Maximum number of worker processes" in the application pool at 1.

6. Add application extension mapping relationship:
1). In Internet Information Services Manager, select the root directory of the website or application.
2). Open the directory property page (right-click and select "Properties"), and then select "Home Directory".
3). Click the "Configuration" button and select the "Mapping" tab page.
4). Click "Add...", set the "Executable File" to: c:phpisapi_fcgi.dll, and set the extension to .php. Be sure to select "Confirm whether the file exists", and then "OK" to save the settings.
5). Also add support for the .php3 or .phtml extension (optional).
6). Save settings and restart IIS.

7. Test it by requesting multiple Web pages at the same time, and then check the process in the task manager. After the page is completed, the php-cgi.exe process continues to run and does not exit.

5. Apache configuration method
1. First make sure you have correctly installed PHP 4.3.x and newer versions. Earlier versions of PHP did not include FastCGI support by default. If you want to work in an earlier version, you need to recompile it. We assume that PHP is installed in c:php and the executable file name that supports FastFCGI is php-cgi.exe.
Note: It is recommended to turn on cgi.force_redirect, turn off fastcgi.impersonate, and turn off cgi.rfc2616_headers in Php.ini.

2. Download [url=http://www.fastcgi.com/dist/mod_fastcgi-2.4.2-AP20.dll] link tag http://www.fastcgi.com/dist/mod_fastcgi-2.4.2-AP20. dll[/url] and put it in the Modules directory of Apache 2.x.

3. Make sure that Apache 2.x can run PHP normally in CGI mode. The following lines exist in httpd.conf:
  ScriptAlias ​​/php/ "c:/php/"
Action application/x-httpd-php "/php/php-cgi.exe"
SetEnv PHPRC "C:/php"
  AddType application/x-httpd-php .php

4. Add:
to httpd.conf LoadModule fastcgi_module modules/mod_fastcgi-2.4.2-AP20.dll
​​ # Note: -processes 3 here means starting three php-cgi.exe processes,
​​ # For detailed parameters of FastCgiServer, please refer to the FastCGI documentation.
  FastCgiServer "c:/php/php-cgi.exe" -processes 3

5. Restart Apache and test as above.

6. Advanced configuration
Imagine a scenario where Apache 2 and IIS 6 are running on your server at the same time, and both web servers are running php applications. Then, there are three possibilities:
A. PHP is installed using iis isapi and apache module, both of which run in multi-threaded mode. This has nothing to do with FastCGI.
B. One of the servers uses FastCGI and the other uses multi-threading. This works fine.
C. Both use FastCGI, which is often abnormal. The general performance is:
The two servers each start some php-cgi.exe processes, and then one of the servers does not interpret the php page, or spawns a new php-cgi.exe process after a while (scary).

Why? I think it's probably because the authors of the two things used above (iis isapi and apache module) probably didn't think about using them at the same time with each other, haha. After thinking about it, it would be great if Apache 2 and IIS 6 could share a batch of php-cgi.exe interpreters, which would not waste memory and probably would not cause scheduling problems.
Research and testing have proven that this idea is feasible. However, since Shane Caraveo does not provide the function of using external FastCGI services in the ISAPI DLL, in this scenario IIS can only be responsible for starting and managing php-cgi.exe, and then configuring Apache to use these IIS-managed services. php-cgi.exe process.

Configuration method:
1. Configure IIS FastCGI according to the above four methods.
2. Basically configure Apache FastCGI according to the method in the above five.4.5.4 instead add:
in httpd.conf LoadModule fastcgi_module modules/mod_fastcgi-2.4.2-AP20.dll
​​ # To use an external FastCGI server, please refer to the FastCGI documentation.
  FastCgiExternalServer "c:/php/php-cgi.exe" -socket "php-fcgi"
Note: The value after the -socket parameter must be consistent with the value of BindPath in HKEY_LOCAL_MACHINE:SoftwareFASTCGI.php, so that the two FastCGI process managers will use the same named pipe to connect to php-cgi.exe.

Note: In this configuration, the php-cgi.exe process is only managed by the FastCGI process manager in IIS. Apache's busy requests will not cause FastCGI in IIS to schedule more php-cgi.exe processes. Therefore, when configuring FastCGI in IIS, the StartServers value should be large enough to avoid insufficient number of PHP interpreters. The same problem is that if IIS is closed, Apache will not be able to find the Php interpreter. Please pay attention to this.

One question that arises from this is: How should cgi.force_redirect, fastcgi.impersonate, and cgi.rfc2616_headers be set in php.ini at this time? I’ll leave this to everyone to think about… Haha

Another problem you may encounter is that IIS is very idle. After a while, php-cgi.exe started by IIS exits, and apache cannot interpret Php. What should I do? At this time, you can visit the iis website, and php-cgi.exe will start again, dizzy. One suggestion is to use the process pool management of IIS 6, turn off the "idle timeout" in the application pool, and set the running account in the "Application Pool Identity" to be consistent with the Apache service startup account.


Netizens’ suggestions:
Collection...
Netizens’ suggestions:
Favorite
However, when I use FastCGI, it often crashes. The test is the same on several machines. I don’t know what the reason is
Netizens’ suggestions:
Official download address: [url=http://www.fastcgi.com/dist/]Link tag http://www.fastcgi.com/dist/[/url]
The version has been upgraded to 2.4.6. The download address provided by the poster does not work. This article should be relatively old

[url=http://www.111cn.cn/html/30/n-34030.html]Link tag http://www.111cn.cn/html/30/n-34030.html[/url] This article Take a look
There are two ways to make PHP work with Apache 2.0.x under Windows. One is using a CGI executable program, and the other is a DLL for Apache modules.
So from a certain perspective, PHP4 installed in the apache module mode has better security and better execution efficiency and speed than CGI mode.

Question: Can PHP installed in the apache module mode use fastcgi, that is, both are loaded in the apache module mode and run normally after installation, but I don’t know what the difference is between the two?

FastCGI's Apache module has some problems, such as the process will be broken and cannot be killed even if it hangs up, which makes it very unstable. Fortunately, there is a solution called FCGID, which will not bring FCGI to the end^^

http://fastcgi.coremail.cn/index.cn.htm

[ ]
Netizens’ suggestions:
Never used it
Netizens’ suggestions:
mark
Netizens’ suggestions:
Take it away, [img]http://www.111cn.cn/bbs/images/smilies/default/lol.gif[/img]
Netizens’ suggestions:
good!
Netizens’ suggestions:
nice.........That is, APACHE MODEL is better than FCGI. . . . . . . . . . [img]http://www.111cn.cn/bbs/images/smilies/default/33.gif[/img] [img]http://www.111cn.cn/bbs/images/smilies/default/33 .gif[/img][img]http://www.111cn.cn/bbs/images/smilies/default/33.gif[/img]
Netizens’ suggestions:
Study by occupying a building

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632516.htmlTechArticleProblem FastCGI Incomplete Advanced Guide (PHP version, Windows platform) Solution 1. What is FastCGI? FastCGI is a language-independent, scalable architecture CGI open extension. Its main behavior...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn