Recipe 12.3. Creating Web SitesProblemYou want to create a web site. SolutionUsing a graphical user interfaceTo create a new web site from scratch using a wizard, do the following:
To create a new web site using a previously saved configuration file, do the following:
Using a command-line interfaceThe following command creates a new web site named Human Resources on server with IP 216.44.65.8 and root directory D:\HR: > iisweb /create D:\HR "Human Resources" /i 216.44.65.8 The following command creates a new site named My Company with root directory D:\Corp and IP address "All Unassigned," effectively making My Company the new default web site on the server: > iisweb /create D:\Corp "My Company" The following command creates a site on a standalone server named web04 using local credentials for that computer and leaving the site in a stopped state: > iisweb /create D:\Finance "Accounting Department" /i 216.44.65.8 /dontstart /s web01 /u web04\Administrator /p <password> The following command creates a site by importing a previously saved password-protected site configuration file named hr.xml: > iiscnfg /import /f D:\hr.xml /d <password> /sp /lm/w3svc/1525757177 /dp /lm/w3svc/2 /child The ID number 1525757177 for this site can be found by opening the XML file in Notepad and examining the Location attribute of the IisWebServer tag. For example: <IIsWebServer Location ="/LM/W3SVC/1525757177" AuthFlags="0" ServerAutoStart="TRUE" ServerBindings="172.16.12.50:80:" ServerComment="Human Resources" > </IIsWebServer>
Using VBScript' This code creates a web site. ' ------ SCRIPT CONFIGURATION ------ strComputer = "<ServerName>" ' computer to connect to strSiteName = "<SiteName>" ' web site description strRootDir = "<DirPath>" ' root directory for the web site ' The following parameters are optional ' strPort = "<PortNumber>" ' port for the web site ' strIP = "<IPAddress>" ' IP address used for the site ' strHostHeader = "<HostName>" ' host header name for the site ' strSiteID = 1234 ' site ID (default is to auto-generate) ' ------ END CONFIGURATION --------- set objIIS = GetObject("IIS://" & strComputer & "/W3SVC" ) objServerBindings = Array(0) objServerBindings(0) = strIP & ":" & strPort & ":" & strHostHeader strNewSiteID = objIIS.CreateNewSite(strSiteName, objServerBindings, _ strRootDir, strSiteID) WScript.Echo "Successfully created web site " & strSiteName & _ " with ID " & strNewSiteID DiscussionIf you leave the IP address for your new site as All Unassigned, your new site will be the default web site for your computer, which is the web site the server returns when a browser tries to access an IP address not currently assigned to another site. For example, if a computer has three IP addresses 172.16.12.50, 172.16.12.51, and 172.16.12.52 and only the first address has been assigned to a site, then opening the URLs http://172.16.12.51 or http://172.16.12.52 will return the default web site. It's a good idea to have a default web site configured with general contact information about your company on a server that will be hosting many sites. Note that if there is already a web site that has All Unassigned for its IP address (such as the Default Web Site created when IIS is installed) then if you assign All Unassigned to another site you won't be able to start that site. Host headers are a feature of the HTTP/1.1 specification and allow IIS to host multiple web sites that have the same IP address and port number but different DNS identities. You can't use host headers for sites that use SSL, however, and to use host headers you must have DNS name resolution working on your network. Also, don't assign any host header names to the Default Web Site. One good side of host headers is that when you have thousands of web sites hosted on a single IIS computer, using host headers to identify them incurs a smaller performance hit than using individual IP addresses. Using VBScriptThe one tricky thing about this code is setting up the ServerBindings array. For whatever reason, instead of making the web site IP address, port, and host header part of the parameters to the CreateNewSite method, they must be concatenated together in an array element and separated by a colon. See AlsoRecipe 12.4, Recipe 12.17, MS KB 304187 (IIS: Home Directory Cannot Point to Mapped Drives), and MS KB 816568 (HOW TO: Manage Web Sites and Web Virtual Directories by Using Command-Line Scripts in IIS 6.0) |