如何在 IIS 10 安裝 PHP 7 以及 PHP 8 (How to install PHP 8 in IIS 10)


  1. 說明
    1. Download PHP
    2. VC15 and VS16 builds
    3. Windows Features - CGI
    4. 建立站台
    5. 設定處理常式對應 (Handler Mapping)
    6. 設定 php.ini
    7. 站台測試
  2. 參考資料
  3. 相關連結

不需要透過 MAMP 或者是 XAMPP,也不需要額外在 Windows 上安裝 Apache,只要搭配 IIS 就可以使用 PHP 提供 Web 服務。

logo

說明

Download PHP

首先需要下載 PHP for Windows

下載的版本選擇 x64 Non Thread Safe的版本,目前網站提供 PHP 8.1、8.0 以及 7.4 可以供選擇,如果需要已經 EOS 的版本,例如 PHP 5.6,可以到 Archive Donwload Page 下載。

PHP Version Release Date EOS Date
1 1995-06-08 EOS
2 1996-04-16 EOS
3 1998-06-06 2000-10-20
4.4 2004-07-13 2005-09-05
5.6 2014-08-28 2018-12-31
7.4 2019-11-28 2022-11-28
8.0 2020-11-26 2023-11-26
8.1 2021-11-25 2024-11-25

完成下載後的 PHP 不需要安裝,僅需要解壓縮至 Web Server。

VC15 and VS16 builds

需要安裝相依資源才能夠正常在 IIS 上啟用 PHP。

x64x86

Windows Features - CGI

需要在 Windows Server 上加入 CGI 功能,才能夠在 IIS 上啟用 PHP。

建立站台

站台所使用的應用程式集區,必須調整為 沒有受控碼 (No Managed)

設定處理常式對應 (Handler Mapping)

進入處理常式對應 (Handler Mapping)後,選擇新增模組對應 (Add Module Mapping)

設定 php.ini

前往 PHP 的資料夾,使用 php.ini-deveopment 重新命名為 php.ini,調整下列內容:

php.ini

調整下列內容:

;extension_dir = "ext"
;date.timezone =
;extension=openssl

調整結果:

extension_dir = "C:\inetpub\php-7.4.33-nts-Win32-vc15-x64\ext"
date.timezone = "Asia/Taipei"
extension=openssl

而 php.ini-deveopment 與 php.ini-production 的差別如下:

php.ini-production

zend.exception_ignore_args = On
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
display_errors = Off
display_startup_errors = Off
mysqlnd.collect_memory_statistics = Off
zend.assertions = -1
;opcache.huge_code_pages=1

站台測試

完成後,在站台下建立 php 檔案進行測試。

Default.php

<?php
  phpinfo();
?>

PHP 7.4 在 IIS 的安裝相當順利,但按照相同的設定設定 PHP 8.0 或 PHP 8.1 則會失敗:

<handler> scriptProcess could not be found in <fastCGI> application configuration

藉由安裝 PHP Manager 2 for IIS 後問題有得到處理。

但會讓所有的站台都繼承 php 的 handler,否則衍生新的問題。而在觀察 applicationHost.config 後,找到問題的所在是僅需要註冊 fastCgi 即可,當中的 Path 對應到實際 Web Server 的 PHP 路徑。如此一來就可以不用透過安裝 PHP Manager 2 for IIS 也能夠順利安裝 PHP 8.0, 8.1 囉 😁

PHP Manager 2 for IIS

C:\Windows\System32\inetsrv\Config\applicationHost.config

<fastCgi>
  <application fullPath="C:\inetpub\php-8.1.12-nts-Win32-vs16-x64\php-cgi.exe" 
  monitorChangesTo="C:\inetpub\php-8.1.12-nts-Win32-vs16-x64\php.ini" 
  activityTimeout="300" requestTimeout="300" instanceMaxRequests="10000">
    <environmentVariables>
      <environmentVariable name="PHPRC" value="C:\inetpub\php-8.1.12-nts-Win32-vs16-x64\" />
      <environmentVariable name="PHP_FCGI_MAX_REQUESTS" value="10000" />
    </environmentVariables>
  </application>
  <application fullPath="C:\inetpub\php-7.4.33-nts-Win32-vc15-x64\php-cgi.exe" />
</fastCgi>

而在站台或者子目錄的應用程式,只需要在 web.config 加入 handlers 的註冊即可以讓站台或者子目錄應用程式支援 php。

web.config

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <handlers>
            <!-- <add name="php 7.4" path="*.php" 
            verb="GET,HEAD,POST"
            modules="FastCgiModule"
            scriptProcessor="C:\inetpub\php-7.4.33-nts-Win32-vc15-x64\php-cgi.exe"
            resourceType="Either" /> -->

            <add name="php 8.4" path="*.php"
            verb="GET,HEAD,POST"
            modules="FastCgiModule"
            scriptProcessor="C:\inetpub\php-8.1.12-nts-Win32-vs16-x64\php-cgi.exe"
            resourceType="Either" requireAccess="Script" />
        </handlers>
        <defaultDocument>
            <files>
                <add value="Default.php" />
            </files>
        </defaultDocument>
    </system.webServer>
</configuration>

參考資料

PHP開發日誌 – IIS7安裝PHP8.0及多個版本如何同時存在一台服務器

【Windows】安裝 Apache Web Server(含多版本PHP並存)

Windows Server IIS 如何安裝 PHP 網頁伺服器 | 理財工程師 Mars

相關連結

IIS 筆記整理

IIS 網頁伺服器的安全設定 (IIS Security Configuration)