Setting up your own WebMatrix server

Some of you may have tried WebMatrix, the PHP and ASP.NET development tool released by Microsoft in January 2011. The first version of this app is quite nice, with an easy way for you to build simple ASP.NET sites using the Razor syntax or even CMS-like solutions by starting from apps like Joomla! or Wordpress. If you haven’t tried WebMatrix, check it out, it’s a quick download.

So now you have a web site built in WebMatrix – where can you put it? There are a couple of good options:

  1. Preview offers on iis.net. These offers are free, but they are time-limited. Eventually you will have to start paying for them. The upside is that they use some great cloud-scale technology to make your web site automatically scalable.
  2. Web Hosting Gallery. This is a listing of curated Windows hosting offerings maintained by Microsoft. The quality of hosters ranges from decent to great. A ratings system and price filters can help you find the offer you want. My blog, for example, is hosted by AppliedI.
  3. Linux hosters are not an option if you want to host ASP.NET web sites, but they are fine choices if you want to host sites like Wordpress. One downside is that some of the ‘light up’ features in Web Deploy (such as database deployment) won’t work with Linux hosters because Web Deploy is not supported Linux.
  4. Your own VPS on a cloud hoster such as Amazon

This blog post, clearly, is about the last option Smile Note that this option is NOT for everyone. For one, a VPS is almost certainly less economical for low-traffic sites (such as my blog) compared to shared hosting. This is common sense – hosters often pack thousands of sites on a single server, whereas a VPS just runs your site. For example, a Small Windows instance on Amazon EC2 will run you approximately $86 / month. A micro instance on Amazon EC2 is free, but it does not support Windows. Compare this to the $9.99 / mo. I pay to AppliedI and you can see why shared hosting is still so popular.

There are, however, some obvious benefits to running your own VPS:

  • You’re the boss (mostly). I say mostly because VPS are often just VMs and you have to take Amazon (or Rackspace’s) word that your VM is being given a fair slice of processor and RAM. That said, you can install pretty much any software you want on the VPS.
  • You can run background jobs. Not all shared hosters let you run background jobs, cron jobs or Windows services.

Okay, on to setting up your own WebMatrix server. Let me save you some time – if you are interested in setting up your own WebMatrix hosting server on Amazon EC2, Microsoft has already provided a pre-configured AMI – pass Go, collect $200 Smile

image

Step 1: Get a VM, open up the ports

Get a VM from Amazon or Rackspace. You should use a Windows Server 2008 or higher operating system – server is the operative keyword here, I do not recommend hosting production sites on a client OS such as Windows 7. This is a fairly standard process, and I won’t repeat it here. One important note: make sure port 8172 is open for inbound traffic. This is the port Web Deploy uses for communicating with your server (well, more accurately, the Web Management Service uses, which Web Deploy piggy-backs on for AuthN and AuthZ). Also, don’t forget to open up port 80.

Step 2: Install the web stack

Years ago, this step would have been painfully … well, painful. This is the part where we install IIS, ASP.NET, PHP, MySQL, SQL Server, etc. Thankfully, this all collapses to a single clickable launcher (yes, a single click …). Go ahead, click here. Now let Microsoft Web Platform Installer install the hosting software stack for you. The server may reboot a couple of times, but eventually you will end up with a rad publishing server.

Step 3: Set up a non-admin publishing user account

At this point, you can publish to the server from WebMatrix using administrator credentials. Personally, I don’t like using admin credentials for this sort of thing. So, let’s create a non-admin user account and set up things so we can publish to Default Web Site with it. In this process, we will also create a Publish Setting File which will contain all the settings we need to publish to Default Web Site from WebMatrix:

  1. Create a directory: c:profiles. We will save our Publish Settings File here.
  2. Start PowerShell as administrator
  3. Navigate to %program files%IISMicrosoft Web Deploy v2Scripts
  4. Run this command (substitute UserA with a username of your choice):

.SetupSiteForPublish.ps1 –siteName “Default Web Site” -deploymentUserName UserA -publishSettingSavePath C:profiles -publishSettingFileName UserA.PublishSettings

image

Step 4: Set up databases

Unless you are writing a very boring application, you will need to set up a database or two. If your application uses SQL Compact, skip ahead to the next section. SQL Compact binaries and the actual database file itself (with a .SDF extension) are automatically deployed to the server as part of your application’s code. No need to install a database engine.

#

4.1. Setting up a SQL Server database

If you are building a SQL Server application, we already installed SQL Server 2008 Express and SQL Management Studio 2008 Express in Step 2. Now you have to create a database and a non-administrator database user so you can deploy the application with non-administrator database credentials in its config files. For example, let’s say you are deploying Umbraco. You can certainly deploy Umbraco as ‘sa’ or ‘root’, but then your admin credentials are stored in web.config. If, for any reason, your web.config is compromised, an attacker now has your database admin credentials! We don’t want this!

  1. Start PowerShell as administrator
  2. Navigate to %program files%IISMicrosoft Web Deploy v2Script
  3. Run this command (substitute UserA with a username of your choice):

.CreateSqlDatabase.ps1 -databaseName UserADb -databaseUserName UserADbUser -databaseUserPassword Passw0rdA -databaseAdminUser sa -databaseAdminPassword Passw0rd -sqlServerInstanceName .SQLExpress -publishSettingSavePath C:profiles -publishSettingFileName UserA.PublishSettings

image

4.2. Setting up a MySQL Server database

Run this command instead:

.CreateMySqlDatabase.ps1 -databaseName UserADb -databaseUsername UserADbUser -databaseUserPassword Passw0rd -databaseAdminUser root -databaseAdminPassword iis6!dfu -publishSettingSavePath C:profiles -publishSettingFileName UserA.PublishSettings

image

Step 5: Do a test publish

Now we are ready to do a test publish from a computer running WebMatrix.

  1. Grab the settings file from c:profiles and copy it to the computer running WebMatrix.
  2. Create a new web site (preferably with a SQL Server or MySQL database) and click Publish
  3. Click “Import publishing settings” and import the settings file
  4. This should work Smile

Common errors you might see:

  • Connection timed out. Are you sure port 8172 is open? Can you see a certificate prompt if you type https://servername:8172/msdeploy.axd?Site=Default%20Web%20Site in your browser?
  • Invalid username / password: Are you sure you did Step 3 correctly? What happens if you enter administrator username and password instead?
  • Could not connect to database: Are you sure you did Step 4 correctly? What happens if you enter administrator username and password instead?
  • Missing components in Publish Compatibility dialog. You may be missing specialized components needed by some apps – just use Web Platform Installer to installed them.

Need help? Drop me a line at baslam at microsoft dot com. Good luck!