Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Setting up ASP.NET MVC 5 via Mono on Mac OSX, with Apache

Install ASP.NET MVC 5 on Mono, Mac OSX, apache with mod_mono

Install mono MDK

  • NOTE: no x64 package is available (unless you want to install from source), so I installed the x86 version

  • Install mono MDK from:

http://www.mono-project.com/download/

I used 3.10.0, which corresponds to .NET 4.5

Install mono develop

I'm honestly suprised at how good an IDE this is compared with Visual Studio: http://www.monodevelop.com/download/

Install XCode

Ensure you have XCode and the command line build tools installed http://osxdaily.com/2014/02/12/install-command-line-tools-mac-os-x/

Install mod_mono

NOTE: You have to install and build from source; no package is available for OSX, it is not included in mono MDK for OSX

  1. Get the latest version from:

http://download.mono-project.com/sources/mod_mono/

I used http://download.mono-project.com/sources/mod_mono/mod_mono-3.8.tar.gz

  1. Extract the tarball
  2. Run this script to set up a build environment:
#!/bin/bash
MONO_PREFIX=/Library/Frameworks/Mono.framework/Home
export DYLD_FALLBACK_LIBRARY_PATH=$MONO_PREFIX/lib:$DYLD_LIBRARY_FALLBACK_PATH
export LD_LIBRARY_PATH=$MONO_PREFIX/lib:$LD_LIBRARY_PATH
export C_INCLUDE_PATH=$MONO_PREFIX/include
export ACLOCAL_PATH=$MONO_PREFIX/share/aclocal
export PKG_CONFIG_PATH=$MONO_PREFIX/lib/pkgconfig
export PATH=$MONO_PREFIX/bin:$PATH
PS1="[mono] \w @ "

(or you can save it as mono_build.sh and run this:)

source ./mono_build.sh
  1. Then build:
./configure
make
sudo make install

If you get an error message about Apache being too old, its probably because some directories got wiped out when you last upgraded Mac OSX. To fix, re-install the xcode command line tools:

http://stackoverflow.com/questions/19730245/installing-mod-mono-on-mac-osx-10-9-mavericks

Install xsp

  • This is a raw ASP.NET server that mod_mono will call to host the application.
  • Install and build xsp from source (also no binary package available for OSX)
  1. Get source from https://github.com/mono/xsp
  2. Run the same environment script from above, for mod_mono
./autogen.sh
make
sudo make install

Note: If you get the error message "No package 'mono' found", its probably because you didn't run the environment setup script

More detailed instructions are available: https://github.com/mono/xsp/blob/master/INSTALL

Configure Apache

On OSX, httpd.conf is usually in /private/etc/apache2/httpd.conf

  1. Add this line near the end of httpd.conf:
Include /private/etc/apache2/mod_mono.conf
  1. Create /private/etc/apache2/mod_mono.conf

This is for mono specific configuration

# mod_mono.conf

# Use 'include mod_mono.conf' from other configuration file
# to load mod_mono module.

<IfModule !mod_mono.c>
    LoadModule mono_module /usr/libexec/apache2/mod_mono.so
</IfModule>

<IfModule mod_headers.c>
    Header set X-Powered-By "Mono"
</IfModule>

AddType application/x-asp-net .aspx
AddType application/x-asp-net .asmx
AddType application/x-asp-net .ashx
AddType application/x-asp-net .asax
AddType application/x-asp-net .ascx
AddType application/x-asp-net .soap
AddType application/x-asp-net .rem
AddType application/x-asp-net .axd
AddType application/x-asp-net .cs
AddType application/x-asp-net .vb
AddType application/x-asp-net .master
AddType application/x-asp-net .sitemap
AddType application/x-asp-net .resources
AddType application/x-asp-net .skin
AddType application/x-asp-net .browser
AddType application/x-asp-net .webinfo
AddType application/x-asp-net .resx
AddType application/x-asp-net .licx
AddType application/x-asp-net .csproj
AddType application/x-asp-net .vbproj
AddType application/x-asp-net .config
AddType application/x-asp-net .Config
AddType application/x-asp-net .dll
DirectoryIndex index.aspx
DirectoryIndex Default.aspx
DirectoryIndex default.aspx

# NOTE: I'm not sure why this is needed when the app paths are also defined in the MonoApplications section
Alias /MyApplicationName "/Library/WebServer/Documents/path-to-web-app-directory"

# MonoServerPath can be changed to specify which version of ASP.NET is hosted
# mod-mono-server1 = ASP.NET 1.1 / mod-mono-server2 = ASP.NET 2.0
MonoServerPath MyApplicationName "/usr/bin/mod-mono-server4"

# To obtain line numbers in stack traces you need to do two things: 
# 1) Enable Debug code generation in your page by using the Debug="true" 
#    page directive, or by setting <compilation debug="true" /> in the 
#    application's Web.config
# 2) Uncomment the MonoDebug true directive below to enable mod_mono debugging
MonoDebug MyApplicationName true

# The MONO_IOMAP environment variable can be configured to provide platform abstraction
# for file access in Linux.  Valid values for MONO_IOMAP are:
#    case
#    drive
#    all
# Uncomment the line below to alter file access behavior for the configured application
MonoSetEnv MyApplicationName MONO_IOMAP=all

# Additional environtment variables can be set for this server instance using 
# the MonoSetEnv directive.  MonoSetEnv takes a string of 'name=value' pairs 
# separated by semicolons.  For instance, to enable platform abstraction *and* 
# use Mono's old regular expression interpreter (which is slower, but has a
# shorter setup time), uncomment the line below instead:
# MonoSetEnv MyApplicationName MONO_IOMAP=all;MONO_OLD_RX=1

MonoApplications MyApplicationName "/MyApplicationName:/Library/WebServer/Documents/path-to-web-app-directory"
<Location "/MyApplicationName">
Allow from all
Order allow,deny
MonoSetServerAlias MyApplicationName
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>
  1. Run sudo apachectl configtest to verify the configuration is correct
  2. Verify Apache is enabled in OSX
  3. To cause Apache to reload after configuration changes, run sudo apachectl restart

Build your application

  1. Put the application in the path you specified in mod_mono.conf (The bin dir should be in that directory)
  2. Use mono develop to create an ASP.NET MVC 5 application
  3. Build it

Run the site

Hit http://localhost/MyApplicationName

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment