原文 ASP.NET Core on Nano Server
作者 Sourabh Shirhatti
翻譯 婁宇(Lyrics)
校對 劉怡(AlexLEWIS)、許登洋(Seay)、謝煬(kiler)、孟帥洋(書緣)
注意:本教程使用 Windows Server Technical Preview 5 的預發行版本的 Nano Server 安裝選項。 你可以在虛擬硬盤映像中用來內部演示和評估,但不能在生產環境中使用該軟件。可通過 https://go.microsoft.com/fwlink/?LinkId=624232 查看預覽版本的截止日期信息。
在本教程中,你將使用一個現有的 ASP.NET Core 應用程序並將其部署在一個 Nano Server 實例的 IIS 上。
章節:
- 介紹
- 設置 Nano Server 實例
- 創建文件共享
- 打開防火牆端口
- 安裝 IIS
- 安裝 ASP.NET Core Module (ANCM)
- 安裝 .NET Core Framework
- 發布應用程序
- 運行應用程序
介紹
Nano Server 是 Windows Server 2016 附屬的一個安裝選項,比 Server Core 或者 full Server 提供更小的安裝體積、更好的安全性以及更好的服務性能。 請參考官方 Nano Server documentation 獲取更多內容。有以下三種方法來試用 Nano Server:
- 你可以下載 Windows Server 2016 Technical Preview 5 ISO 文件,並且生成 Nano Server 鏡像。
- 下載 Nano Server 開發者 VHD (虛擬磁盤文件)
- 在 Azure 中從 Azure Gallery 使用 Nano Server 鏡像創建虛擬機。如果沒有 Azure 賬戶,你可以申請一個 30天免費試用賬戶。
在本教程中,我們將使用在 Windows Server Technical Preview 5 中預創建的 Nano Server Developer VHD 。
在進行本教程之前,你需要 發布 已有的 ASP.NET Core 應用程序。並確保你的程序是構建在 64 位 進程中運行的。
設置 Nano Server 實例
在你的開發機上 通過 Hyper-V 創建一個新的虛擬機 並使用之前下載的 VHD 。這個虛擬機需要你在登錄前設置一個管理員密碼。第一次登錄前,在虛擬機(VM)控制台按 F11 設置密碼。
在設置本地密碼之后,你將通過 PowerShell Remoting 管理 Nano Server 。
通過 PowerShell Remoting 連接你的 Nano Server 實例
打開一個提升過權限的 PowerShell 窗口來添加你的遠程 Nano Server 實例到你的 受信任的主機(TrustedHosts) 列表。
$nanoServerIpAddress = "10.83.181.14"
Set-Item WSMan:\localhost\Client\TrustedHosts "$nanoServerIpAddress" -Concatenate -Force
注意:把
$nanoServerIpAddress替換為對應的 IP 地址。
一旦你添加了 Nano Server 實例到你的 受信任的主機(TrustedHosts) 列表,你就可以用 PowerShell Remoting 連接它了。
$nanoServerSession = New-PSSession -ComputerName $nanoServerIpAddress -Credential ~\Administrator
Enter-PSSession $nanoServerSession
成功連接的命令提示符將看起來像這樣 [10.83.181.14]: PS C:\Users\Administrator\Documents>
創建文件共享
在 Nano server 上創建文件共享這樣可以直接拷貝發布的程序,在遠程服務器上運行下述命令:
mkdir C:\PublishedApps\AspNetCoreSampleForNano
netsh advfirewall firewall set rule group="File and Printer Sharing" new enable=yes
net share AspNetCoreSampleForNano=c:\PublishedApps\AspNetCoreSampleForNano /GRANT:EVERYONE`,FULL
上述命令運行完以后,你就可以在你本機使用 Windows 文件瀏覽器通過 \\<nanoserver-ip-address>\AspNetCoreSampleForNano 地址訪問共享文件了。
打開防火牆端口
在遠程會話中運行下面的命令在防火牆中打開一個端口來監聽TCP流量。
New-NetFirewallRule -Name "AspNet5 IIS" -DisplayName "Allow HTTP on TCP/8000" -Protocol TCP -LocalPort 8000 -Action Allow -Enabled True
安裝 IIS
從 PowerShell 庫平台(PowerShell gallery)中添加 NanoServerPackage 提供程序(provider),一旦提供程序(provider)被安裝或者導入,你就可以安裝 Window 包了。
在前面創建的 PowerShell 會話中運行以下代碼:
Install-PackageProvider NanoServerPackage
Import-PackageProvider NanoServerPackage
Install-NanoServerPackage -Name Microsoft-NanoServer-IIS-Package
為了快速驗證ISS是否正確安裝,你可以訪問 http://<nanoserver-ip-address>/ 鏈接看看是否可以顯示歡迎頁面。當IIS被安裝好以后,默認會創建一個名為 Default Web Site 的網站在80端口上偵聽。
安裝 ASP.NET Core Module (ANCM)
ASP.NET Core Module 是一個適用於 IIS 7.5 及以上版本的組件,它用來負責 ASP.NET Core HTTP 監聽器的過程管理和代理請求的過程管理。 目前需要手動在 IIS 上安裝 ASP.NET Core 組件。你需要在你的常規機(不是 Nano Server)上安裝最新的 64 位版本的 .NET Core Windows Server Hosting bundle 。安裝之后您需要將以下文件復制到我們前面創建的共享文件:
在常規機(不是 Nano Server)上運行下述拷貝命令:
copy C:\windows\system32\inetsrv\aspnetcore.dll ``\\<nanoserver-ip-address>\AspNetCoreSampleForNano``
copy C:\windows\system32\inetsrv\config\schema\aspnetcore_schema.xml ``\\<nanoserver-ip-address>\AspNetCoreSampleForNano``
在 Nano 主機中你需要從前面創建的文件共享中復制下面的文件到對應的位置。
運行下面拷貝腳本:
copy C:\PublishedApps\AspNetCoreSampleForNano\aspnetcore.dll C:\windows\system32\inetsrv\
copy C:\PublishedApps\AspNetCoreSampleForNano\aspnetcore_schema.xml C:\windows\system32\inetsrv\config\schema\
在遠程會話中運行下面的腳本:
# Backup existing applicationHost.config
copy C:\Windows\System32\inetsrv\config\applicationHost.config C:\Windows\System32\inetsrv\config\applicationHost_BeforeInstallingANCM.config
Import-Module IISAdministration
# Initialize variables
$aspNetCoreHandlerFilePath="C:\windows\system32\inetsrv\aspnetcore.dll"
Reset-IISServerManager -confirm:$false
$sm = Get-IISServerManager
# Add AppSettings section
$sm.GetApplicationHostConfiguration().RootSectionGroup.Sections.Add("appSettings")
# Set Allow for handlers section
$appHostconfig = $sm.GetApplicationHostConfiguration()
$section = $appHostconfig.GetSection("system.webServer/handlers")
$section.OverrideMode="Allow"
# Add aspNetCore section to system.webServer
$sectionaspNetCore = $appHostConfig.RootSectionGroup.SectionGroups["system.webServer"].Sections.Add("aspNetCore")
$sectionaspNetCore.OverrideModeDefault = "Allow"
$sm.CommitChanges()
# Configure globalModule
Reset-IISServerManager -confirm:$false
$globalModules = Get-IISConfigSection "system.webServer/globalModules" | Get-IISConfigCollection
New-IISConfigCollectionElement $globalModules -ConfigAttribute @{"name"="AspNetCoreModule";"image"=$aspNetCoreHandlerFilePath}
# Configure module
$modules = Get-IISConfigSection "system.webServer/modules" | Get-IISConfigCollection
New-IISConfigCollectionElement $modules -ConfigAttribute @{"name"="AspNetCoreModule"}
# Backup existing applicationHost.config
copy C:\Windows\System32\inetsrv\config\applicationHost.config C:\Windows\System32\inetsrv\config\applicationHost_AfterInstallingANCM.config
注意:從共享中刪除
aspnetcore.dll和aspnetcore_schema.xml文件在上述步驟之后。
安裝 .NET Core Framework
如果你發布移動應用, .NET Core 必須安裝在目標機器。 在遠程 Powershell 會話中執行下述 Powershell 腳本來在你的 Nano Server 安裝 .NET Framework。
$SourcePath = "https://dotnetcli.blob.core.windows.net/dotnet/beta/Binaries/Latest/dotnet-win-x64.latest.zip"
$DestinationPath = "C:\dotnet"
$EditionId = (Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' -Name 'EditionID').EditionId
if (($EditionId -eq "ServerStandardNano") -or
($EditionId -eq "ServerDataCenterNano") -or
($EditionId -eq "NanoServer") -or
($EditionId -eq "ServerTuva")) {
$TempPath = [System.IO.Path]::GetTempFileName()
if (($SourcePath -as [System.URI]).AbsoluteURI -ne $null)
{
$handler = New-Object System.Net.Http.HttpClientHandler
$client = New-Object System.Net.Http.HttpClient($handler)
$client.Timeout = New-Object System.TimeSpan(0, 30, 0)
$cancelTokenSource = [System.Threading.CancellationTokenSource]::new()
$responseMsg = $client.GetAsync([System.Uri]::new($SourcePath), $cancelTokenSource.Token)
$responseMsg.Wait()
if (!$responseMsg.IsCanceled)
{
$response = $responseMsg.Result
if ($response.IsSuccessStatusCode)
{
$downloadedFileStream = [System.IO.FileStream]::new($TempPath, [System.IO.FileMode]::Create, [System.IO.FileAccess]::Write)
$copyStreamOp = $response.Content.CopyToAsync($downloadedFileStream)
$copyStreamOp.Wait()
$downloadedFileStream.Close()
if ($copyStreamOp.Exception -ne $null)
{
throw $copyStreamOp.Exception
}
}
}
}
else
{
throw "Cannot copy from $SourcePath"
}
[System.IO.Compression.ZipFile]::ExtractToDirectory($TempPath, $DestinationPath)
Remove-Item $TempPath
}
發布應用程序
將你發布好的現有應用程序復制到文件共享。
您可能需要修改你的 web.config 文件指向你解壓縮 dotnet.exe 文件的路徑。或者,你也可以把 dotnet.exe 添加到你的路徑。
下面是一個修改 web.config 的例子,當 dotnet.exe 不在 當前路徑中:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="C:\dotnet\dotnet.exe" arguments=".\AspNetCoreSampleForNano.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="true" />
</system.webServer>
</configuration>
在遠程會話中運行以下命令在IIS中為已發布的應用創建一個新的站點。此腳本只是簡單的使用了 DefaultAppPool 。更多關於應用程序池的信息,請參閱 Application Pools 。
Import-module IISAdministration
New-IISSite -Name "AspNetCore" -PhysicalPath c:\PublishedApps\AspNetCoreSampleForNano -BindingInformation "*:8000:"
運行應用程序
發布好的 Web 應用程序可以通過瀏覽器打開 http://<nanoserver-ip-address>:8000 鏈接訪問。
如果你按照 Log creation and redirection 介紹的方式設置好了日志。你應該能夠在 C:\PublishedApps\AspNetCoreSampleForNano\logs 目錄中查看你的日志。
