Categories
Dev Ops Dev Tools

Stupid Easy Scripts to Run to Setup a Website in IIS

Why use batch scripts to setup IIS?

I’m weird.  I like my development, test, and production environments to be setup as close to the same as possible.  It isn’t exactly rocket science but it helps eliminate errors that always tend to creep up on deployment day and are always unexpected.  I’ve had instances where deployments were rolled back because a site wasn’t tested with an SSL certificate on test or in a load balanced environment, someone missed a key IIS setting that caused half of all requests to fail.

So now whenever I am working on a new site, I like to create a batch script does all of this creation for me and I can run it on multiple environments will little changes.

Where?

I’ve gather much of these scripts from various sources on StackOverflow , MSDN documentation, and from several co-workers over the years.  This is just the full collection I’ve done up until now.

How?

I’m going to break up each different script into small parts so it is easier to find a script that you could be looking for.

Setup Your Variables

set app_pool_name=@@ App Pool Name @@
set site_name=@@ Web Site Name @@
set host_name=@@ Host Name @@
set physical_path_web=@@ Server File Path @@
set ip_address=*
set user_name=@@ User Name %%
set user_password=@@ User Password@@

Create User

NET USER %user_name% %user_name% /ADD

 

Create App Pool

call %systemroot%\system32\inetsrv\appcmd add apppool /name:%app_pool_name%

Set App Pool to use .NET 4.0

call %systemroot%\system32\inetsrv\appcmd set apppool /apppool.name:%app_pool_name% /managedRuntimeVersion:v4.0

Assign User To App Pool

call %systemroot%\system32\inetsrv\appcmd set config /section:applicationPools “/[name=’%app_pool_name%’].processModel.identityType:SpecificUser” “/[name=’%app_pool_name%’].processModel.userName:%user_name%” “/[name=’%app_pool_name%’].processModel.password:%user_password%”

Turn of App Pool User Profile

call %systemroot%\system32\inetsrv\appcmd set config -section:applicationPools “/[name=’%app_pool_name%’].processModel.loadUserProfile:false”

Log All the Things

call %systemroot%\system32\inetsrv\appcmd set config /section:applicationPools “/[name=’%app_pool_name%’].recycling.logEventOnRecycle:OnDemand,ConfigChange,IsapiUnhealthy,Memory,PrivateMemory,Requests,Schedule,Time”

Setup App Pool Restarts

call %systemroot%\system32\inetsrv\appcmd set apppool /apppool.name:%app_pool_name% /+recycling.periodicRestart.schedule.[value=’12:00:00′]

call %systemroot%\system32\inetsrv\appcmd set apppool /apppool.name:%app_pool_name% /recycling.periodicRestart.time:00:00:00

call %systemroot%\system32\inetsrv\appcmd set config /section:applicationPools /[name=’%app_pool_name%’].processModel.idleTimeout:0.00:60:00

Create Website folders and grant user permissions

if not exist “%physical_path_web%” mkdir “%physical_path_web%”

icacls “%physical_path_web%” /q /c /t /grant %user_name%:(OI)(CI)(RC,RD,RX)

Setup Website Bindings

call %systemroot%\system32\inetsrv\appcmd add site /name:%site_name% /bindings:http/%ip_address%:80:%host_name% /physicalPath:”%physical_path_web%”

call %systemroot%\system32\inetsrv\appcmd set site /site.name:”%site_name%” /+bindings.[protocol=’https’,bindingInformation=’%ip_address%:443:%host_name%’]

call %systemroot%\system32\inetsrv\appcmd set site /site.name:%site_name% /[path=’/’].applicationPool:%app_pool_name%

Conclusion

I run these on almost every project I create to make easy, reusable infrastructure components that make server setup a breeze.  Hopefully, these will help you too.  Do you have any batch scripts commands that you use to setup IIS?  Let me know, and I can add them to this list with credit to you!

 

By Brett The Whitt

Brett is a web development consultant and has been working for more than 12 years. He specializes in hybrid mobile app development and using front end frameworks such as AngularJS. Always passionate about continuous education, he has been most recently mentoring and encouraging his co-workers to become better speakers. To further this growth, he leads Lightning Talk Nights and other events at his job at HMB Inc

One reply on “Stupid Easy Scripts to Run to Setup a Website in IIS”

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 )

Facebook photo

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

Connecting to %s