The Will Will Web

記載著 Will 在網路世界的學習心得與技術分享

分享我的 Windows Server 2022 裝機必備軟體與安裝筆記

由於 Windows Server 2022 已於 2021/9/1 正式推出,這篇文章我打算介紹我安裝一台測試機的所有過程,方便日後裝機時參考。

基本作業環境設定

以下我直接列出完整 PowerShell 命令,請以系統管理員身份執行:

# 更改主機名稱並重開機 ( 假設要更名為 WILL2022SRV )
# Rename-Computer -NewName WILL2022SRV -LocalCredential Administrator -PassThru
# Restart-Computer

# 設定時區為台北標準時間 (Taipei Standard Time)
Set-TimeZone -Name "Taipei Standard Time"

# 關閉開機自動啟動伺服器管理員 (Server Manager)
Get-ScheduledTask -TaskName ServerManager | Disable-ScheduledTask -Verbose

# 調整 ExecutionPolicy 等級到 RemoteSigned
Set-ExecutionPolicy RemoteSigned -Force

# 建立 $PROFILE 所需的資料夾
[System.IO.Directory]::CreateDirectory([System.IO.Path]::GetDirectoryName($PROFILE))

# 設定 PowerShell 的 ProgressPreference, TLS 1.2 與 PSReadLine 快速鍵
# https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_preference_variables#progresspreference
@'
# 修正 PowerShell 關閉進度列提示
$ProgressPreference = 'SilentlyContinue'

# 使用 TLS 1.2 進行網路安全連線
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

# 設定按下 Ctrl+d 可以退出 PowerShell 執行環境
Set-PSReadlineKeyHandler -Chord ctrl+d -Function ViExit

# 設定按下 Ctrl+w 可以刪除一個單字
Set-PSReadlineKeyHandler -Chord ctrl+w -Function BackwardDeleteWord

# 設定按下 Ctrl+e 可以移動游標到最後面(End)
Set-PSReadlineKeyHandler -Chord ctrl+e -Function EndOfLine

# 設定按下 Ctrl+a 可以移動游標到最前面(Begin)
Set-PSReadlineKeyHandler -Chord ctrl+a -Function BeginningOfLine

# 移除內建的 curl 與 wget 命令別名
If (Test-Path Alias:curl) {Remove-Item Alias:curl}
If (Test-Path Alias:wget) {Remove-Item Alias:wget}
'@ | Out-File $PROFILE

# 安裝 Chocolatey 套件管理器 ( https://chocolatey.org/install )
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

變更系統語言設定並設定 zh-TW 為顯示語言

  1. 調整語言設定

    請想想 Windows 難用的中文輸入法,將預設語言設定在「英文」才能讓你順利的按出 Ctrl-., Ctrl-,Ctrl-Space 等常用快速鍵!

    $UserLanguageList = New-WinUserLanguageList -Language "en-US"
    $UserLanguageList.Add("zh-TW")
    Set-WinUserLanguageList -LanguageList $UserLanguageList -Force
    Set-WinUILanguageOverride -Language zh-TW
    
  2. 手動下載繁體中文的輸入法

    由於我 Windows Server 2022 是安裝英文版,如果要加裝繁體中文,除了上一步的調整語言設定外,還要額外到控制台手動點擊繁體中文Typings (輸入法) 的「下載」按鈕,安裝之後才可以使用注音輸入法!

安裝常用應用程式

