Web development environment scripting

Sometimes getting a web project running on your development machine requires a lot more work than simply doing a

git clone

Inspired by my colleagues Anders Janmyr, who wrote the book (or at least a great blog post) on being a responsible developer, and with the help of Morten Faester, who has done a tremendous job on scripting the development environment on a recent project, this blog post will describe how to script a complete development environment for a .NET web project from the command line.

The following steps are described:

  • Prerequisites
    – Installing via web platform installer and DISM
  • IIS
    – IIS app pool
    – IIS website
  • SSL certificate
    – import SSL certificate
  • hosts file
    – Add entry in hosts file
  • SQL Server
    – Create database
    – Setup access rights for the IIS app pool

Most of these commands are pretty self-explanatory, I will simply show the commands needed to setup  an environment for a project called “scriptingdemo”, running locally on the url http(s)://scriptingdemo/ with access to a sql server database also called scriptingdemo.

The install scripts need to be run from a command prompt with administrative privileges.

Install prerequisites

It might be necessary to ensure certain windows components are installed (such as IIS management console). Other dependencies, that are not windows components can be installed via Web Platform Installer (which you need to download and install separately).

Installing windows components can be done with the following command

DISM /ONLINE /Enable-Feature /FeatureName:Feature1 /FeatureName:Feature2 ...

A complete list of features can be retrieved with:

dism /Online /Get-Features

For this project we would like to ensure IIS and its components are installed.

DISM /ONLINE /Enable-Feature /FeatureName:IIS-WebServerRole /FeatureName:IIS-WebServerManagementTools /FeatureName:IIS-ApplicationDevelopment /FeatureName:IIS-NetFxExtensibility /FeatureName:IIS-ManagementScriptingTools /FeatureName:IIS-ISAPIExtensions /FeatureName:IIS-DirectoryBrowsing /FeatureName:IIS-ASPNET /FeatureName:IIS-ManagementConsole

 

You can script installation of software via web platform installer like this:

WebPICmd /Install /Products:<productid>

Where <productid> indicates what you want to install. You can get a list of already installed products using the command

webpicmd /List /ListOption:Installed

Or a list of products available for installation

webpicmd /List /ListOption:Available

For this project we would like to make sure we have SQL Express with tools and .NET 4.5 installed.

WebPICmd /Install /Products:SQLExpressTools
WebPICmd /Install /Products:ASPNET45

IIS

This script will create a website in IIS with its own app pool:

echo Creating directories for IIS installation
if not exist C:deploy mkdir C:deploy
if not exist C:deployscriptingdemo mkdir C:deployscriptingdemo

set appcmd=C:windowssystem32inetsrvappcmd.exe

echo Creating app pool
%appcmd% add apppool /name:"scriptingdemo" /managedRuntimeVersion:"v4.0" /managedPipelineMode:Integrated

echo Creating IIS website
%appcmd% add site /site.name:"scriptingdemo" /bindings:"http://scriptingdemo:80,https://scriptingdemo:443" /physicalPath:"C:deployscriptingdemo"
%appcmd% set site /site.name:"scriptingdemo" /[path='/'].applicationPool:"scriptingdemo"

SSL certificate

Our local site is set up to run both http and https. To establish a secure connection via https, we a self-signed ssl-certificate for the url https://scriptingdemo/. Generating this certificate is only done once and then the certificate is distributed in the source code along with the setup script files. I’m showing the command here anyway:

makecert -r -pe -n "CN=scriptingdemo" -b 01/01/2011 -e 01/01/2025 -eku 1.3.6.1.5.5.7.3.1 -ss my -sr localMachine -sky exchange -sp "Microsoft RSA SChannel Cryptographic Provider" -sy 12

The -n option is where you specify the hostname for the certificate.

Once we have the certificate, you can export it from your certificate store:

  • Start mmc.exe
  • File => Add/Remove Snap-in
  • Add a certificate snap-in for local computer
  • Go to “Certificates (local computer)” => Personal => Certificates and right click on the scriptingdemo certificate
  • All tasks => export (export the certificate to a file called scriptingdemo.pfx
  • Take the exported certificate and put it next to the setup script files.

At this point you might want to remove the certificate from you certificate store, so you can verify if the script adding the certificate works.

The next step is to import the certificate (-p option is the password you provided when the certificate was exported) and finally we bind the imported certificate to the site running on https://scriptingdemo

echo Import SSL certificate
certutil -importPFX -p test1234 -f scriptingdemo.pfx

echo Bind the certificate to the site
netsh http add sslcert hostnameport=scriptingdemo:443 certhash=a56f6521f0c57624f896a459e27c34b408276f7a certstorename=MY appid={5a599f4f-02dc-4120-864
6-74fcbc5b4827}

The parameter certhash identifies the certificate and can be found if you open the certificate from the certificate snap-in and look for Thumbprint under the details tab. The parameter appid is just any unique guid.

SQL Server

This script will run the content of a sql-script file against the local sqlexpress instance.

set sqlcmd=SQLCMD
set server=.sqlexpress
set credentials=-E
set exec=%sqlcmd% -S %server% %credentials% -i

echo Creating database
%exec% makedb.sql

This is the content of the file makedb.sql, which will create a sql server database for our website and set the AppPool running the site as db_owner.

-- Create database
IF NOT EXISTS(SELECT * FROM sys.sysdatabases WHERE name = 'scriptingdemo')
BEGIN
CREATE DATABASE scriptingdemo
END

-- Create login for IIS web application
CREATE LOGIN [IIS APPPOOLscriptingdemo] FROM WINDOWS

-- Set IIS web application as db_owner
use scriptingdemo
exec sp_addrolemember N'db_owner', N'IIS APPPOOLscriptingdemo'

If your web application does  not automatically bootstrap itself on startup, you need to run the sql script needed to setup the data as well.

 hosts file

This script will insert an entry in the hosts file, pointing the url http(s)://scriptingdemo/ to the localhost

echo Configuring host file
SET hostfile=C:windowssystem32driversetchosts
FIND "scriptingdemo" %hostfile%
IF ERRORLEVEL 1 ECHO 127.0.0.1 scriptingdemo >>%hostfile%

 

This Post Has 3 Comments

  1. Sakib

    It’s a good technique as well as its help web designer.

  2. I do web developing in php and some times i found it difficult to overcome the errors in my code. the way you have explained here about the scripting in web developing is very authentic and useful to all programmers. I really had a good time in visiting your site. I have tried your code it really works well.

Leave a Reply