Thursday, June 30, 2016

Deploying a ASP.NET MVC Application on Mono

Ever tried to deploy an ASP.NET MVC application before? Well, it's the bee's knees... on Windows, where most Visual Studio project types are first class citizens of the ecosystem (sorry, F#). However, things get a tad trickier on Linux / Mono, so let's run through the very basics of setting up an environment like this.

This post will not cover advanced topics, such as setting up an SSL certificate or linking with a database. It will just get the barebones ASP.NET server deployed. Note, however, that there are better ways to deploy an ASP.NET MVC service than what this post describes (through fastcgi or mod_mono), so if you're concerned about the RIGHT way of doing things rather than the FAST way of doing things, follow that link.

Structure - Skip if you have a project
Let's make ourselves an ASP.NET MVC application in Monodevelop. If you don't already have monodevelop, you'll need to install the following packages:

  • monodevelop
  • xsp
  • mono-devel
from your distribution's repository. If some of those aren't found, it may be okay, as both tend to be bundled with monodevelop itself. Now, make an ASP.NET MVC project with Razor

this should create an ASP.NET MVC project that's ready for deploying.

Now, it's time to compile the project. This is where a program from mono-devel comes into play. xbuild, an implementation of msbuild (link)  is what will generate what the web server runs. The binaries are generated from the .sln or .csproj file itself.

So, open up a terminal and go to the project root, where your project file is.
Then issue the "xbuild" command on your file. This will build all targets for the project, which are Debug and Release by default in Monodevelop

xbuild {file.sln}

Wonderful! If everything in your project passes, you have successfully built your ASP.NET MVC application

Now, here are where the tough decisions are made. If you are truly deploying a production service, you should do the following things:

  • Secure a webserver, preferably a dedicated virtual machine
  • Generate SSL certificates / other cryptographic keys
  • Create a user for running your service
  • Install the fastcgi-mono or mod_mono (apache) modules and run your service from Apache

However, the quick-n'-dirty way I do it is with xsp4. xsp is yet another pet project of Mono's, that aims to be a fast ASP.NET web server for development. However, there is nothing that says you can't use it in production. It allows SSL certificates, variable ports, etc. so you won't find yourself missing too many features from Apache.

Running xsp4 is dead simple. Go to the build directory (the one under your project root) and run "

xsp4 --port 80"

if all goes well with this, you should have a full-fledged ASP.NET MVC 4 application deployed under Linux, and available on the standard http port. This is, of course, the unsecure, bare-bones deployment with xsp4. But it's a good starting point. To find out how to make xsp4 handle errors, set up your certificates, etc. issue the command 

xsp4 --help

Hope you found this useful!

1 comment:

  1. BlueHost is the best website hosting company for any hosting services you might require.