AspNetCore.Docs/aspnetcore/tutorials/nano-server.md

205 lines
10 KiB
Markdown
Raw Normal View History

---
2017-07-01 07:47:15 +08:00
title: ASP.NET Core on Nano Server
author: shirhatti
description: Learn how to take an existing ASP.NET Core app and deploy it to a Nano Server instance running IIS.
keywords: ASP.NET Core, nano server
2016-10-29 01:35:15 +08:00
ms.author: riande
manager: wpickett
ms.date: 11/04/2016
2016-10-29 01:35:15 +08:00
ms.topic: article
ms.assetid: 50922cf1-ca58-4006-9236-99b7ff2dd0cf
2016-11-17 08:24:57 +08:00
ms.technology: aspnet
ms.prod: asp.net-core
uid: tutorials/nano-server
2016-10-29 01:35:15 +08:00
---
# ASP.NET Core with IIS on Nano Server
2016-10-29 01:35:15 +08:00
By [Sourabh Shirhatti](https://twitter.com/sshirhatti)
In this tutorial, you'll take an existing ASP.NET Core app and deploy it to a Nano Server instance running IIS.
## Introduction
Nano Server is an installation option in Windows Server 2016, offering a tiny footprint, better security, and better servicing than Server Core or full Server. Please consult the official [Nano Server documentation](https://technet.microsoft.com/library/mt126167.aspx) for more details and download links for 180 Days evaluation versions.
2016-10-29 01:35:15 +08:00
There are three easy ways for you to try out Nano Server. When you sign in with your MS account:
2016-10-29 01:35:15 +08:00
1. You can download the Windows Server 2016 ISO file and build a Nano Server image.
2016-10-29 01:35:15 +08:00
2. Download the Nano Server VHD.
2016-10-29 01:35:15 +08:00
3. Create a VM in Azure using the Nano Server image in the Azure Gallery. If you dont have an Azure account, you can get a free 30-day trial.
2016-10-29 01:35:15 +08:00
In this tutorial, we will be using the 2nd option, the pre-built Nano Server VHD from Windows Server 2016.
2016-10-29 01:35:15 +08:00
Before proceeding with this tutorial, you will need the [published output](xref:hosting/directory-structure) of an existing ASP.NET Core application. Ensure your application is built to run in a **64-bit** process.
## Setting up the Nano Server instance
2016-10-29 01:35:15 +08:00
[Create a new Virtual Machine using Hyper-V](https://technet.microsoft.com/library/hh846766.aspx) on your development machine using the previously downloaded VHD. The machine will require you to set an administrator password before logging on. At the VM console, press F11 to set the password before the first log in. You also need to check your new VM's IP address either my checking your DHCP server's fixed IP supplied while provisioning your VM or in Nano Server recovery console's networking settings.
> [!NOTE]
> Let's assume your new VM runs with the local V4 IP address 192.168.1.10.
2016-10-29 01:35:15 +08:00
Now you're able to manage it using PowerShell remoting, which is the only way to fully administer your Nano Server.
2016-10-29 01:35:15 +08:00
## Connecting to your Nano Server instance using PowerShell Remoting
2016-10-29 01:35:15 +08:00
Open an elevated PowerShell window to add your remote Nano Server instance to your `TrustedHosts` list.
```PowerShell
$nanoServerIpAddress = "192.168.1.10"
Set-Item WSMan:\localhost\Client\TrustedHosts "$nanoServerIpAddress" -Concatenate -Force
2016-11-18 13:03:07 +08:00
```
2016-10-29 01:35:15 +08:00
> [!NOTE]
> Replace the variable `$nanoServerIpAddress` with the correct IP address.
Once you have added your Nano Server instance to your `TrustedHosts`, you can connect to it using PowerShell remoting.
2016-10-29 01:35:15 +08:00
```PowerShell
2016-10-29 01:35:15 +08:00
$nanoServerSession = New-PSSession -ComputerName $nanoServerIpAddress -Credential ~\Administrator
Enter-PSSession $nanoServerSession
```
A successful connection results in a prompt with a format looking like: `[192.168.1.10]: PS C:\Users\Administrator\Documents>`
2016-10-29 01:35:15 +08:00
## Creating a file share
Create a file share on the Nano server so that the published application can be copied to it. Run the following commands in the remote session:
```PowerShell
New-Item C:\PublishedApps\AspNetCoreSampleForNano -type directory
netsh advfirewall firewall set rule group="File and Printer Sharing" new enable=yes
net share AspNetCoreSampleForNano=c:\PublishedApps\AspNetCoreSampleForNano /GRANT:EVERYONE`,FULL
2016-11-18 13:03:07 +08:00
```
2016-10-29 01:35:15 +08:00
After running the above commands, you should be able to access this share by visiting `\\192.168.1.10\AspNetCoreSampleForNano` in the host machine's Windows Explorer.
2016-10-29 01:35:15 +08:00
## Open port in the firewall
2016-10-29 01:35:15 +08:00
Run the following commands in the remote session to open up a port in the firewall to let IIS listen for TCP traffic on port TCP/8000.
2016-10-29 01:35:15 +08:00
```PowerShell
2016-10-29 01:35:15 +08:00
New-NetFirewallRule -Name "AspNet5 IIS" -DisplayName "Allow HTTP on TCP/8000" -Protocol TCP -LocalPort 8000 -Action Allow -Enabled True
```
2016-10-29 01:35:15 +08:00
## Installing IIS
Add the `NanoServerPackage` provider from the PowerShell Gallery. Once the provider is installed and imported, you can install Windows packages.
2016-10-29 01:35:15 +08:00
Run the following commands in the PowerShell session that was created earlier:
```PowerShell
2016-10-29 01:35:15 +08:00
Install-PackageProvider NanoServerPackage
Import-PackageProvider NanoServerPackage
Install-NanoServerPackage -Name Microsoft-NanoServer-Storage-Package
Install-NanoServerPackage -Name Microsoft-NanoServer-IIS-Package
```
2016-10-29 01:35:15 +08:00
To quickly verify if IIS is setup correctly, you can visit the URL `http://192.168.1.10/` and should see a welcome page. When IIS is installed, a website called `Default Web Site` listening on port 80 is created by default.
2016-10-29 01:35:15 +08:00
## Installing the ASP.NET Core Module (ANCM)
2016-10-29 01:35:15 +08:00
The ASP.NET Core Module is an IIS 7.5+ module which is responsible for process management of ASP.NET Core HTTP listeners and to proxy requests to processes that it manages. At the moment, the process to install the ASP.NET Core Module for IIS is manual. You will need to install the [.NET Core Windows Server Hosting bundle](https://aka.ms/dotnetcore.2.0.0-windowshosting) on a regular (not Nano) machine. After installing the bundle on a regular machine, you will need to copy the following files to the file share that we created earlier.
2016-10-29 01:35:15 +08:00
On a regular (not Nano) server with IIS, run the following copy commands:
2016-10-29 01:35:15 +08:00
```PowerShell
Copy-Item -Path C:\windows\system32\inetsrv\aspnetcore.dll -Destination `\\<nanoserver-ip-address>\AspNetCoreSampleForNano`
Copy-Item -Path C:\windows\system32\inetsrv\config\schema\aspnetcore_schema.xml -Destination `\\<nanoserver-ip-address>\AspNetCoreSampleForNano`
2016-11-18 13:03:07 +08:00
```
Replace `C:\windows\system32\inetsrv` with `C:\Program Files\IIS Express` on a Windows 10 machine.
On the Nano side, you will need to copy the following files from the file share that we created earlier to the valid locations. So, run the following copy commands:
2016-10-29 01:35:15 +08:00
```PowerShell
Copy-Item -Path C:\PublishedApps\AspNetCoreSampleForNano\aspnetcore.dll -Destination C:\windows\system32\inetsrv\
Copy-Item -Path C:\PublishedApps\AspNetCoreSampleForNano\aspnetcore_schema.xml -Destination C:\windows\system32\inetsrv\config\schema\
2016-11-18 13:03:07 +08:00
```
2016-10-29 01:35:15 +08:00
Run the following script in the remote session:
```PowerShell
2016-10-29 01:35:15 +08:00
# Backup existing applicationHost.config
Copy-Item -Path C:\Windows\System32\inetsrv\config\applicationHost.config -Destination C:\Windows\System32\inetsrv\config\applicationHost_BeforeInstallingANCM.config
Import-Module IISAdministration
2016-10-29 01:35:15 +08:00
# Initialize variables
$aspNetCoreHandlerFilePath="C:\windows\system32\inetsrv\aspnetcore.dll"
Reset-IISServerManager -confirm:$false
$sm = Get-IISServerManager
2016-10-29 01:35:15 +08:00
# Add AppSettings section
$sm.GetApplicationHostConfiguration().RootSectionGroup.Sections.Add("appSettings")
2016-10-29 01:35:15 +08:00
# Set Allow for handlers section
$appHostconfig = $sm.GetApplicationHostConfiguration()
$section = $appHostconfig.GetSection("system.webServer/handlers")
$section.OverrideMode="Allow"
2016-10-29 01:35:15 +08:00
# Add aspNetCore section to system.webServer
$sectionaspNetCore = $appHostConfig.RootSectionGroup.SectionGroups["system.webServer"].Sections.Add("aspNetCore")
$sectionaspNetCore.OverrideModeDefault = "Allow"
$sm.CommitChanges()
2016-10-29 01:35:15 +08:00
# Configure globalModule
Reset-IISServerManager -confirm:$false
$globalModules = Get-IISConfigSection "system.webServer/globalModules" | Get-IISConfigCollection
New-IISConfigCollectionElement $globalModules -ConfigAttribute @{"name"="AspNetCoreModule";"image"=$aspNetCoreHandlerFilePath}
2016-10-29 01:35:15 +08:00
# Configure module
$modules = Get-IISConfigSection "system.webServer/modules" | Get-IISConfigCollection
New-IISConfigCollectionElement $modules -ConfigAttribute @{"name"="AspNetCoreModule"}
```
2016-10-29 01:35:15 +08:00
> [!NOTE]
> Delete the files *aspnetcore.dll* and *aspnetcore_schema.xml* from the share after the above step.
2016-10-29 01:35:15 +08:00
## Installing .NET Core Framework
If you published a Framework-dependent (portable) app, .NET Core must be installed on the target machine. Execute the following PowerShell script in a remote PowerShell session to install the .NET Framework on your Nano Server.
> [!NOTE]
> To understand the differences between Framework-dependent deployments (FDD) and Self-contained deployments (SCD), see [deployment options](https://docs.microsoft.com/dotnet/articles/core/deploying/).
2016-10-29 01:35:15 +08:00
[!code-powershell[Main](nano-server/Download-Dotnet.ps1)]
## Publishing the application
Copy the published output of your existing application to the file share's root.
2016-10-29 01:35:15 +08:00
You may need to make changes to your *web.config* to point to where you extracted *dotnet.exe*. Alternatively, you can add *dotnet.exe* to your PATH.
2016-10-29 01:35:15 +08:00
Example of how a *web.config* might look if *dotnet.exe* is **not** on the PATH:
2016-10-29 01:35:15 +08:00
```xml
2016-10-29 01:35:15 +08:00
<?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\aspnetcore-stdout" forwardWindowsAuthToken="true" />
</system.webServer>
</configuration>
2016-11-18 13:03:07 +08:00
```
2016-10-29 01:35:15 +08:00
Run the following commands in the remote session to create a new site in IIS for the published app on a different port than the default website. You also need to open that port to access the web. This script uses the `DefaultAppPool` for simplicity. For more considerations on running under an application pool, see [Application Pools](xref:publishing/iis#application-pools).
2016-10-29 01:35:15 +08:00
```PowerShell
Import-module IISAdministration
New-IISSite -Name "AspNetCore" -PhysicalPath c:\PublishedApps\AspNetCoreSampleForNano -BindingInformation "*:8000:"
```
2016-10-29 01:35:15 +08:00
## Known issue running .NET Core CLI on Nano Server and workaround
```PowerShell
New-NetFirewallRule -Name "AspNetCore Port 81 IIS" -DisplayName "Allow HTTP on TCP/81" -Protocol TCP -LocalPort 81 -Action Allow -Enabled True
```
2016-10-29 01:35:15 +08:00
## Running the application
The published web app is accessible in a browser at `http://192.168.1.10:8000`. If you've set up logging as described in [Log creation and redirection](xref:hosting/aspnet-core-module#log-creation-and-redirection), you can view your logs at *C:\PublishedApps\AspNetCoreSampleForNano\logs*.