Windows Server 2022 已經預設內建 Internet Explorer 11 與 Microsoft Edge (v92) 瀏覽器,所以可以不用額外安裝 Google Chrome 了!

  1. 安裝常用工具

    底下這段 choco install 命令將會自動安裝 Cascadia Code 字型、Windows Terminal7-ZipNotepad2Notepad++Git for WindowsTortoiseGitWinMergePowerShell CoreClinkGNU utilities for Win32Windows SysinternalsGNU WgetcURLjqVisual Studio CodeNode.jsEverything,這幾套都是我覺得超級好用的必備裝機軟體!

    choco install cascadiafonts microsoft-windows-terminal 7zip notepad2 notepadplusplus git tortoisegit winmerge powershell-core clink unxutils sysinternals wget curl jq vscode nodejs-lts everything -y
    
  2. 調整 Windows Terminal 預設設定

    底下我只列出非預設的設定值:

    {
        "copyOnSelect": true,
        "multiLinePasteWarning": false,
        "profiles":
        {
            "defaults":
            {
                "fontFace": "Cascadia Code PL"
            }
        }
    }
    
  3. 調整 PowerShell 命令提示格式

    # 設定 oh-my-posh 與 posh-git 命令列執行環境
    Install-Module posh-git -Scope CurrentUser -Confirm:$false -Force
    Install-Module oh-my-posh -Scope CurrentUser -Confirm:$false -Force
    
    @'
    Import-Module posh-git
    Import-Module oh-my-posh
    Set-PoshPrompt -Theme spaceship
    '@ | Out-File -Append $PROFILE
    
  4. 調整 Git 版控設定

    # 直接設定 Git 預設 user.name 與 user.email
    git config --global user.email "you@example.com"
    git config --global user.name "Your Name"
    
    # 設定預設 Git Alias
    git config --global alias.ci   commit
    git config --global alias.cm   "commit --amend -C HEAD"
    git config --global alias.co   checkout
    git config --global alias.st   status
    git config --global alias.sts  "status -s"
    git config --global alias.br   branch
    git config --global alias.re   remote
    git config --global alias.di   diff
    git config --global alias.type "cat-file -t"
    git config --global alias.dump "cat-file -p"
    git config --global alias.lo   "log --oneline"
    git config --global alias.ls   "log --show-signature"
    git config --global alias.ll   "log --pretty=format:'%h %ad | %s%d [%Cgreen%an%Creset]' --graph --date=short"
    git config --global alias.lg   "log --graph --pretty=format:'%Cred%h%Creset %ad |%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset [%Cgreen%an%Creset]' --abbrev-commit --date=short"
    git config --global alias.alias "config --get-regexp ^alias\."
    git config --global alias.ignore "!gi() { curl -sL https://www.gitignore.io/api/\$@ ;}; gi"
    git config --global alias.iac  "!giac() { git init && git add . && git commit -m 'Initial commit' ;}; giac"
    
    # 設定 git 預設編輯器為 notepad
    git config --global core.editor "notepad"
    
    # 設定 git 預設編輯器為 Visual Studio Code
    # git config --global core.editor "code --wait"
    
    # 設定 git tlog 開啟 Tortoise Show log 視窗
    git config --global alias.tlog "!start 'C:\PROGRA~1\TortoiseGit\bin\TortoiseGitProc.exe' /command:log /path:."
    
    # 設定 git status 若有中文不會顯示亂碼
    git config --global core.quotepath false
    
    # 設定 git log 若有中文不會顯示亂碼
    SETX LC_ALL C.UTF-8 /M
    

安裝 WSL 2 (Windows Subsystem for Linux) 執行環境

Windows Server 2022 已經跟 WSL 2 已經無縫整合,安裝起來非常的方便!

  1. 查詢有哪些 WSL image 可以用

    wsl --list --online
    
    The following is a list of valid distributions that can be installed.
    The default distribution is denoted by '*'.
    Install using 'wsl --install -d <Distro>'.
    
      NAME            FRIENDLY NAME
    * Ubuntu          Ubuntu
      Debian          Debian GNU/Linux
      kali-linux      Kali Linux Rolling
      openSUSE-42     openSUSE Leap 42
      SLES-12         SUSE Linux Enterprise Server v12
      Ubuntu-16.04    Ubuntu 16.04 LTS
      Ubuntu-18.04    Ubuntu 18.04 LTS
      Ubuntu-20.04    Ubuntu 20.04 LTS
    
  2. 安裝 Ubuntu 20.04 LTS 版本

    wsl --install -d Ubuntu-20.04
    
  3. 重開機

    Restart-Computer -Force
    

