2025年3月21日 星期五

[研究]用 PowerShell 安裝 IIS 和 CGI (Windows Server 2019)

[研究]用 PowerShell 安裝 IIS 和 CGI (Windows Server 2019)

2025-03-21

1. 開啟 PowerShell(以系統管理員身份執行)

按 Win + X,選擇 Windows PowerShell (Admin) 或 Windows Terminal (Admin)。

2. 安裝 IIS 及 CGI

Install-WindowsFeature -Name Web-Server, Web-CGI -IncludeManagementTools

  • 安裝 IIS(Web Server)
  • 安裝 CGI(Common Gateway Interface)
  • 包含 管理工具(IIS Manager)

3. 確認安裝狀態

Get-WindowsFeature -Name Web-Server, Web-CGI

如果 Installed 欄位顯示 True,表示安裝成功。

4. 啟動 IIS 服務

Start-Service W3SVC

Get-Service -Name W3SVC

IIS 預設會開啟 80 端口,您可以透過 http://localhost/ 測試。

5. 允許防火牆規則(如果需要開放外部訪問)

如果此伺服器要被其他裝置訪問,請開啟 HTTP 和 HTTPS 連線:

New-NetFirewallRule -DisplayName "IIS HTTP" -Direction Inbound -Protocol TCP -LocalPort 80 -Action Allow

New-NetFirewallRule -DisplayName "IIS HTTPS" -Direction Inbound -Protocol TCP -LocalPort 443 -Action Allow

安裝完成後,您可以在 IIS 管理員 (inetmgr) 中檢視與管理 CGI 設定。

********************************************************************************

實際測試





PS C:\> Install-WindowsFeature -Name Web-Server, Web-CGI -IncludeManagementTools

Success Restart Needed Exit Code      Feature Result
------- -------------- ---------      --------------
True    No             Success        {應用程式開發, CGI, 一般 HTTP 功能, 預設文...


PS C:\> Get-WindowsFeature -Name Web-Server, Web-CGI

Display Name                                            Name                       Install State
------------                                            ----                       -------------
[X] 網頁伺服器 (IIS)                                    Web-Server                     Installed
            [X] CGI                                     Web-CGI                        Installed


PS C:\> Start-Service W3SVC
PS C:\> Get-Service -Name W3SVC

Status   Name               DisplayName
------   ----               -----------
Running  W3SVC              World Wide Web Publishing 服務


PS C:\> New-NetFirewallRule -DisplayName "IIS HTTP" -Direction Inbound -Protocol TCP -LocalPort 80 -Action Allow


Name                  : {c890fe1d-7041-4e3d-9855-4e97204d4ebc}
DisplayName           : IIS HTTP
Description           :
DisplayGroup          :
Group                 :
Enabled               : True
Profile               : Any
Platform              : {}
Direction             : Inbound
Action                : Allow
EdgeTraversalPolicy   : Block
LooseSourceMapping    : False
LocalOnlyMapping      : False
Owner                 :
PrimaryStatus         : OK
Status                : 已從存放區成功剖析規則。 (65536)
EnforcementStatus     : NotApplicable
PolicyStoreSource     : PersistentStore
PolicyStoreSourceType : Local



PS C:\> New-NetFirewallRule -DisplayName "IIS HTTPS" -Direction Inbound -Protocol TCP -LocalPort 443 -Action Allow


Name                  : {34876d5a-c3de-4033-9646-b77687d756f0}
DisplayName           : IIS HTTPS
Description           :
DisplayGroup          :
Group                 :
Enabled               : True
Profile               : Any
Platform              : {}
Direction             : Inbound
Action                : Allow
EdgeTraversalPolicy   : Block
LooseSourceMapping    : False
LocalOnlyMapping      : False
Owner                 :
PrimaryStatus         : OK
Status                : 已從存放區成功剖析規則。 (65536)
EnforcementStatus     : NotApplicable
PolicyStoreSource     : PersistentStore
PolicyStoreSourceType : Local



PS C:\>


(完)


沒有留言:

張貼留言