搞了一天,总算把IIS+php环境搭好了,规整一下:
1,Enable FastCGI Support in IIS 7
Go to Control Panel -> Programs and Features ->Turn Windows features on or off. In theWindows Features dialog box, select theCGI check box. This enables both the CGI and FastCGI services.
It is recommended that you use a non-thread safe build of PHP with IIS 7 FastCGI. A non-thread safe build of PHP provides significant performance gains over the standard build by not doing any thread-safety checks, which are not necessary, since FastCGI ensures a single threaded execution environment.
To install PHP:
- Download the latest non-thread safe zip package with binaries of PHP: http://www.php.net/downloads.php.
- Unpack the files to the directory of your choice (e.g. C:\php). Rename the php.ini-recommended file to php.ini.
- Open the php.ini file. Uncomment and modify the settings as follows:
- Set fastcgi.impersonate = 1. FastCGI under IIS supports the ability to impersonate security tokens of the calling client. This allows IIS to define the security context that the request runs under.
- Set cgi.fix_pathinfo=1. cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI. Previously, PHP behavior was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not define PATH_INFO. For more information about PATH_INFO, see the cgi specifications. Setting this value to 1 will cause PHP CGI to fix its paths to conform to the specifications.
- Set cgi.force_redirect = 0.
- Set open_basedir to point to the folder or network path where the content of the Web site(s) is located.
- Set extension_dir to point to the location where the PHP extensions are located. Typically, for PHP 5.2.X the value would be set asextension_dir = "C:\php\ext"
- Enable the required PHP extension by un-commenting ( 把该行前面的分号去掉,取消注释,加分号就是注释了该行)the corresponding lines, for example:
extension=php_mssql.dll
extension=php_mysql.dll
- Open a command prompt, and run the following command to verify that PHP installed successfully:
C:\PHP>php –info
If PHP installed correctly and all its dependencies are available on the machine, this command will output the current PHP configuration information.
3,Configure IIS 7 to Handle PHP Requests
For IIS 7 to host PHP applications, you must add a handler mapping that tells IIS to pass all PHP-specific requests to the PHP application framework by using the FastCGI protocol.
Configure IIS 7 to handle PHP requests by using IIS Manager
1. Open IIS Manager. At the server level, double-click Handler Mappings.
2. In the Actions pane, click Add Module Mapping.... In theAdd Module Mapping dialog box, specify the configuration settings as follows:
- Request path: *.php
- Module: FastCgiModule
- Executable: "C:\[Path to your PHP installation]\php-cgi.exe"
- Name: PHP via FastCGI
3. Modify the Resuest Restrictions configuration:
4. Click OK. and 4. In the Add Module Mapping confirmation dialog box that asks if you want to create a FastCGI application for this executable, clickYes.\
5. Test that the handler mapping works correctly by creating a phpinfo.php file in the C:\inetpub\wwwroot folder that contains the following code:
<?php phpinfo(); ?>
6. Open a browser and navigate to http://localhost/phpinfo.php. If everything was setup correctly, you will see the standard PHP information page.
在这一步中,为了更加方便地检测Php是否安装成功,可以在cmd中,cd到c:\php目录下执行:php-cgi.exe D:\phpTest\test.php来验证。
NOTE: If you do not see FastCgiModule in theModules: list, the module is either not registered or not enabled. To check if the FastCGI module is registered, open the IIS configuration file that is located at %windir%\windows\system32\config\applicationHost.config and check that the following line is present in the <globalModules> section:
<add name="FastCgiModule" image="%windir%\System32\inetsrv\iisfcgi.dll" />
In the same file, also check that the FastCGI module is added to the <modules> section:
<add name="FastCgiModule" />
Configure IIS 7 to handle PHP requests by using the command line
Alternatively, you can complete the steps above by using the command line tool AppCmd.
1. Create the FastCGI application process pool by running the following command:
C:\>%windir%\system32\inetsrv\appcmd set config /section:system.webServer/fastCGI /+[fullPath='c:\{php_folder}\php-cgi.exe']
2. Create the handler mapping by running the following command:
C:\>%windir%\system32\inetsrv\appcmd set config /section:system.webServer/handlers /+[name='PHP_via_FastCGI',path='*.php',verb='*',modules='FastCgiModule',scriptProcessor='c:\{php_folder}\php-cgi.exe',resourceType='Unspecified']
Note: If you are using PHP version 4.X, you can use php.exe instead of php-cgi.exe.
BTW, 在这个过程中,可能会遇到500错误:
Error info:
HTTP Error 500.0 - Internal Server Error
C:\PHP\php-cgi.exe - The FastCGI process exited unexpectedlyDetailed Error Information
Module: FastCgiModule
Notification: ExecuteRequestHandler
Handler: PHP via FastCGI
Error Code: 0x000000ff
Requested URL: localhost/test.php
Physical Path: C:\inetpub\wwwroot\test.php
Logon Method: Negotiate
Logon User: Domain\user引起这个问题的关键还是php.ini文件的设置有问题,可以通过排除法来解决:直接到 c:\php目录下,双击PHP-CGI.EXE, 在弹出的命令行窗口下看具体是什么错误,然后相应地修改php.ini文件,清除掉(用;注释掉)无效的或者提示找不到的extensions,就可以使用了。
Try to execute PHP-CGI.EXE (BY DOUBLECLICKING ON IT). See if you get any error messages/ pop-ups. I got a ton of them and it all boiled down on having a bunch of extensions turned on, but not being available in my ext folder. Clear the PHP.INI of those invalid extensions and the problem should go away.
-
下面,附一个正常使用的php.ini文件:
[PHP] ;;;;;;;;;;; ; WARNING ; ;;;;;;;;;;; ; This is the default settings file for new PHP installations. ; By default, PHP installs itself with a configuration suitable for ; development purposes, and *NOT* for production purposes. ; For several security-oriented considerations that should be taken ; before going online with your site, please consult php.ini-recommended ; and http://php.net/manual/en/security.php. ;;;;;;;;;;;;;;;;;;; ; About php.ini ; ;;;;;;;;;;;;;;;;;;; ; This file controls many aspects of PHP's behavior. In order for PHP to ; read it, it must be named 'php.ini'. PHP looks for it in the current ; working directory, in the path designated by the environment variable ; PHPRC, and in the path that was defined in compile time (in that order). ; Under Windows, the compile-time path is the Windows directory. The ; path in which the php.ini file is looked for can be overridden using ; the -c argument in command line mode. ; ; The syntax of the file is extremely simple. Whitespace and Lines ; beginning with a semicolon are silently ignored (as you probably guessed). ; Section headers (e.g. [Foo]) are also silently ignored, even though ; they might mean something in the future. ; ; Directives are specified using the following syntax: ; directive = value ; Directive names are *case sensitive* - foo=bar is different from FOO=bar. ; ; The value can be a string, a number, a PHP constant (e.g. E_ALL or M_PI), one ; of the INI constants (On, Off, True, False, Yes, No and None) or an expression ; (e.g. E_ALL & ~E_NOTICE), or a quoted string ("foo"). ; ; Expressions in the INI file are limited to bitwise operators and parentheses: ; | bitwise OR ; & bitwise AND ; ~ bitwise NOT ; ! boolean NOT ; ; Boolean flags can be turned on using the values 1, On, True or Yes. ; They can be turned off using the values 0, Off, False or No. ; ; An empty string can be denoted by simply not writing anything after the equal ; sign, or by using the None keyword: ; ; foo = ; sets foo to an empty string ; foo = none ; sets foo to an empty string ; foo = "none" ; sets foo to the string 'none' ; ; If you use constants in your value, and these constants belong to a ; dynamically loaded extension (either a PHP extension or a Zend extension), ; you may only use these constants *after* the line that loads the extension. ; ; ;;;;;;;;;;;;;;;;;;; ; About this file ; ;;;;;;;;;;;;;;;;;;; ; All the values in the php.ini-dist file correspond to the builtin ; defaults (that is, if no php.ini is used, or if you delete these lines, ; the builtin defaults will be identical). ;;;;;;;;;;;;