安裝 IIS + .NET Framework + ASP.NET 執行環境

  1. 安裝 IIS 服務、管理工具與 .NET Framework 執行環境

    Install-WindowsFeature Web-Server,Web-Asp-Net45,NET-Framework-Features -IncludeManagementTools
    
  2. 安裝 URL Rewrite 模組

    Invoke-WebRequest -Uri https://download.microsoft.com/download/1/2/8/128E2E22-C1B9-44A4-BE2A-5859ED1D4592/rewrite_amd64_en-US.msi -OutFile "$env:USERPROFILE\Downloads\rewrite_amd64_en-US.msi"
    msiexec.exe /i "$env:USERPROFILE\Downloads\rewrite_amd64_en-US.msi" /qn
    
  3. 預設匯入 IISAdministrationWebAdministration 模組

    @'
    Import-Module IISAdministration
    Import-Module WebAdministration
    '@ | Out-File -Append $PROFILE
    

安裝 ASP.NET Core 5.0 執行環境

  1. 安裝 .NET SDK

    # Install the .NET Core 5.0 SDK
    Invoke-WebRequest https://download.visualstudio.microsoft.com/download/pr/c1bfbb13-ad09-459c-99aa-8971582af86e/61553270dd9348d7ba29bacfbb4da7bd/dotnet-sdk-5.0.400-win-x64.exe -outfile $env:temp\dotnet-sdk-5.0.400-win-x64.exe
    Start-Process $env:temp\dotnet-sdk-5.0.400-win-x64.exe -ArgumentList '/quiet' -Wait
    
  2. 安裝 IIS 的 ASP.NET Core Module (ANCM)

    # Install the .NET Core 5.0 Windows Server Hosting bundle
    Invoke-WebRequest https://download.visualstudio.microsoft.com/download/pr/a0f49856-eec9-4962-8d81-b09af6be9435/1d5fc0083b7f7e10ebed181329ca88ae/dotnet-hosting-5.0.9-win.exe -outfile $env:temp\dotnet-hosting-5.0.9-win.exe
    Start-Process $env:temp\dotnet-hosting-5.0.9-win.exe -ArgumentList '/quiet' -Wait
    
    # Restart the web server so that system PATH updates take effect
    net stop was /y
    net start w3svc
    
  3. 查看 .NET 安裝版本

    dotnet --info
    
  4. 測試 ASP.NET Core 執行

    $projectName = "api50"
    New-Item -Path $projectName -ItemType Directory
    cd $projectName
    
    dotnet new globaljson --sdk-version 5.0.400
    dotnet new webapi
    dotnet publish -c Release -o C:\Inetpub\TestSite
    
    $folder = Get-Item -Path "C:\Inetpub\TestSite"
    
    Import-Module WebAdministration
    
    # $appPool = New-WebAppPool -Name TestSite
    # $appPool.managedRuntimeVersion = ""
    # $appPool | Set-Item
    
    New-WebAppPool -Name TestSite
    $appPool = Get-Item IIS:\AppPools\TestSite
    $appPool.managedPipelineMode="Integrated"
    $appPool.managedRuntimeVersion=""
    $appPool | set-item
    
    New-Website -Name "TestSite" -HostHeader "testsite" -Port 80 -PhysicalPath $folder  -ApplicationPool "TestSite"
    New-WebBinding -Name "TestSite" -HostHeader "localhost" -Port 80 -Protocol "http"
    
    $result = curl.exe -s http://localhost/WeatherForecast
    
    if ( $result -like "*temperatureC*" ) {
      Write-Host "TestSite connnection successful!"
    } else {
      Write-Warning "TestSite connection failed!!!";
    }
    
    Remove-WebSite -Name TestSite
    Remove-WebAppPool -Name TestSite
    
    Start-Sleep -Seconds 1
    Remove-Item $folder -Recurse -Force -Confirm:$false
    
    cd ..
    Remove-Item -Path $projectName -Recurse -Force -Confirm:$false
    

