Use FastCGI mode to run PHP under IIS

WBOY
Release: 2016-08-08 09:33:31
Original
1324 people have browsed it

Use FastCGI mode to run PHP under IIS

Due to the improvement of PHP5.3, the original IIS parsing PHP scripts through isapi is no longer supported. PHP uses Microsoft's fastcgi mode starting from versions 5.3.0 and later. This is a more advanced method and runs faster. more stable. 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.

Step 1: Download the installation files

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.​

Step 2: Install the files

1. Create a folder on the disk where you want to install it. 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 the 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.

Step 3: Configure IIS

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. We set the port here to 8080 and the website home directory path to D:WebWWW.

Step 4: Configure FastCGI

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 that contains 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
Copy after login

The meanings of the scripts are:

  1. Register PHP to FastCGI.
  2. InstanceMaxRequests is used to set application pool recycling. The number of FastCGI process requests reaches the InstanceMaxRequests value of 5000, and the application pool will be automatically recycled. The default value is 1000.
  3. When the environment variable PHP_FCGI_MAX_REQUESTS reaches 10000, the application pool will be automatically recycled.
  4. RequestTimeout sets the request timeout, which is the maximum time allowed for a request. If the FastCGI process request exceeds this setting, it will be prohibited. The value is 90 seconds.
  5. ActivityTimeout specifies the maximum active time requested by the FastCGI process. Within the set time, if there is no communication between the FastCGI process and IIS, the process will be terminated. The default value is 70 seconds.

2. Configure fcgiext.ini directly

Open 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
Copy after login

Tip: "php" represents the extension and "PHP" is the configuration section name, defined with "[PHP]".

Copy the above code to the end of fcgiext.ini and save it.

Step 5: Configure PHP

打开PHP安装目录,即D:\Web\PHP,我们可以看到目录下有两个这样的文件 php.ini-development 和 php.ini-production,第一个是开发使用的配置文件,第二个是生产环境的配置,因为我们是在本机做开发,所以选择前者。将php.ini-development复制一份到同目录下,并改名为 php.ini。这个php.ini 就是当前使用的配置文件,以下几步都是在修改这个php.ini文件的内容。 另外要注意的是修改php.ini文件后需重新启动IIS服务,新设置才会生效。参见文档:如何重启IIS服务的方法

1. 设置关于FastCGI部分

找到下面几行,那前面的引号“;” 去掉,参数设置为如下

	fastcgi.impersonate = 1
	cgi.fix_pathinfo= 1
	cgi.force_redirect = 0
Copy after login

它们的说明请参考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"
Copy after login

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
Copy after login

把以上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"
Copy after login

6. 配置PHP的文件上传功能

同session一样,在使用PHP文件上传功能时,我们必须要指定一个临时文件夹以完成文件上传功能,否则文件上传功能会失败。
我们建立一个可读写的目录文件夹 D:\Web\fileuploadtmp,然后在 php.ini 配置文件中找到 ;upload_tmp_dir = (大概875行),修改为:

        upload_tmp_dir = "D:/Web/fileuploadtmp"
Copy after login

PHP默认上传文件大小不超过2M,要想上传大于2M的文件,请参考文档  配置php.ini实现文件上传功能。

7. 设置时区

在 php.ini 配置文件查找 ;date.timezone = ,去掉前面的分号“;” 改为如下:

	date.timezone = PRC
Copy after login

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>
            <?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>
Copy after login

然后在浏览器地址栏输入“http://localhost:8080/index.php”。结果如下图所示:

以上就介绍了IIS下使用FastCGI模式运行PHP,包括了FastCGI模式方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!