只有累積,沒有奇蹟

2019年1月10日 星期四

[PowerShell] 設定 IIS - 建立 Application Pool、WebSite

前言
PowerShell是微軟公司開發的任務自動化和組態管理框架,相信有接觸過 CI / CD 的人都是聽過且用過這工具,今天就來簡單介紹如何使用 PowerShell 來建立 Application Pool 跟 WebSite 的過程

指令說明
輸入指令
相關指令可以參考 WebAdministration
  1. # import module
  2. Import-Module WebAdministration
  3. cd IIS:\AppPools\
  4.  
  5. # Create WebSite
  6. New-Item IIS:\Sites\DemoWeb -bindings @{protocol="http";bindingInformation=":80:localhost"} -physicalPath D:\Demo\DemoWeb
  7.  
  8. # Create App pools
  9. New-item IIS:\AppPools\DemoPool | Set-ItemProperty -Name "managedRuntimeVersion" -Value "v4.0"
程式說明 : 
  • Line 2 : 先 Import-Module WebAdministration 載入 IIS 管理模組
  • Line 6 : 使用 New-Item command 建立 WebSite第一個參數是 bindings 的資訊 : localhost  80 port
    第二個參數是應用程式的位置 : D:\Demo\DemoWeb
  • Line 9 : 建立 AppPool
    設定 CLR 為 4.0 以及 Name 為 managedRuntimeVersion
確認結果
在 PowerShell 按下執行,接著輸入以下指令確認是否成功
  1. PS C:\> Get-IISAppPool
  1. PS C:\> Get-Website

還記得之前有提到 [IIS] 如何解決網站第一個請求 Request 特別慢 ? 為了加快 Application 啟動問題,需要在 IIS 設定 preloadEnabled 和 AlwaysRunning為 true,我們希望在建立 AppPool & WebSite 時將這兩個設定加在指令中,讓部署時一致化不用在進到 Server 分別調整

安裝 Application Initialization
  1. PS C:\> $webAppInit = Get-WindowsFeature -Name "Web-AppInit"
  2. Install-WindowsFeature $webAppInit -ErrorAction Stop
新增 WebSite & AppPool 
  1. # Create WebSite
  2. New-Item IIS:\Sites\DemoWeb -Name applicationDefaults.preloadEnabled -Value True
  3.  
  4. # Create App pools
  5. New-item IIS:\AppPools\DemoPool | Set-ItemProperty -Name "startMode" -Value "AlwaysRunning"

    參考
    powershell-snap-in-creating-web-sites-web-applications-virtual-directories-and-application-pools
    enable-IIS-preloadEnabled-and-AlwaysRunning-using-PowerShell

    Related Posts:

    • [C#] Anonymous Type 匿名型別說明 Anonymous Type 是甚麼? 匿名型別是C# 3.0開始有的特性,是一種暫存型的型別,不需要建立額外的類別來存放資料 根據MSDN對於 匿名型別 的說明如下 根據MSDN的說明,整理一下重點及特性 1. 透過 new 建立實體  // Anonymous Typevar employee = new { Id = 1, Name = "Marcus", Age = 22 } … Read More
    • [VS2017] The current .NET SDK does not support targeting .NET Core 2.1發生情境 最近在開發時遇到取完最新的source code Build時發生錯誤,Visual Studio error message 如下 The current .NET SDK does not support targeting .NET Core 2.1.  Either target .NET Core 2.0 or lower, or use a version of the .NET SDK that su… Read More
    • [VS2017] 如何在Visual Studio中設定C#建置版本發生情境 前陣子同事從版控取完最新的source Build在Build時跳出錯誤,錯誤訊息如下 Feature 'nameof operator' is not available in C# 5. Please use language version 6 or greater.錯誤訊息中很明確的說明在 code 所使用的 nameof function 僅支援 C# 6以上的版本,但修改的同事並未發生此問題,因此立即解決發生提出… Read More
    • [CheatSheets] ASP.NET Format Strings 廢話 在之前自己有整理過 [筆記] string.Format 格式整理,最近在蒐集小抄表時發現原來早有神人整理過類似的資訊 .NET Format Strings 整理為下列幾項 DateTime : Standard, Custom DateTime Format Strings Number : Standard, Custom DateTime Format S… Read More
    • [C#] Web API - HttpClient 入門HttpClient 介紹 當在開發 Client 程式時,要取得呈現在畫面上的資料會與 API / Server 進行連線動作,在過去可以使用 WebClient 或者是 HttpWebRequest ,在 .NET Framework 4.5 開始微軟提供 HttpClient 類別給開發者使用,命名空間為 Sysyem.Net.Http,它提供靈活且可擴充的 API 來訪問 HTTP 公開的物件,發送 Request 跟… Read More

    0 意見:

    張貼留言

    Copyright © 2025 m@rcus 學習筆記 | Powered by Blogger

    Design by Anders Noren | Blogger Theme by NewBloggerThemes.com