©
Ce document utilise Manuel du site Web PHP chinois Libérer
本章包含有在 Windows Vista SP1,Windows 7,Windows Server 2008 以及 Windows Server 2008 R2下的 IIS 7.0 以及更高版本中手工安装 PHP 的指南。有关在 Windows XP 和 Windows Server 2003 下的 IIS 5.1 和 IIS 6.0 中安装 PHP 的指南见 Microsoft IIS 5.1 和 IIS 6.0 一章。
默认安装的 IIS 中 FastCGI 模块被关闭。要激活其的步骤在不同版本的 Windows 下不同。
要在 Windows Vista SP1 和 Windows 7 中激活 FastCGI 支持:
在 Windows 开始菜单中选择“运行...”(或在搜索框内),输入“optionalfeatures.exe”并按“确定”(或敲回车键);
在“Windows 功能”对话框中展开“Internet 信息服务”,“万维网服务”,“应用程序开发功能”,并选中“CGI”的选择框;
点击确定按钮并等待安装完成。
要在 Windows Server 2008 和 Windows Server 2008 R2 中激活 FastCGI 支持:
在 Windows 开始菜单中选择 "Run:",输入 "CompMgmtLauncher" 并点击 "Ok";
如果在 "Roles" 节点下没有 "Web Server (IIS)" role,则点击 "Add Roles" 添加之;
如果存在 "Web Server (IIS)" role,则点击 "Add Role Services" 并激活 "Application Development" 组之下的 "CGI" 选择框;
点击 "Next" 及 "Install",等待安装完成。
根据手工安装步骤的说明下载和安装 PHP。
Note:
在使用 IIS 时推荐使用非线程安全的 PHP。可以在 » PHP for Windows: Binaries and Sources Releases 下载。
按以下示例在 php.ini 文件中配置 针对 CGI- 和 FastCGI- 的指令:
Example #1 php.ini 中的 CGI 和 FastCGI 设定
fastcgi.impersonate = 1 fastcgi.logging = 0 cgi.fix_pathinfo=1 cgi.force_redirect = 0
通过 IIS 管理界面或者命令行工具配置对应于 PHP 的 IIS 程序映射。
按照以下步骤在 IIS 管理界面中创建 PHP 的程序映射:
在 Windows 开始菜单中选择“运行...”,输入“inetmgr”并点击“确定”;
在 IIS 管理器中左边面板“连接”下面的树状图中选择该服务器的节点;
在中间面板下方的“功能视图”页面打开“处理程序映射”功能;
在右边“操作”面板中点击“添加模块映射...”;
在“添加模块映射”对话框中输入以下内容:
点击“请求限制(R)...”按钮并选中“仅当请求映射至以下内容时才调用处理程序(I):”然后选择“文件或文件夹(O)”;
在所有对话框中点击确定以保存配置。
用以下命令创建一个 IIS FastCGI 处理池以使 php-cgi.exe 可执行文件来处理 PHP 请求。用自己系统上指向 php-cgi.exe 文件的绝对路径来替代 fullPath 参数的值。
Example #2 创建 IIS FastCGI 处理池
%windir%\system32\inetsrv\appcmd set config /section:system.webServer/fastCGI ^ /+[fullPath='c:\PHP\php-cgi.exe']
用以下命令配置 IIS 处理针对 PHP 的请求。用自己系统上指向 php-cgi.exe 文件的绝对路径来替代 scriptProcessor 参数的值。
Example #3 创建对应于 PHP 请求的处理程序映射
%windir%\system32\inetsrv\appcmd set config /section:system.webServer/handlers ^ /+[name='PHP_via_FastCGI', path='*.php',verb='*',modules='FastCgiModule',^ scriptProcessor='c:\PHP\php-cgi.exe',resourceType='Either']
此命令创建了一个对于 *.php 文件后缀的处理程序映射,使得所有以 .php 结尾的 URL 都会被 FastCGI 模块处理。
Note:
至此所需的安装和配置步骤就完成了。以下剩余的指示是可选项,但是强烈推荐以使得在 IIS 上达到最佳的 PHP 功能和性能。
在 IIS 中使用 PHP 建议激活 FastCGI 的角色扮演功能。此功能在 php.ini 中由 fastcgi.impersonate 指令控制。激活角色扮演后,PHP 将以 IIS 所认证的用户帐号身份进行所有的文件系统操作。这将确保即使在(同一个主机)不同的 IIS 网站下使用了同一个 PHP 进程,只要每个网站使用了不同的用户帐号作为 IIS 身份认证,则这些网站的 PHP 脚本将不能访问彼此的文件。
例如在 IIS 7 中,默认配置下的匿名认证将使用内置的用户帐号 IUSR 作为默认身份。这意味着要使得 IIS 能够运行 PHP 脚本,至少要将这些脚本的读取权限授予 IUSR 帐号。如果 PHP 程序需要对某些文件或文件夹进行写入操作,那 IUSER 帐号也需要有相对应的写入权限。
在 IIS 7 中要查看哪个用户帐号被用作匿名认证的身份,使用以下命令。将其中的 "Default Web Site" 替换为自己使用的网站名。在输出的 XML 配置单元中查找 userName 属性。(注意:要以管理员身份运行此命令行)
Example #4 确定用于 IIS 匿名认证的用户帐号
%windir%\system32\inetsrv\appcmd.exe list config "Default Web Site" ^ /section:anonymousAuthentication<system.webServer> <security> <authentication> <anonymousAuthentication enabled="true" userName="IUSR" /> </authentication> </security> </system.webServer>
Note:
如果在 anonymousAuthentication 单元中没有显示 userName 属性,或者其值为一个空字符串,那意味着应用程序池的身份被用于该网站的匿名身份。
要修改文件及文件夹的权限,使用 Windows 资源管理器或者 icacls 命令行。
Example #5 配置文件访问权限
icacls C:\inetpub\wwwroot\upload /grant IUSR:(OI)(CI)(M)
IIS 的默认文档用于没有指定文件名的 HTTP 请求。对于 PHP 应用来说默认文档通常为 index.php。要将 index.php 添加到 IIS 的默认文档列表中,使用此命令:
Example #6 将 index.php 设为 IIS 的默认文档
%windir%\system32\inetsrv\appcmd.exe set config ^ -section:system.webServer/defaultDocument /+"files.[value='index.php']" ^ /commit:apphost
用以下命令配置 IIS FastCGI 对于 PHP 进程的回收设定。FastCGI 的设置 instanceMaxRequests 控制了单一的 php-cgi.exe 进程处理多少个请求之后会被 IIS 关闭。PHP 环境变量 PHP_FCGI_MAX_REQUESTS 控制了一个 php-cgi.exe 进程处理多少个请求之后会被自我回收。要确保 FastCGI 中 InstanceMaxRequests 的值小于或等于 PHP_FCGI_MAX_REQUESTS 的值。
Example #7 配置 FastCGI 和 PHP 的回收
%windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/fastCgi ^ /[fullPath='c:\php\php-cgi.exe'].instanceMaxRequests:10000%windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/fastCgi ^ /+"[fullPath='C:\{php_folder}\php-cgi.exe'].environmentVariables.^ [name='PHP_FCGI_MAX_REQUESTS',value='10000']"
如果会有一些需时较长的 PHP 脚本运行,则增加超时的设定值。有两个控制超时的指令 activityTimeout 和 requestTimeout 。用以下命令修改超时设定。确保在 fullPath 的参数中使用自己系统上的 php-cgi.exe 的绝对路径。
Example #8 配置 FastCGI 超时设定
%windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/fastCgi ^ /[fullPath='C:\php\php-cgi.exe',arguments=''].activityTimeout:"90" /commit:apphost%windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/fastCgi ^ /[fullPath='C:\php\php-cgi.exe',arguments=''].requestTimeout:"90" /commit:apphost
PHP 在几个位置搜索配置文件 php.ini,可以通过环境变量 PHPRC 来改变 php.ini 的默认位置。要使得 PHP 从用户指定的位置加载配置文件,使用以下命令。指向 php.ini 文件的绝对路径应作为环境变量 PHPRC 的值。
Example #9 改变 php.ini 文件的位置
appcmd.exe set config -section:system.webServer/fastCgi ^ /+"[fullPath='C:\php\php.exe',arguments=''].environmentVariables.^ [name='PHPRC',value='C:\Some\Directory\']" /commit:apphost
[#1] jbtibor [2015-06-17 15:35:28]
Microsoft Web Platform Installer for php MySQL and others:
http://www.microsoft.com/web/downloads/platform.aspx
[#2] sedativchunk at gmail dot com [2015-01-31 23:19:12]
This might be an incredibly stupid comment, but I thought I would share anyway. If you want to install PHP on Windows Server in one click without all the hassle above, simply go here to the official IIS website:
http://php.iis.net
Run the install, boom, PHP works on your IIS/Windows server! No command prompt windows, no messing with settings, everything is pre-configured. I followed all the steps on this page and I could not get PHP working for anything. When I was about to give up, I found that link where the official Microsoft installer made it stupidly easy.
Use it, save your time and get back to work faster.
[#3] matteo dot alvazzi at gmail dot com [2014-09-11 08:42:29]
It's a bit silly, but it's worth writing it down, as there will be a day you'll have to configure php on IIS in a Preproduction via VPN with a really pushing customer and only few minutes available... and somehow you WILL forget this.
Php.ini by default has the short code set to off, so if you configure everything and test it out with
<?php phpinfo() ?>
you will a blank screen with the code in the source, which can also indicate that there is something wrong in the configuration and that the script doesn't get sent to the php processor. It's not, it's correct! Just test out with
<?php phpinfo(); ?>
or turn on the short codes!
[#4] daniel at kaliel dot net [2014-04-14 18:30:31]
I ran into the Error 500 with PHP 5.5 and Server 2008 R2 x64. The issue was that I installed the 64-bit version of Visual C++ Redistributable 2012 and not the 32-bit version.
[#5] B-Edge (MS certified Trainer) [2014-04-07 21:36:53]
Excellent notes the last comment about adding the environment variables works as previously stated for both examples. I had to figure it out before reading these comments...
and yes the correct format would be
%windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/FastCGI ^ /+"[fullPath='c:\PHP\php-cgi.exe'].environmentVariables.[name='PHP_FCGI_MAX_REQUESTS',value='10000']"
and for the second one
%windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/FastCGI ^ /+"[fullPath='c:\PHP\php-cgi.exe'].environmentVariables.[name='PHPRC',value='c:\PHP\']"
hope this helped!
[#6] brrg58 at yahoo dot com [2014-03-04 15:58:39]
Following the installation steps second line of Example #3 resulted in an error:
ERROR ( message:Cannot find SITE object with identifier "path='*.php',verb='*',modules='FastCgiModule',scriptProcessor='(path-to-PHP)\php-cgi.exe',resourceType='Either']". )
Apparently there is an extra space before the ,path='*.php' that must be removed.
Thanks
[#7] Aditya [2013-10-09 20:21:54]
To resolve the error "The FastCGI process exited unexpectedly" install the below package:
To fix it I had to install the Visual C++ Redistributable for Visual Studio 2012 Update 3
http://www.microsoft.com/en-us/download/confirmation.aspx?id=30679
[#8] Bradford Plummer [2013-08-21 00:24:12]
If you get an error running the second command in Example 7 try this instead:
%windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/fastCgi /+"[fullPath='C:\php\php-cgi.exe'].environmentVariables.[name='PHP_FCGI_MAX_REQUESTS',value='10000']"
Be sure to change the path to the php-cgi.exe file if it is not at C:\php\.
Good luck.
[#9] m dot benett at gmail dot com [2013-07-11 15:57:21]
In CGI and FastCGI settings in php.ini exaple, add:
extension_dir = 'ext'
[#10] williamhong at 163 dot com [2013-05-11 04:41:11]
CgiModule may be better than FastCgiModule in development because CGI processes are not cached.
[#11] stpetersn at hotmail dot com [2012-08-17 02:21:49]
For setting the time out above it took me forever to figure out the right combination for double and single quotes (and no quotes) given I had spaces in the absolute path to my php-cgi.exe file. Here is what worked in case you have/had the same problem:
%windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/fastCgi ^
/[fullPath="'C:\Program Files (x86)\PHP\php-cgi.exe'"].activityTimeout:90 /commit:apphost
%windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/fastCgi ^
/[fullPath="'C:\Program Files (x86)\PHP\php-cgi.exe'"].requestTimeout:90 /commit:apphost
[#12] NG [2012-06-07 06:30:34]
Example #9 fails unless I change "php.exe" to "php-cgi.exe" as follows:
appcmd.exe set config -section:system.webServer/fastCgi ^
/+"[fullPath='C:\php\php-cgi.exe',arguments=''].environmentVariables.^
[name='PHPRC',value='C:\PHP\']" /commit:apphost
[#13] mfuhrman at enetarch dot net [2012-06-01 15:19:38]
When installing PHP in Windows 2008 Server R2, you might find that Windows IIS 7 returns error code 500 when executing scripts. To resolve this do two things:
1) browse to the folder that contains the script with the error using CMD. Yes, this is the command prompt.
Then execute the script using something like: "C:\Program Files\PHP\PHP5.4\php.exe" [script-filename].php
Note that the quotes ("") are needed for file paths with spaces.
This will return the results of the script and any errors you might have.
2) To have IIS 7 return the errors to your webbrowser, turn off error logging and turn on display errors. And to make sure that all the errors are display, set error reporting to ALL.
log_errors = Off
display_errors = On
error_reporting = E_ALL