推荐
关注
功能
  • 分享到微博
  • 分享到微信
  • 分享到豆瓣
  • 分享到 QQ
  • 分享到 QZone
  • 收藏到博客园
  • 联系博主
TOP
Message

ASP.NET Core 中文文档 第二章 指南(5) 在 Nano Server 上运行ASP.NET Core

原文 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 是 Windows Server 2016 附属的一个安装选项,比 Server Core 或者 full Server 提供更小的安装体积、更好的安全性以及更好的服务性能。 请参考官方 Nano Server documentation 获取更多内容。有以下三种方法来试用 Nano Server:

  1. 你可以下载 Windows Server 2016 Technical Preview 5 ISO 文件,并且生成 Nano Server 镜像。
  2. 下载 Nano Server 开发者 VHD (虚拟磁盘文件)
  3. 在 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.dllaspnetcore_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 目录中查看你的日志。

返回目录

posted @ 2016-07-08 11:36  .NET_CORE_COMMUNITY  阅读(6671)  评论(6编辑  收藏  举报