Due to the improvement of PHP5.3, the original IIS parsing PHP scripts through isapi is no longer supported, and PHP has changed from 5.3 Versions after .0 begin to use Microsoft's fastcgi mode, which is a more advanced method that runs faster and more stably. This article introduces running PHP in FastCGI mode on IIS. Let’s take Windows 2003 + IIS 6.0 (comes with windows 2003) as an example.
1. FastCGI version fcgisetup_1.5_x86_rtw.msi
2. MySQL version mysql-5.5.19-win32.msi
3. PHP version php-5.3.8-nts-Win32-VC9-x86.zip
Tips:
1. We use the latest FastCGI 1.5 version here. Official address
2. To install MySQL under Windows, choose the .msi installation package format. We chose mysql-5.5.19-win32.msi. Just double-click and follow the wizard to install. It is simple and convenient.
3. For PHP, we choose to use the php-5.3.8-nts-Win32-VC9-x86.zip version. "vc9" is customized for IIS. "nts" is the non-thread-safe version for FastCGI installation of PHP.
1. Create a folder on the disk to be installed. We create a Web folder in the root directory of the D drive. D:Web.
2. Create a WWW folder under the Web folder to store your PHP website program. D:WebWWW.
3. Unzip the downloaded php-5.3.8-nts-Win32-VC9-x86.zip to the D:WebPHP folder.
4. Install the MySql database and install MySQL to the D:WebMySQL folder. For the specific MySQL installation process, please refer to the document MySQL Installation Diagram.
5. Install FastCGI extension for IIS. First make sure that IIS has been installed on the system. Then double-click fcgisetup_1.5_x86_rtw.msi to install. If the C drive is your Windows system installation disk, it will copy the fcgi-specific files to the C:Windowssystem32inetsrv folder, register and enable the fcgi web service to take effect. At this time, in the IIS "Web Service Extension" includes FastCGI Handler.
1. Click "Control Panel" -> "Administrative Tools" -> "Internet Information Services" (or directly click "Start" -> "Run" -> enter inetmgr.exe) to open IIS.
2. Click "Website"->"right-click-Properties", click "Home Directory"->"Configuration"->"Add" in the properties dialog box.
3. In the "Add and Edit Application Extension Mapping" dialog box, click Browse to find fcgiext.dll in C:Windowssystem32inetsrv. Write ".php" for the extension, click "Limit to" in the "Action" item, write "GET, POST, HEAD", and check the following two options "Script Engine" and "Confirm whether the file exists". Confirm configuration.
4. Return to the website properties box, click "Document" and add "index.php" as the default page. Finally click "Confirm".
5. Click "Website"->"right-click-New"->"Website" to create a new site. The port here is set to 8080, and the website's main directory path is D:WebWWW.
After installing FastCGI, there are the following three important files in the C:Windowssystem32inetsrv folder:
fcgiext.dll - This is the fastcgi handle dynamic link library. It can handle requests for programs that communicate with fastcgi.
fcgiext.ini - This is the configuration file containing file mappings extended to the fastcgi process. It also contains the configuration of the fastcgi process pool.
fcgiconfig.js - This configuration extension for fastgci. This script updates the fcgiext.ini file, modifies the iis metabase, and reuses the web service application pool if necessary.
There are two ways to configure FastCGI here, one is to configure through the command line, and the other is to directly modify the fcgiext.ini file.
1. Configuration through command line
Open the command line, convert the address to C:Windowssystem32inetsrv, and enter the following command
cscript fcgiconfig.js -add -section:"PHP" -extension:php -path:"D:\Web\php\php-cgi.exe" cscript fcgiconfig.js -set -section:"PHP" -InstanceMaxRequests:5000 cscript fcgiconfig.js -set -section:"PHP" -EnvironmentVars:PHP_FCGI_MAX_REQUESTS:10000 cscript fcgiconfig.js -set -section:"PHP" -RequestTimeout:360 cscript fcgiconfig.js -set -section:"PHP" -ActivityTimeout:120
The meaning of the script is:
2. Configure fcgiext.ini directlyOpen the C:windowssystem32inetsrvfcgiext.ini file and add
[Types] php=PHP [PHP] ExePath=D:\Web\php\php-cgi.exe InstanceMaxRequests=5000 EnvironmentVars=PHP_FCGI_MAX_REQUESTS:10000 RequestTimeout=360 ActivityTimeout=120
Tip: "php" represents the extension, "PHP" is the configuration section name, defined with "[PHP]".
Copy the above code to the end of fcgiext.ini and save it.
Open the PHP installation directory, namely D:WebPHP. We can see that there are two such files php.ini-development and php.ini-production in the directory. The first is the configuration file used for development, and the second It is the configuration of the production environment. Because we are doing development on this machine, we choose the former. Copy php.ini-development to the same directory and rename it to php.ini. This php.ini is the currently used configuration file. The following steps are all about modifying the contents of this php.ini file. Also note that after modifying the php.ini file, you need to restart the IIS service for the new settings to take effect. See the document: How to restart the IIS service
1. Set up the FastCGI part
找到下面几行,那前面的引号“;” 去掉,参数设置为如下
fastcgi.impersonate = 1 cgi.fix_pathinfo= 1 cgi.force_redirect = 0
它们的说明请参考cgi.force-redirect;cgi.fix-pathinfo;fastcgi.impersonate。
2. 指定PHP扩展库的存放目录
在Windows下PHP的扩展库通常以 DLL 形式存放在PHP的ext目录中。
使用文本工具打开php.ini文件,查找extension_dir = "ext",也就是在 On windows 下面那一行。把它前面的分号“;”去除掉,改为如下:
extension_dir = "D:/Web/PHP/ext"
extension_dir 表示PHP扩展库的具体目录,以便调用相应的DLL文件。
3. 开启相应的扩展库
默认情况下许多PHP的扩展库都是关闭的,比如默认PHP不支持连接Mysql数据库,需开启相应的扩展库。
这里我们开启一些常用的扩展库。
查找 ; Windows Extensions(大概在941行),在它的下面是extension列表。
找到如下扩展:
;extension=php_curl.dll ;extension=php_gd2.dll ;extension=php_mbstring.dll ;extension=php_exif.dll ;extension=php_mysql.dll ;extension=php_mysqli.dll ;extension=php_pdo_mysql.dll ;extension=php_pdo_odbc.dll ;extension=php_sockets.dll ;extension=php_xmlrpc.dll ;extension=php_xsl.dll
把以上extension 前面的分号 “;” 去掉。如果你希望加载其它扩展模块,方法相同,只要去掉前面的分号即可。
提示1:Exif 扩展是用来显示图片的 exif 信息的,因为该扩展需要 mbstring.dll 支持,所以必须将 extension=php_exif.dll 这一行写到extension=php_mbstring.dll 的后面。
提示2:如果PHP自带的扩展库不能满足你的需要,你可以去 pecl.php.net 查找并下载你需要的库。windows用户可以去 pecl4win.php.net 或者 http://downloads.php.net/pierre/ 下载已经编译好的DLL文件,也可以去http://museum.php.net/php5/ 载PECL集合压缩包。
提示3:很多人在找php_zip.dll,其实从php5.3开始已经集成了zip扩展,你可以用 找到zip扩展的说明。
4. 让PHP支持短标签
在 php.ini 配置文件查找 short_open_tag = Off (大概在266行)把它修改成:short_open_tag = On 让其支持短标签。
php的代码一般包含在的标签内,设置 short_open_tag = On 之后,可以写成更简洁的形式: .... ?>。
5. 配置PHP的Session功能
在使用session功能时,我们必须配置session文件在服务器上的保存目录。我们需要为session建立一个可读写的目录,这个目录最好独立于网站目录之外。
这里把目录上建在了D:\Web\sessiontmp,然后在 php.ini 配置文件中找 ;session.save_path = "/tmp" (大概在1467行),去掉前面的分号“;” ,改为如下:
session.save_path = "D:/Web/sessiontmp"
6. 配置PHP的文件上传功能
同session一样,在使用PHP文件上传功能时,我们必须要指定一个临时文件夹以完成文件上传功能,否则文件上传功能会失败。
我们建立一个可读写的目录文件夹 D:\Web\fileuploadtmp,然后在 php.ini 配置文件中找到 ;upload_tmp_dir = (大概875行),修改为:
upload_tmp_dir = "D:/Web/fileuploadtmp"
PHP默认上传文件大小不超过2M,要想上传大于2M的文件,请参考文档 配置php.ini实现文件上传功能。
7. 设置时区
在 php.ini 配置文件查找 ;date.timezone = ,去掉前面的分号“;” 改为如下:
date.timezone = PRC
date.timezone是PHP5.1开始新增加的配置参数,用来设置时区。默认date.timezone是被注释掉的,也就是默认时区是UTC格林威治标准时间。
我们这里改为 date.timezone = PRC(PRC,People’s Republic of China,中华人民共和国),也就是日期使用中国的时区。这样可以解决时间相差八小时的问题。
将如下代码保存为index.php文件,放到D:\Web\WWW目录下面。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>测试服务器</title> </head> <body> <div style="text-align:center; color:red"> <?php $link=mysql_connect("localhost","<em>你的mysql管理账号</em>","<em>你的mysql管理密码</em>"); if(!$link) echo "MySQL数据库 连接失败!"; else echo "MySQL数据库 连接成功!"; mysql_close(); ?> </div> <br/> <?php phpinfo(); ?> </body> </html>
然后在浏览器地址栏输入“http://localhost:8080/index.php”。结果如下图所示: