Hosting Websharper/ASP.NET apps on Linux with Mono + nginx

F# + cheaper hosting = winning

One of the arguments often levelled against .NET web frameworks is that Windows hosting options are expensive compared to their Linux counterparts. Pricing aside, many people, myself included, also prefer the simplicity and flexibility of being able to quickly SSH into a box for administration rather than faffing around with remote desktop.

In the past Mono had something of a reputation for poor performance due to the primitive garbage collector. Mono 3.0 however ships with a new garbage collector called sgen which is much better. The Xamarin guys are doing a great job and it now seems ready for primetime.

Having recently been experimenting with Websharper, and being a big proponent of F#, I was keen to see if I could have the best of both worlds. Would it be possible, I wondered, to use Mono to host a Websharper app on Linux?

My initial attempts at installing Mono and F# proved somewhat fruitless because the mainstream Debian packages are hopelessly out of date. Fortunately some bright spark has uploaded some more recent ones onto launchpad which makes the process fairly straightforward.

Once that was done the rest was easy enough. I just copied a compiled Websharper site across from my Windows machine, fired up fastcgi-mono4, configured nginx to proxy the requests and hey presto, the page popped up! The same process should also work just fine for ASP.NET sites.

One small caveat: I did run into a websharper bug that was causing links to render incorrectly but that wasn’t too difficult to resolve.

Being a fan of automation (i.e incredibly lazy) I also created some vagrant provisioning scripts. This means you can be up and running with Ubuntu 13.04 64-bit Server hosting a Websharper site in minutes!

image

What the scripts do

  1. Download and install Mono 3.0.10 and F# 3.0
  2. Adds init.d script for fastcgi-mono4 (/etc/init.d/monoserve) – this also configures mono to use the new sgen garbage collector.
  3. Sets up nginx to point to fastcgi4-mono.
  4. Hosts the sample Websharper app in which is housed in /vagrant/www (this folder is shared between the guest VM and the host machine).

How to get started

  1. Install Virtualbox.
  2. Install Vagrant.
  3. Clone the provisioning scripts from my bitbucket account:
    git clone https://perfectshuffle@bitbucket.org/perfectshuffle/vagrant_raring_mono.git mono
  4. Launch the vagrant box:
    cd mono
    vagrant up
    
  5. Once everything has finished configuring it dumps out the boxes IP addresses to the console. Just point your browser to the eth1 IP address and you should see the site running!
  6. Replace the sample files in the
    /vagrant/www

    folder with your own website.

  7. Profit!

I’ve also tried running the scripts on some cloud hosting rather than inside vagrant and they work great.

Advertisements