安裝 Java 執行環境

  1. 安裝 Adopt OpenJDK 11、Zulu8 與 Maven

    choco install adoptopenjdk11 zulu8 maven -y
    
  2. 設定 JAVA_HOME 環境變數

    $env:JAVA_HOME = 'C:\Program Files\Zulu\zulu-8'
    [System.Environment]::SetEnvironmentVariable('JAVA_HOME','C:\Program Files\Zulu\zulu-8',[System.EnvironmentVariableTarget]::Machine)
    
  3. 安裝 Apache Tomcat 服務

    通常 Spring Boot 在用 Maven 發行 JAR 檔的時候,已經內建了 Tomcat,所以不太需要特別安裝 Tomcat 服務,如果要手動安裝,可以使用以下命令安裝:

    # 安裝 Apache Tomcat 9
    choco install tomcat -y
    
    # 啟動 Apache Tomcat 9 服務
    Start-Service -Name Tomcat9
    
    # 設定 Tomcat 為開機自動啟動
    Set-Service -Name Tomcat9 -StartupType Automatic
    

安裝 Angular 開發環境 (VSCode)

  1. 安裝 Angular CLI 12 工具

    npm i -g @angular/cli@12
    
  2. 關閉分析服務

    ng analytics off
    

安裝 Visual Studio Code 開發環境

  1. 安裝常用擴充套件

    # 安裝 Java 與 Spring Boot 相關擴充套件
    code --install-extension doggy8088.git-extension-pack
    # 安裝 Essential Spring Boot Snippets 擴充套件
    code --install-extension doggy8088.spring-boot-snippets
    # 安裝 Angular Extension Pack 擴充套件
    code --install-extension doggy8088.angular-extension-pack
    # 安裝 Markdown Extension Pack 擴充套件
    code --install-extension doggy8088.markdown-extension-pack
    # 安裝 .NET Core Extension Pack 擴充套件
    code --install-extension doggy8088.netcore-extension-pack
    # 安裝 JavaScript Debugger 擴充套件 (取代 Debugger for Chrome 套件)
    code --install-extension ms-vscode.js-debug
    # 安裝 Prettier - Code formatter 擴充套件
    code --install-extension esbenp.prettier-vscode
    # 安裝 .NET Interactive Notebooks 擴充套件
    code --install-extension ms-dotnettools.dotnet-interactive-vscode
    
    # 安裝 VSCode 繁體中文語言包
    #code --install-extension MS-CEINTL.vscode-language-pack-zh-hant
    
  2. 調整 VSCode 使用者設定檔

    "git.autofetch": true,
    
    "editor.foldingImportsByDefault": true,
    
    "java.home": "C:\\Program Files\\AdoptOpenJDK\\jdk-11.0.11.9-hotspot",
    "java.configuration.runtimes": [
        {
            "name": "JavaSE-1.8",
            "path": "C:\\Program Files\\Zulu\\zulu-8"
        },
        {
            "name": "JavaSE-11",
            "path": "C:\\Program Files\\AdoptOpenJDK\\jdk-11.0.11.9-hotspot",
            "sources": "C:\\Program Files\\AdoptOpenJDK\\jdk-11.0.11.9-hotspot\\src.zip",
            "javadoc": "https://docs.oracle.com/en/java/javase/11/docs/api",
            "default": true
        }
    ],
    "java.debug.settings.hotCodeReplace": "auto",
    "java.saveActions.organizeImports": true,
    

安裝 Hyper-V 相關服務

  1. 安裝相關服務與管理工具

    Add-WindowsFeature Hyper-V,Hyper-V-Tools,Hyper-V-PowerShell -IncludeManagementTools
    
  2. 重開機

    Restart-Computer -Force
    

