Sunday, August 21, 2016

Setting up IIS

Summary 

It's been a while since my last blog post.  My summer has been filled with hiking and some travel (involving more hiking).  One subject I'd like to blog about is IIS and how to set it up for development purposes.  I've posted information about this before (see here and here), but this time I'm going to show how to use powershell to do the dirty work for you.


IIS

First of all, I'm going to assume you have IIS installed on your PC.  You can install IIS by going to your programs and features control panel and choose "Turn Windows Features On or Off".  Then search for Internet Information Services and turn on the World Wide Web Services section.  Here's an example for Windows 7:



Once IIS is installed, you can setup any number of servers for testing purposes.  To do this, you'll need to know how to setup an entry in the hosts file.  The hosts file will allow you to use multiple url's on your PC and IIS will capture each url you define for the IIS server that it is bound to.

First, the hosts file.  Navigate to your hosts file:

C:\Windows\System32\drivers\etc

Edit the hosts file with a text editor.  A blank hosts file will look like this:



When you decide to add an entry, make sure you make up a url that is unique.  If you use something like www.google.com then you will no longer be able to get to google (until you remove the entry from this file).

So make an entry like this:

127.0.0.1    www.mytestwebsite.com

Next, let's create a directory where the website files will reside after we create a website.  The c:\inetpub subdirectory is already setup with the correct permissions for IIS (more on this later).  So we'll just create a directory that matches the url name above (so we can find it quickly):

C:\inetpub\mytestwebsite

Now open your IIS console.  Just type "IIS" into your search for programs and files box and select "Internet Information Services (IIS) Manager".  I usually pin the icon to my task bar, since I use this console a lot.  Here's an example of my console.  Obviously, my PC name is "DECAIREPC" and this is the default console that you'll see the first time you use it:



The only reason this console is default is because I have a new PC with a new hard drive and I have not setup any servers on it yet.  Normally, my "Sites" are full of test sites that I use for various projects.  I'm going to show how to setup one site to match the url entered in the hosts file.  Right-click on "Sites" and "Add Web Site...":



Notice how I named the "Site Name" the same as the url.  The site name is what shows in the "Sites" tree-view:



I just match it up so it's easy to find.  The physical path is set to the directory that we created earlier.  That directory will contain files for the website.  If you're writing a C# MVC application, you'll point that directory to your project directory so you can modify your C# with Visual Studio, but still see it on a website with the URL you created.

You can select the application pool for this app.  In fact, you can share one app pool with multiple servers.  The app pool caches files from your website, so if you want to make sure your site is reset after a change, you'll need to cycle the app pool itself.  

Make sure your host name is set to the url that is in the hosts file.

Let's do a dumb and dirty test.  Navigate to the directory you created above (c:\inetpub\mytestwebsite).  Create a home.html text file and type this inside it:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
Test website
</body>
</html>


Save it and then open a browser.  Type in your url and the page name created above:

www.mytestwebsite.com/home.html

You should see this:



Now, there is the possibility that you'll run into directory permissions problems.  IIS must have permissions to the directory where the files will reside.  You can add permissions to the directory for IIS_IUSRS and IUSR.


Setting up IIS with Powershell

As you can imagine, this task can become tedious, especially if you need to setup multiple servers with the same settings.  This is where a script can come in handy.  Let's do some experimenting.  Open powershell in administrator mode.  Then make sure you can execute scripts in powershell:

set-executionpolicy remotesigned

Answer "[Y] Yes" to allow.  Then you can use WebAdministration module to manipulate IIS.  Go to the IIS app pools using the following command:

cd IIS:\AppPools\

You can use the "dir" command to list the app pools that exist in IIS.  You should see something like this:



Compare with the app pools in the IIS control panel:




Next, you can look at the websites by using the same "cd" (change directory) instruction to go to the "Sites" directory:

cd IIS:\Sites\

Then use the "dir" command to list these:



The "e.com" might be confusing.  It's the wrap-around for the first column where "www.mytestwebsite.com" wraps.

First, let's cleanup IIS.  Delete the website that we created earlier using the IIS console.  Then go to "Application Pools" and delete the app pool that matches.  Then copy this script into a "createwebsite.ps1" text file.  From powershell, go to the directory where you created the ps1 file and type:

.\createwebsite.ps1

Now copy and paste this script (which came from this blog: http://geekswithblogs.net/QuandaryPhase/archive/2013/02/24/create-iis-app-pool-and-site-with-windows-powershell.aspx):

$iisAppPoolName = "www.mytestwebsite.com"
$iisAppPoolDotNetVersion = "v4.0"
$iisAppName = "www.mytestwebsite.com"
$directoryPath = "C:\inetpub\mytestwebsite"

#navigate to the app pools dir
cd IIS:\AppPools\

#check if the app pool exists
if (!(Test-Path $iisAppPoolName -pathType container))
{
    #create the app pool
    $appPool = New-Item $iisAppPoolName
    $appPool | Set-ItemProperty -Name "managedRuntimeVersion" -Value $iisAppPoolDotNetVersion
}

#navigate to the sites dir
cd IIS:\Sites\

#check if the site exists
if (Test-Path $iisAppName -pathType container)
{
    return
}

#create the site
$iisApp = New-Item $iisAppName -bindings @{protocol="http";bindingInformation=":80:" + $iisAppName} -physicalPath $directoryPath
$iisApp | Set-ItemProperty -Name "applicationPool" -Value $iisAppPoolName

c:


I removed the WebAdministration import module command at the top because my computer complained that it was already installed.  If you run into errors running this script you can add it back to the top like the geeks with blogs link shows.

When you run the script an app pool will be created along with a matching website.  The parameters are set at the top, making it easy to setup any website.

What this script does not setup is the directory permissions or installation of IIS.  If you need to setup a powershell script to do these functions (say for the purpose of installing on a new machine), you'll have to add commands at the beginning of the script to do this.

You can also parameterize the variables at the top of the page to make this into a powershell script that can be called from another script.  The purpose could be to setup multiple websites on a machine.  This is typical in production systems or even in development systems were an IIS server is stood up or torn down as needed.

For more information on scripting IIS functions you can go to this website:

http://www.tomsitpro.com/articles/powershell-manage-iis-websites,2-994.html