Installing web services and deploying a web application to a remote server

Sometimes a package may contain more than just a nuspec file and an install script. You may also want to install the package onto a remote machine.

Boxstarter can create a template package targeting an existing directory and install the package on any machine open to WINRM or WMI remoting.

This walk through will create a Nuget ODATA feed application and deploy it to a remote server after enabling web services on the server.

Step 1

Create and Build the Nuget server

Create an empty Web Application in Visual Studio and add a Nuget reference to Nuget.Server. After the package install completes, build the solution. NOTE: This can be performed with the free Visual Studio Express for Web which can be installed via Chocolatey - cinst VisualStudio2013ExpressWeb.

Step 2

Install Boxstarter

If you have Chocolatey:

CINST Boxstarter

Otherwise, click on the download link at the top of this page and run the Setup.bat file. See Installing Boxstarter for details.

Step 3

Invoke the Boxstarter shell

The Boxstarter install creates a shortcut to a shell that provides the easiest way to access Boxstarter commands especially if you are not familiar with Powershell. See Using Boxstarter Commands for information about running Boxstarter in your own shell.

Step 4

Create the Package

Lets assume that you saved the above Visual Studio project to c:\dev\NugetServer. You can now create a package with the artifacts of this project embedded in the package:

New-BoxstarterPackage -Name NugetServer
Copy-Item c:\dev\NugetServer "$($Boxstarter.LocalRepo)\NugetServer" -Recurse -Force

This creates a skeleton Chocolatey package with the contents of your NugetServer application embedded in the package. You will need to complete the ChocolateyInstall.ps1 file.

Step 5

Add installation details to ChocolateyInstall.ps1

Boxstarter has created a skeletal ChocolateyInstall.ps1 file at $($Boxstarter.LocalRepo)\NugetServer\tools\chocolateyInstall.ps1. Edit this file and replace its contents with the following script:

try {
    cinst DotNet4.5

    #Enable Web Services
    cinst IIS-WebServerRole -source WindowsFeatures
    cinst IIS-ISAPIFilter -source WindowsFeatures
    cinst IIS-ISAPIExtensions -source WindowsFeatures

    #Enable ASP.NET on win 2012/8
    cinst IIS-NetFxExtensibility45 -source WindowsFeatures
    cinst NetFx4Extended-ASPNET45 -source WindowsFeatures
    cinst IIS-ASPNet45 -source WindowsFeatures

    #Enable ASP.NET on win 7/2008R2
    ."$env:windir\microsoft.net\framework\v4.0.30319\aspnet_regiis.exe" -i

    #clean and create application
    Remove-Item c:\web\NugetServer -Recurse -Force -ErrorAction SilentlyContinue
    Mkdir c:\web\NugetServer -ErrorAction SilentlyContinue
    Copy-Item "$(Join-Path (Get-PackageRoot $MyInvocation ) NugetServer)\*" c:\web\NugetServer -Recurse -Force
    Import-Module WebAdministration
    Remove-WebSite -Name "Default Web Site" -ErrorAction SilentlyContinue
    Remove-WebSite -Name NugetServer -ErrorAction SilentlyContinue
    New-WebSite -ID 1 -Name NugetServer -Port 80 -PhysicalPath c:\web\NugetServer -Force

    #Client SKUs need to enable firewall
    netsh advfirewall firewall add rule name="Open Port 80" dir=in action=allow protocol=TCP localport=80
    
    Write-ChocolateySuccess 'NugetServer'
} catch {
  Write-ChocolateyFailure 'NugetServer' $($_.Exception.ToString())
  throw
}

This script enables the minimum IIS features for the application to run, copies the application to the computer installing the package, creates a web application in IIS to point to the application directory and allows http traffic through the firewall.

Step 6

Build the package

We need to build our package script and application files into a single .nupkg file. Run:

Invoke-BoxstarterBuild -Name NugetServer

Step 7

Ensure Powershell Remoting is enabled on the target machine

In many cases this step is unnecessary. If you are using either the builtin administrator account or a domain account with admin privileges on a server operating system, remoting is on by default. Otherwise, run the following command on the machine with a user who has admin privileges:

Enable-PSRemoting -Force

Step 8

Install the package on the target

Now we are ready to enable IIS services and deploy the application. We will create a PSCredential object and invoke the install:

$Creds = Get-Credential administrator
Install-BoxstarterPackage -ComputerName MyMachine -PackageName NugetServer -Credential $creds -Force

Step 9

Run the application

Now we should be able to browse to our Nuget server application at http://MyServer