Supersonic Man

October 20, 2011

IIS Express

Filed under: Hobbyism and Nerdry — Supersonic Man @ 12:02 am

An addendum about the little web app I made: how to serve local web pages on your home system with IIS Express. It’s much lighter weight than real IIS, and is meant to serve only a single user, so you don’t have to worry all that much about securing yourself against outside access to your system via browser.

IIS Express can be installed for free as a part of assorted Microsoft development tool suites. Since it’s intended as a development tool, it normally runs only intermittently. But if you want to have a web app ready and waiting on your desktop for everyday use, it needs to be running all the time. Unfortunately it’s a command-line app, and Windows isn’t very good at splitting off command-line processes into the background.

First, you have to get the web app installed into it. To do this, go to your My Documents directory and find a folder called IISExpress, and inside that a folder called config. In there is a file called applicationhost.config. It’s an XML file. Edit it with Notepad or another plain-text editor. Look for a section like this:

        <sites>
            <site name="WebSite1" id="1" serverAutoStart="true">
                <application path="/">
                    <virtualDirectory path="/" physicalPath="%IIS_SITES_HOME%\WebSite1" />
                </application>
                <bindings>
                    <binding protocol="http" bindingInformation=":8080:localhost" />
                </bindings>
            </site>

If you only want one web application, you can modify the <application> tag that’s there; all you have to do is change the physicalPath= attribute, replacing the quoted string %IIS_SITES_HOME%\WebSite1 with the real disk location of your web app folder, which in the case of my wee phonebook app is D:\Programming\Phonebook.

But you might want to support more than one app. You do that by adding a virtual directory to IIS. To do that, add a second <application> tag after the close of the first one. Pick an easy name for it — in my case, “Phonebook”. Use that name, with a slash in front, for the path= attribute in the <virtualDirectory> tag, and the physical disk location as before. So the whole section ends up looking (in the case of my example) like this:

        <sites>
            <site name="WebSite1" id="1" serverAutoStart="true">
                <application path="/">
                    <virtualDirectory path="/" physicalPath="%IIS_SITES_HOME%\WebSite1" />
                </application>
                <application path="/">
                    <virtualDirectory path="/Phonebook" physicalPath="D:\Programming\Phonebook" />
                </application>
                <bindings>
                    <binding protocol="http" bindingInformation=":8080:localhost" />
                </bindings>
            </site>

With this entry in place, you’ll access your app by putting your virtual directory name after localhost:8080/. So in my case, the url for accessing my app is http://localhost:8080/Phonebook.

To test this config file, you first have to find where your copy of iisexpress.exe is located. Usually it’s in C:\Program Files\IIS Express, or in 64 bit Windows, C:\Program Files (x86)\IIS Express. next, open a command prompt. Give the command cd “C:\Program Files\IIS Express” (or wherever it’s located), and then the command “iisexpress”. You shouldn’t need any arguments on that command.

Now IIS Express starts, but it doesn’t return your command prompt. Instead it gives status messages and then says Enter ‘Q’ to stop IIS Express. That is, if it started successfully. If there are any error messages in red, you might have to go back and fix something in the config file. If there are no errors and it starts, don’t hit the ‘Q’ key yet. Open your browser and put in your app’s URL. It should work. Now go back to the command prompt and hit the Q key. IIS Express will stop. Try the app in the browser again; it should no longer work.

Now all you need is a way to have IIS Express running without having a command prompt window hanging around. There are third-party command-line tools to do this job, but instead of downloading one of those and then having to take time to decide how much you trust it, you can get the same effect with the Windows Script Host… if you have that installed.

To do that, just make a little JScript file that reads like this:

function arg(n)
{
    var r = WScript.Arguments(n);       // yes, you use parens, not brackets
    return r.indexOf(" ") >= 0 ? '"' + r + '"' : r;
}

var command = arg(0);
for (var a = 1; a < WScript.Arguments.length; a++)
    command += " " + arg(a);
var wshShell = WScript.CreateObject("WScript.Shell");
wshShell.Run(command, 0, false);

This script is a general solution for the launch-in-background problem. I call it launch_in_background.js.

To apply it to IIS Express, first make a shortcut to the script — you can do this by dragging it to the desktop with Ctrl and Shift held down. Edit the Properties of the shortcut, and you should see its full path, such as C:\Windows\System32\launch_in_background.js (or wherever), in the dialog’s Target box. Edit this box so that before that path it says WScript, separated by a space, and after it, separated by another space, you have the full path to IIS Express, in quotes. So the complete text might be, for instance, WScript C:\Windows\System32\launch_in_background.js “C:\Program Files (x86)\IIS Express\iisexpress”.

Double-click this shortcut, and you should see a system tray icon appear, and you should be able to browse to your app again. Now all you have to do is drag the shortcut into your Start Menu’s Startup folder.

Advertisements

3 Comments »

  1. Alternatively, a much easier solution if running on vista or above. Create a bat file with the following:

    set IISEXPRESS=%programfiles(x86)%\IIS Express\iisexpress

    powershell start-process \”%IISEXPRESS%\” /site:BIG4.Membership.Service -windowstyle Hidden
    powershell start-process \”%IISEXPRESS%\” /site:BIG4.Loyalty.Service -windowstyle Hidden
    powershell start-process \”%IISEXPRESS%\” /site:BIG4.Store.Service -windowstyle Hidden
    powershell start-process \”%IISEXPRESS%\” /site:BIG4.Booking.Service -windowstyle Hidden
    powershell start-process \”%IISEXPRESS%\” /site:BIG4.Fulfilment.Service -windowstyle Hidden
    powershell start-process \”%IISEXPRESS%\” /site:BIG4.Logging.Service -windowstyle Hidden
    powershell start-process \”%IISEXPRESS%\” /site:BIG4.Messaging.Service -windowstyle Hidden

    Comment by Marc — April 3, 2013 @ 5:44 pm | Reply

  2. To be clear, the powershell start-process command is just a substitute for the little JScript file. Right?

    I don’t see why you need multiple start-process commands; IIS Express should be able to serve multiple sites just fine from one process. It’s only local use, after all.

    Comment by Supersonic Man — April 3, 2013 @ 7:04 pm | Reply

  3. Yes, that’s just a way of starting multiple IISExpress sites from command line without having to have the command window open waiting for the user to press Q. IIS Express will always start a new thread for each website, which you can observe in the process list in the task manager. Calling start-process in this instance doesn’t do anything different from just calling the ‘iisexpress’ command but simply allows each website thread to start without opening a command window.

    Comment by Marc — April 4, 2013 @ 3:17 pm | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: