Multiple IIS Virtual Servers on local machines

By Author : Steven Cohn

Posted on : http://www.developerfusion.co.uk/show/4645/

When Microsoft released Windows XP Pro they pushed it as the next development platform, superceding Windows 2000 Workstation. The reality, however, is that it is nothing more than a very slightly enhanced version of Windows XP Home edition. One of the major development features that didn’t make it from 2000 to XP was the ability to host multiple virtual Web servers on a single machine. Essentially, Microsoft disabled (hid) the menu item in the IIS Management Console that allows you create a new virtual server.

As a developer who builds and maintains multiple Web sites for friends, collegues, other (non-competitive) companies, this is extremely annoying! Although you can theoretically create a new Web site under a new virtual root (a child path under your default Web server) this presents a big problem: the new Web site would have to be aware of this path offset when referring to itself with absolute or relative paths. The advantage of having a new and independent virtual server is that it only recognizes its own virtual roots and paths.

The Secret

However, there is an alternative, albeit slightly cumbersome. The ability to create multiple virtual servers is only hidden, not stripped out completely.

While you cannot create a new virtual servers through the MMC, you can still use the administrative scripts that come with IIS, specifically, the adsutil.vbs script.

IIS virtual servers are defined in the IIS metabase as numbered entries under the W3SVC key. For example, the default Web site is named W3SVC/1; the second site created would be named W3SVC/2; and so on.

So, to create a second virtual server, open a command window and type:

C:InetpubAdminScripts> adsutil.vbs create_vserv W3SVC/2
C:InetpubAdminScripts> adsutil.vbs copy W3SVC/1 W3SVC/2

The first command creates a new virtual server in the IIS metabase.

The second command copies all the necessary meta data from your default Web site to the new Web site to make it work properly.

Rename It!

When you copy the meta data from an old site to a new site, the new site will inherit all of the old attributes, including the name. So you’ll want to immediately rename the new virtual server. Open MMC and find the new entry – they will be listed in the sequence in which they were created (W3SVC/1, W3SVC/2, ….).

You’ll also need to change other settings, such as the home directory. I typically like to create a new folder directly under the C:InetPub folder for each new virtual server.

The Caveat!

When you open the IIS MMC, you’ll notice a red icon next to the new virtual server and and error message in the comments column. This is OK. What this means is that inetinfo.exe attempted to start up this second virtual server.

The caveat in this whole thing is that, while you can create multiple virtual Web servers on a single Windows XP Pro machine, you can only run one at a time. Unfortunately, I (and apparently no one else) can find a way to work around this. Even applying a unique port number to each virtual server doesn’t seem to work.

Toggling

But it’s not so bad… All you have to do is open the IIS MMC, stop the currently running virtual server and start the one you want.

Of course, this means that when you switch between virtual servers, any active sessions on the virtual server you just shutdown will be lost. But the original intention was to be able to develop multiple Web sites, without having to worry about absolute or relative URL paths throughout the source code.

Deleting and Enumerating

You can also delete a virtual server that you previously created using this technique. The adsutil.vbs script includes a delete command:

C:InetpubAdminScripts> adsutil.vbs delete W3SVC/2

Keep in mind, after deleting a virtual server, that you can’t rely on the number of virtual servers shown in the IIS console to indicate the next available virtual server id number. For example, if you’ve created server 1, 2 and 3 and then delete server 2, the IIS console will only show two servers, but you cannot create another W3SVC/3 because that one already exists.

But you can enumerate the existing virtual servers using this adsutil.vbs syntax:

C:InetpubAdminScripts> adsutil.vbs enum w3svc /p

The /p qualifier indicates that you only want to view top level paths and not individual parameters. Otherwise, the output will be very long and difficult to understand.