安裝 Windows Containers 相關服務

注意:安裝 Windows Containers 之前必須先安裝好 Hyper-V 服務。

  1. 安裝 DockerMsftProvider 的過程會提示你要不要安裝 NuGet Provider,這裡必須手動按下 Y 之後繼續。

    Install-Module -Name DockerMsftProvider -Repository PSGallery -Force
    
  2. 使用 PackageManagement PowerShell 模組安裝最新版 Docker。

    Install-Package -Name docker -ProviderName DockerMsftProvider -Force
    
  3. 重開機

    Restart-Computer -Force
    
  4. 查看版本資訊

    docker version
    
    ❯ docker version
    Client: Mirantis Container Runtime
    Version:           20.10.6
    API version:       1.41
    Go version:        go1.13.15
    Git commit:        b3766ff
    Built:             06/29/2021 17:14:16
    OS/Arch:           windows/amd64
    Context:           default
    Experimental:      true
    
    Server: Mirantis Container Runtime
    Engine:
      Version:          20.10.6
      API version:      1.41 (minimum version 1.24)
      Go version:       go1.13.15
      Git commit:       a3dc69e6b9
      Built:            06/29/2021 17:12:49
      OS/Arch:          windows/amd64
      Experimental:     false
    

    日後升級的命令請參考 Get started: Prep Windows for containers 文件說明。

安裝 Web Deployment Tool 3.6 遠端代理程式

  1. 安裝 IIS 的 Management Service 管理服務 (Remote Administration for IIS Manager)

    # 安裝管理服務
    Install-WindowsFeature Web-Mgmt-Service
    
    # 啟用遠端連線
    Set-Itemproperty -path 'HKLM:\Software\Microsoft\WebManagement\Server' -Name 'EnableRemoteManagement' -value '1'
    
    # 設定自動啟動服務
    Set-Service -Name WMSvc -StartupType Automatic
    Start-Service -Name WMSvc
    
  2. 安裝 Web Deployment Tool 3.6 遠端代理程式

    English (x64)

    Invoke-WebRequest https://download.microsoft.com/download/0/1/D/01DC28EA-638C-4A22-A57B-4CEF97755C6C/WebDeploy_amd64_en-US.msi -outfile $env:temp\WebDeploy_amd64_en-US.msi
    Start-Process $env:temp\WebDeploy_amd64_en-US.msi -ArgumentList 'ADDLOCAL=ALL /qn /norestart LicenseAccepted="0"' -Wait
    

    Chinese (Traditional) (x64)

    Invoke-WebRequest https://download.microsoft.com/download/5/4/4/5441215F-78DB-4686-AD05-3FAD23C406D1/WebDeploy_amd64_zh-TW.msi -outfile $env:temp\WebDeploy_amd64_zh-TW.msi
    Start-Process $env:temp\WebDeploy_amd64_zh-TW.msi -ArgumentList 'ADDLOCAL=ALL /qn /norestart LicenseAccepted="0"' -Wait
    

安裝 Windows Admin Center (WAC) 管理服務

微軟正在嘗試用 Windows Admin Center (WAC) 取代 伺服器管理員(Server Manager),所以當你打開 伺服器管理員 的時候,預設就會提醒你要不要安裝 Windows Admin Center (WAC)!

Server Manager

你可以直接到 Try Windows Admin Center on Microsoft Evaluation Center 下載安裝檔 (WindowsAdminCenter2103.2.msi),基本上很順利就可以裝好,預設網址為:

https://HOSTNAME/

注意: 你不用在每台電腦安裝 WAC,也不一定要安裝在 Windows Server 2022 上,任何一台 Windows 10、Windows Server 或 Windows Server Core 都可以安裝 WAC。被管理的作業系統只要額外安裝 WMF 5.1 版或更新版本,基本上就可以被 WAC 管理。

詳細使用方式請參見 Windows Admin Center 官方文件。

相關連結

留言評論