2016/08/21

[IIS] Windows 10 + IIS 10 + PHP + Laravel 環境設定

大多數 在 Windows OS 下執行 Laravel 專案都會用 WAMP 環境 - 像是 wagon, UwAmp, 或是 Xampp 以 Apache 為 server 為基礎的伺服器環境.

今天來點不一樣的~ 用 Windows IIS 來架設 PHP 專案環境.

重要的是在 Windows 10 的 IIS 上安裝 PHP Manager 及 URL Rewrite 有些小細節需要微調




環境:

  • Windows 10
  • IIS 10
  • PHP Manager 1.4.0
  • URL Rewrite 2.0

IIS 設定


IIS 的安裝方法,請至 控制台 -> 程式集 -> 程式和功能 -> 開啟或關閉  Windows 功能



勾選 Internet Information Services (IIS)

並在 World Wide Web 服務 中 應用程式開發功能 部分
為了要可執行 PHP, 需要勾選 CGI



PHP Manager 及 URL Rewrite


原本這兩項工具, 可以透過 Web Platform Installer 來安裝

但是因為版本有點舊, 或是設定值太高, 造成安裝失敗

PHP Manager 目前只提供 1.2 版, 在 Windows 8 以前還可以正常, 但是卻無法在 Windows 10 安裝

PHP Manager for IIS on Windows 10

還好有熱心的網友提供新版本



URL Rewrite


URL Rewrite 2.0 下載

因為 Laravel 會用到 URL Rewrite, 這個元件是必要的, 不然網址可就執行不正常囉

執行開啟 regedit



需要修改一下 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\InetStpMajorVersion 值改為十進位 9, 裝完後再改回 10






WPI 安裝 URL Rewrite 2.0

下載 Microsoft Web Platform Installer 並安裝

搜尋 rewrite 會找到 URL Rewrite 2.0








IIS - PHP 版本設定





預設 WPI 會自動下載 PHP 5.3 版本, 可自行下載其他版本 或是 透過 WPI 來管理其他版本



在 PHP Manager 即可針對各網站去設定 PHP 版本



IIS web.config for Laravel


Laravel 的 URL Rewrite 設定

在 Apache 的設定是在 .htaccess, 而在 IIS 則是 web.config
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^(.*)/$" ignoreCase="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="/{R:1}" />
</rule>
<rule name="Imported Rule 2" stopProcessing="true">
<match url="^" ignoreCase="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
view raw web.config hosted with ❤ by GitHub

設定完成!