Get MVC3 Razor Running on Mono

Updated 10-18-2011 - added some more common issues

I dug around a little to figure this out. Hopefully this will clarify all the steps required to get an MVC 3 Razor site running on Mono. The main pitfall I ran into was gathering the additional dependencies and excluding Microsoft.Web.Infrastructure assembly from my deployment. Overall though it’s not too difficult to deploy your MVC3 app.

In Summary:

  1. Install Mono
  2. BIN Deploy Website
  3. Configure Apache (or Xsp environment)
  4. Fire Up Your Server
  5. Errors You Might See

Install Mono

If you haven’t done this yet, go grab it from the Mono downloads page. You need to run at least Mono 2.10, preferably the latest release (2.10.2). For Ubuntu you can use the badgerports; or for Ubuntu, compile from source using a bash script I created. (Skip the MonoDevelop install).

 

BIN Deploy Your Website

For this, I just performed a traditional “Publish” from Visual Studio to a file system directory. Be sure to set these references to “copy local.”

  • System.Web.Mvc
  • System.Web.Helpers
  • System.Web.Routing

 

BIN Deploy Additional MVC3/Razor Dependencies

Just like Scott Hanselman mentions in his BIN deployment post, add these additional libraries found in the MVC3 RTM. On Windows, they install to C:\Program Files\Asp.Net.

  • System.Web.Razor
  • System.Web.WebPages
  • System.Web.WebPages.Razor
  • System.Web.WebPages.Deployment

 

NOTE: Do NOT copy the Microsoft.Web.Infrastructure assembly.  This assembly has been rebuilt in the mono library and will already be available to your app. 

 

Configure Apache/XSP

Once you have Mono runtime installed, setup your apache virtual host configuration. Setup a virtual host to use mod-mono-server4. If you installed Mono with a custom prefix (like my script above), then be sure and setup your virtual host environment. Here’s a sample virtual host configuration using the Mono install prefix of, “/opt/mono-2.10”.

<VirtualHost *:80>  
  ServerName yourhostname

  DocumentRoot /srv/www/mvc3

  <Directory />
    Options FollowSymLinks
  </Directory>

  MonoServerPath mvc3 "/opt/mono-2.10/bin/mod-mono-server4"

  #optional debug mode
  MonoDebug mvc3 true 

  #be sure and set the prefixed mono paths here
  MonoSetEnv mvc3 MONO_IOMAP=all;PATH=/opt/mono-2.10/bin:$PATH;LD_LIBRARY_PATH=/opt/mono-2.10/lib:$LD_LIBRARY_PATH;  

  MonoApplications mvc3 "/:/srv/www/mvc3"
  <Location />
    Allow from all
    Order allow,deny
    MonoSetServerAlias mvc3
    SetHandler mono
    SetOutputFilter DEFLATE
    SetEnvIfNoCase Request_URI "\.(?:gif|jpe?g|png)$" no-gzip dont-vary
  </Location>
  <IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/javascript
  </IfModule>
</VirtualHost>

For running XSP manually, setup your shell environment with the correct prefix. Here’s a sample bash script.

#!/bin/bash  
MONO_PREFIX=/opt/mono-2.10  
GNOME_PREFIX=/usr  
export DYLD_LIBRARY_FALLBACK_PATH=/lib:  
export LD_LIBRARY_PATH=/lib:/opt/mono-2.10/lib:  
export C_INCLUDE_PATH=/include:/include  
export ACLOCAL_PATH=/share/aclocal  
export PKG_CONFIG_PATH=/lib/pkgconfig:/lib/pkgconfig  
export PATH=/bin:/opt/mono-2.10/bin:$PATH

 

When you’re ready to fire up Xsp4: save the script above to a file (in this case env.sh), enter a terminal, change your working directory to the web directory and enter the following:

$ source env.sh  
$ xsp4

 

Fire It Up

Once everything has been deployed, fire up xsp4 or restart your apache2, all should be working.

 

Errors You Might See

  • System.Security.SecurityException: No access to the given key.
    • Delete Microsoft.Web.Infrastructure.dll from you bin directory.


  • No response from web server on request, but you do see errors in the log /var/log/apache2/error.log

    • Check to make sure all your dependencies are present in the bin folder and apache has read/execute access to them. (Ubuntu: www-data group).  Also read through the errors at the end of the file and see if something else is causing the problem. Sometimes I rename/delete error.log and then restart the server for a fresh log.


  • File request returns a 404 or asp.net missing assembly exception (but the file(s) actually exists)

    • Check your file permissions and make sure www-data group has read access to the files and read/execute to the directories involved.
    • find web-directory -type f exec chmod 664 {} \;
    • find web-directory -type d -exec chmod 775 {} \;


  • Cannot find View...  (but it really does exist...)
    • There are a few things that cause this. 
    • File permissions; make sure www-data can read the web files
    • missing Web.config (especially under ~/Views); this is what informs the compiler how to build the view templates.
    • Syntax error in a view (may cause other views not to render returning this error).
    • Missing assemblies (make sure you bin deploy the site with all the necessary Razor and WebPages dlls. Do not deploy Microsoft.Web.Infrastructure.
    • Layout/Master page being used doesn't exist.
    • Getting: "The classes in the module cannot be loaded",   Just delete System.Web.WebPages.Administration.dll from your local bin directory. (not required for this).


comments powered by Disqus