Fastest way to create IIS7 websites, applications, and application pools

IIS7 provides quite a few ways to create websites, applications, and application pools.  The simplest way of course is to use the new GUI Administration tool, where your new website is always a few mouse-clicks away.

But, what if you need to create 10, 100, or even 10,000 websites?  Unless you own a farm of trained pigeons, using the GUI would certainly not be your first choice.  Fortunately, IIS7 comes with quite a few alternatives for automated configuration management, including:

·         AppCmd command line tool

·         Scriptable COM-based API

·         A WMI provider

·         The .NET Microsoft.Web.Administration namespace. 

All of these can be used to edit any IIS7 configuration, and most also provide simplified ways to create websites, applications, and application pools.

But, which one is the fastest way to get it done?

Both WMI, MWA, and scriptable COM have a pretty high upfront investment in developing the script/code to create the sites.  AppCmd allows you to create a website in one line at the command line, and from this perspective it’s very hard to beat.  This is the main reason why AppCmd is my weapon of choice in this situation.

More interestingly, it turns out that AppCmd can also provide the best performance for creating a large amount of websites on Windows Server 2008 when compared to the other options.  This should come as a surprise, especially when you consider that with some extra work you can create a WMI/Ahadmin COM script or .NET program using MWA that create all of the sites in a single run, as opposed to starting AppCmd a 1000 times.

Indeed, you won’t get this performance if you write a batch file to run a 1000 of AppCmd Add Site commands, due to the high cost of starting a process and the expensive one-time initialization performed inside AppCmd.

To get this performance, you need to take advantage of, drum roll, command pipelining (You can learn more about it my previous post, Do complex IIS management tasks easily with AppCmd command piping).

Using AppCmd to bulk create IIS7 websites and application pools

To recap quickly, AppCmd supports piping the output of the LIST command as XML to another AppCmd command, which can take that output as its input.  This allows multiple AppCmd commands to be chained together and essentially create a pipeline of related operations.

If we take a look at the output of %windir%system32inetsrvAppCmd List Sites /XML, we get the following:

<?xml version="1.0" encoding="UTF-8"?>
<appcmd>
    <SITE SITE.NAME="Default Web Site" SITE.ID="1" bindings="http/*:80:" state="Started" />
</appcmd>

Now, what would happen if we created an XML file containing our site definitions in this format, and then piped them to the AppCmd Add Site command? Voila, you just discovered what I call AppCmd bulk loading.

With this in mind, our process becomes:

1) Write a batch file that generates the sites.xml (or apppools.xml) file containing site (or application pool) definitions.  Here is an example:

gensitexml.bat:
@echo off
echo ^<appcmd^>
FOR /L %%I IN (0,1,%1) DO echo   ^<site name="Site%%I" bindings="http/*:80:site%%I" id="1%%I" physicalPath="%systemdrive%inetpubsite%%I" /^>
echo ^</appcmd^>

gensitexml.bat 1000 > sites.xml

2) Run the AppCmd Add Site command with the /IN switch with that file as input:

%windir%system32inetsrvAppCmd Add Sites /in < sites/xml

3) Watch our sites get created

In the case where you cannot use the AppCmd Add Site command’s short form for creating the site/root app/root vdir in a single command (by specifying the /physicalPath parameter), you can create the site, application, and virtual directory in 3 separate bulk operations.  You may need to do this if you need to create non-root apps/virtual directories for the site, or set application or virtual directory settings like the app’s applicationPool.  Just look at the output of AppCmd List Sites /XML, AppCmd List Apps /XML, and AppCmd List Vdirs /XML, create your bulk files as above, and feed them to AppCmd Add Site, AppCmd Add App, and AppCmd Add Vdir commands.

To learn more about creating websites, applications, virtual directories, and application pools with AppCmd, be sure to read my previous post: Creating IIS7 sites, applications, and virtual directories.

A final note: Windows Server contains a lot of performance improvements across the entire configuration stack, including many improvements in AppCmd that truly make bulk loading fly.  You will not get this on your Windows Vista machine, unless you get the Vista SP1 Beta.  However, there is one thing you can do to help AppCmd’s performance on Vista when bulk creating websites – be sure to stop WAS during the operation:

Iisreset /stop
// create the sites
iisreset /start

This speed up site creation multiple times, due to a last minute fix I snuck in that turns off website state retrieval during site creation.  This won’t get you anywhere to server or Vista SP1 performance, but it will help out a bit.

Let me know if you have questions about bulk loading, or command piping, as those are both very powerful ways that AppCmd provides to help you manage IIS7. 

There are plenty more things you can do with both, and I am looking forward to seeing them come up on the net as people discover the tool J

 

21 Comments

  1. Anonymous

    Hey Mike! While I have not played with it yet, I really like what I see with AppCmd. We’re getting ready to start on a large web project that will be running under IIS 7 on Windows 2008.

    In the past we’ve written a custom tool to push out our deployment. I’m thinking we can avoid using that now and give AppCmd a try.

    My question is about mass deployment though. Let’s say we have an application that needs to be deployed to 10 servers (or 100 for that matter) running IIS. Do you have any recommendations for the best way to execute our batch file (which will use AppCmd) on all 10 machines in parallel and to monitor output? We’ve looked at a number of vendor tools, but have not found one we really like.

  2. Anonymous

    When I delete using APPCMD it leaves remnants of the website in IIS7 running a shared configuration. Is there something else I need to do?

    THE COMMAND:
    APPCMD delete site “JasonTest083007”

    THIS IS WHAT’S LEFT IN THE applicationHost.config FILE:





  3. Anonymous

    @Mac, you should probably use the Shared configuration capability of IIS7 for deploying apps to your 10 (or 100) servers. Your batch file would just focus on pushing your code to a file share and you can have IIS in all 10 servers pointed at the share.
    There’s documentation available on iis.net.

  4. Anonymous

    Hi

    How Can I get last Site id so that when we run script it autoincrement site id ?

    I tried old ii6 way but while checking it creates empty sites whihc are deleted.

    say i have 10 sites & i deleted 3 & 4

    so when it check it also creates sites having 3 & 4 with empty properties & then it create new site having id 11 with the properties given

    thanks

  5. Anonymous

    Very Confused
    i need to find an application to create a website ( frontpage, Sharempoint/) so what is the application i install to IIS7 i don’t need an app because it’s there. or is this a different app. so confusing.
    can you better explain this???
    can i use Sharepoint designer to create a website like i did with frontpage? what’s the app your talking about?
    sooooo stressed out. i’m 9 hours into not understanding this

  6. Anonymous

    Mike, Great info, thanks for sharing.

    I’m trying to add multiple host headers (http, unsigned, port 80) to a single website using AppCmd and am a little confused by the syntax..

    i see that it is supposed to be something like..

    %systemroot%system32inetsrvAPPCMD set site “Default Web Site” -bindings.[protocol=’string’,bindingInformation=’string’].protocol

    or

    %systemroot%system32inetsrvAPPCMD set site “Default Web Site” -bindings.[protocol=’string’,bindingInformation=’string’].bindingInformation

    Problem is that i’m a newbie at this and i’m am totally unsure how to properly configure the statement. I’m one of those types that learns from examples and there just doesnt seem to be much out there on this stuff.

  7. rotts

    Mike,

    I want to bulk create websites with xml as you show in this article. How do you supply username/password credentials for each site to access a network drive for content when creating sites in bulk ?

  8. Siwanka De Silva

    Hi Mike,

    Highly appeciate your comment on this. We have a MVC3 web application. We are creating programmatically IIS binding Within our application we create an account and need to create a binding for that account while we create it. It;s a wizrd based and and in the first page we crate the binding and moving to the next wizard page. When moving to the next will redirect to the Login because Session variables are null. Created a App pool as Integrated. This is not happen when it”s Classic. Do you know any reason why it”s setting Session values to null. Probably this due to recycling the App ppol. not sure.. I saw when debugging it”s attaching DefaultApppool to the site. But attahced Apppool was earlier different one. Thank you,

    Siwanka De Silva

Leave a Reply

Your email address will not be published. Required fields are marked *