设为首页 收藏本站
查看: 984|回复: 0

[经验分享] ASP.NET Core 中文文档 第二章 指南(5) 在 Nano Server 上运行ASP.NET Core

[复制链接]

尚未签到

发表于 2017-6-22 06:18:42 | 显示全部楼层 |阅读模式
  原文 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 &quot;AspNet5 IIS&quot; -DisplayName &quot;Allow HTTP on TCP/8000&quot; -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=&quot;C:\windows\system32\inetsrv\aspnetcore.dll&quot;
  
Reset-IISServerManager -confirm:$false
  
$sm = Get-IISServerManager
  

  
# Add AppSettings section
  
$sm.GetApplicationHostConfiguration().RootSectionGroup.Sections.Add(&quot;appSettings&quot;)
  

  
# Set Allow for handlers section
  
$appHostconfig = $sm.GetApplicationHostConfiguration()
  
$section = $appHostconfig.GetSection(&quot;system.webServer/handlers&quot;)
  
$section.OverrideMode=&quot;Allow&quot;
  

  
# Add aspNetCore section to system.webServer
  
$sectionaspNetCore = $appHostConfig.RootSectionGroup.SectionGroups[&quot;system.webServer&quot;].Sections.Add(&quot;aspNetCore&quot;)
  
$sectionaspNetCore.OverrideModeDefault = &quot;Allow&quot;
  
$sm.CommitChanges()
  

  
# Configure globalModule
  
Reset-IISServerManager -confirm:$false
  
$globalModules = Get-IISConfigSection &quot;system.webServer/globalModules&quot; | Get-IISConfigCollection
  
New-IISConfigCollectionElement $globalModules -ConfigAttribute @{&quot;name&quot;=&quot;AspNetCoreModule&quot;;&quot;image&quot;=$aspNetCoreHandlerFilePath}
  

  
# Configure module
  
$modules = Get-IISConfigSection &quot;system.webServer/modules&quot; | Get-IISConfigCollection
  
New-IISConfigCollectionElement $modules -ConfigAttribute @{&quot;name&quot;=&quot;AspNetCoreModule&quot;}
  

  
# 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 = &quot;https://dotnetcli.blob.core.windows.net/dotnet/beta/Binaries/Latest/dotnet-win-x64.latest.zip&quot;  
$DestinationPath = &quot;C:\dotnet&quot;
  

  
$EditionId = (Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' -Name 'EditionID').EditionId
  

  
if (($EditionId -eq &quot;ServerStandardNano&quot;) -or
  ($EditionId -eq &quot;ServerDataCenterNano&quot;) -or
  ($EditionId -eq &quot;NanoServer&quot;) -or
  ($EditionId -eq &quot;ServerTuva&quot;)) {
  

  $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 &quot;Cannot copy from $SourcePath&quot;
  }
  [System.IO.Compression.ZipFile]::ExtractToDirectory($TempPath, $DestinationPath)
  Remove-Item $TempPath
  
}
  

发布应用程序
  将你发布好的现有应用程序复制到文件共享。
  您可能需要修改你的 web.config 文件指向你解压缩 dotnet.exe 文件的路径。或者,你也可以把 dotnet.exe 添加到你的路径。
  下面是一个修改 web.config 的例子,当 dotnet.exe 不在 当前路径中:
  

<?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?>  
<configuration>
  <system.webServer>
  <handlers>
  <add name=&quot;aspNetCore&quot; path=&quot;*&quot; verb=&quot;*&quot; modules=&quot;AspNetCoreModule&quot; resourceType=&quot;Unspecified&quot; />
  </handlers>
  <aspNetCore processPath=&quot;C:\dotnet\dotnet.exe&quot; arguments=&quot;.\AspNetCoreSampleForNano.dll&quot; stdoutLogEnabled=&quot;false&quot; stdoutLogFile=&quot;.\logs\stdout&quot; forwardWindowsAuthToken=&quot;true&quot; />
  </system.webServer>
  
</configuration>
  

  在远程会话中运行以下命令在IIS中为已发布的应用创建一个新的站点。此脚本只是简单的使用了 DefaultAppPool 。更多关于应用程序池的信息,请参阅 Application Pools 。
  

Import-module IISAdministration  
New-IISSite -Name &quot;AspNetCore&quot; -PhysicalPath c:\PublishedApps\AspNetCoreSampleForNano -BindingInformation &quot;*:8000:&quot;
  

运行应用程序
  发布好的 Web 应用程序可以通过浏览器打开 http://<nanoserver-ip-address>:8000 链接访问。
  
如果你按照 Log creation and redirection 介绍的方式设置好了日志。你应该能够在 C:\PublishedApps\AspNetCoreSampleForNano\logs 目录中查看你的日志。
  返回目录

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-386590-1-1.html 上篇帖子: 使用Visual Studio进行 Android开发的十大理由 下篇帖子: [转载]矩阵求导公式
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表