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 实例
- 创建文件共享
- 打开防火墙端口
- 安装 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 目录中查看你的日志。
ASP.NET Core 中文文档 第二章 指南(5) 在 Nano Server 上运行ASP.NET Core的更多相关文章
- ASP.NET Core 中文文档 第二章 指南 (09) 使用 Swagger 生成 ASP.NET Web API 在线帮助测试文档
原文:ASP.NET Web API Help Pages using Swagger 作者:Shayne Boyer 翻译:谢炀(kiler) 翻译:许登洋(Seay) 对于开发人员来说,构建一个消 ...
- ASP.NET Core 中文文档 第二章 指南(4.6)Controller 方法与视图
原文:Controller methods and views 作者:Rick Anderson 翻译:谢炀(Kiler) 校对:孟帅洋(书缘) .张仁建(第二年.夏) .许登洋(Seay) .姚阿勇 ...
- ASP.NET Core 中文文档 第二章 指南(2)用 Visual Studio 和 ASP.NET Core MVC 创建首个 Web API
原文:Building Your First Web API with ASP.NET Core MVC and Visual Studio 作者:Mike Wasson 和 Rick Anderso ...
- ASP.NET Core 中文文档 第二章 指南(4.2)添加 Controller
原文:Adding a controller 翻译:娄宇(Lyrics) 校对:刘怡(AlexLEWIS).何镇汐.夏申斌.孟帅洋(书缘) Model-View-Controller (MVC) 架构 ...
- ASP.NET Core 中文文档 第二章 指南(4.4)添加 Model
原文:Adding a model 作者:Rick Anderson 翻译:娄宇(Lyrics) 校对:许登洋(Seay).孟帅洋(书缘).姚阿勇(Mr.Yao).夏申斌 在这一节里,你将添加一些类来 ...
- ASP.NET Core 中文文档 第二章 指南(4.9)添加验证
原文:Adding Validation 作者:Rick Anderson 翻译:谢炀(Kiler) 校对:孟帅洋(书缘).娄宇(Lyrics).许登洋(Seay) 在本章节中你将为 Movie 模型 ...
- ASP.NET Core 中文文档 第二章 指南(1)用 Visual Studio Code 在 macOS 上创建首个 ASP.NET Core 应用程序
原文:Your First ASP.NET Core Application on a Mac Using Visual Studio Code 作者:Daniel Roth.Steve Smith ...
- ASP.NET Core 中文文档 第二章 指南(3)用 Visual Studio 发布一个 Azure 云 Web 应用程序
原文:Getting Started 作者:Rick Anderson 翻译:谢炀(Kiler) 校对:孟帅洋(书缘).刘怡(AlexLEWIS).何镇汐 设置开发环境 安装最新版本的 Azure S ...
- ASP.NET Core 中文文档 第二章 指南(4.1)ASP.NET Core MVC 与 Visual Studio 入门
原文:Getting started with ASP.NET Core MVC and Visual Studio 作者:Rick Anderson 翻译:娄宇(Lyrics) 校对:刘怡(Alex ...
随机推荐
- REST简介
一说到REST,我想大家的第一反应就是“啊,就是那种前后台通信方式.”但是在要求详细讲述它所提出的各个约束,以及如何开始搭建REST服务时,却很少有人能够清晰地说出它到底是什么,需要遵守什么样的准则. ...
- json与JavaScript对象互换
1,json字符串转化为JavaScript对象: 方法:JSON.parse(string) eg:var account = '{"name":"jaytan&quo ...
- node中的cmd规范
你应该熟悉nodejs模块中的exports对象,你可以用它创建你的模块.例如:(假设这是rocker.js文件) exports.name = function() { console.log('M ...
- Android数据存储之Android 6.0运行时权限下文件存储的思考
前言: 在我们做App开发的过程中基本上都会用到文件存储,所以文件存储对于我们来说是相当熟悉了,不过自从Android 6.0发布之后,基于运行时权限机制访问外置sdcard是需要动态申请权限,所以以 ...
- 阿里云服务器上配置并使用: PHP + Redis + Mysql 从配置到使用
(原创出处为本博客,http://www.cnblogs.com/linguanh/) 目录: 一,下载 二,解压 三,配置与启动 四,测试 Redis 五,配置 phpRedis 扩展 六,综合测试 ...
- C++随笔:.NET CoreCLR之GC探索(2)
首先谢谢 @dudu 和 @张善友 这2位大神能订阅我,本来在写这个系列以前,我一直对写一些核心而且底层的知识持怀疑态度,我为什么持怀疑态度呢?因为一般写高层语言的人99%都不会碰底层,其实说句实话, ...
- SignalR系列目录
[置顶]用SignalR 2.0开发客服系统[系列1:实现群发通讯] [置顶]用SignalR 2.0开发客服系统[系列2:实现聊天室] [置顶]用SignalR 2.0开发客服系统[系列3:实现点对 ...
- UML图中经常用到几种的关系图例
学习这个东西挺奇怪的,时间一长就容易忘记,或者记不清楚.今天看到一些UML图的关系,发现有些出入了,索性就写下来,以后再忘记的时候过来看看. 在UML的类图中,常见的有以下几种关系: 继承(Gener ...
- 解决使用IE8打开ADFS 3.0登录页面
系统上线前一天,发现客户竟然有XP系统和2003系统,这些系统都不能访问外网.测试时,客户端是IE8,打开我们系统ADFS的登录页面,一直在Loading,无法打开,也不报错.后来通过fiddler跟 ...
- Android中的LinearLayout布局
LinearLayout : 线性布局 在一般情况下,当有很多控件需要在一个界面列出来时,我们就可以使用线性布局(LinearLayout)了, 线性布局是按照垂直方向(vertical)或水平方向 ...