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 ...
随机推荐
- ABP文档 - Hangfire 集成
文档目录 本节内容: 简介 集成 Hangfire 面板授权 简介 Hangfire是一个综合的后台作业管理器,可以在ABP里集成它替代默认的后台作业管理器,你可以为Hangfire使用相同的后台作业 ...
- Vue-Router 页面正在加载特效
Vue-Router 页面正在加载特效 如果你在使用 Vue.js 和 Vue-Router 开发单页面应用.因为每个页面都是一个 Vue 组件,你需要从服务器端请求数据,然后再让 Vue 引擎来渲染 ...
- Angular企业级开发(3)-Angular MVC实现
1.MVC介绍 Model-View-Controller 在20世纪80年代为程序语言Smalltalk发明的一种软件架构.MVC模式的目的是实现一种动态的程序设计,使后续对程序的修改和扩展简化,并 ...
- PHP类和对象之重载
PHP中的重载指的是动态的创建属性与方法,是通过魔术方法来实现的.属性的重载通过__set,__get,__isset,__unset来分别实现对不存在属性的赋值.读取.判断属性是否设置.销毁属性. ...
- 如何优化coding
如何优化coding 前言 最近一直在做修改bug工作,修改bug花费时间最多的不是如何解决问题而是怎样快速读懂代码.如果代码写的好的,不用debug就可以一眼看出来哪里出了问题.实际上,我都要deb ...
- H3 BPM让天下没有难用的流程之产品概述
一.产品简介 BPM(Business Process Management),是指根据业务环境的变化,推进人与人之间.人与系统之间以及系统与系统之间的整合及调整的经营方法与解决方案的IT工具. H3 ...
- JDK安装与配置
JDK安装与配置 一.下载 JDK是ORACLE提供免费下载使用的,官网地址:https://www.oracle.com/index.html 一般选择Java SE版本即可,企业版的选择Java ...
- 基于jQuery左右滑动切换特效 附源码
分享一款基于脚jQuery左右滑动切换特效.这是一款鼠标点击左右箭头按钮图片滚动切换,鼠标移到图片上显示透明边框特效. 效果图如下: 废话不多说,代码奉上! html代码: <div ...
- Prometheus 系统监控方案 一
最近一直在折腾时序类型的数据库,经过一段时间项目应用,觉得十分不错.而Prometheus又是刚刚推出不久的开源方案,中文资料较少,所以打算写一系列应用的实践过程分享一下. Prometheus 是什 ...
- photoshop:无法完成请求 因为暂存盘已满
今天photoshop打开一个问题,提醒:无法完成请求因为暂存盘已满 不用担心这个问题很好解决可能是你做的图比较大并不需要清理C盘空间 选择:编辑→首选项→暂存盘 设置第一暂存盘为D盘或E盘 总之 第 ...