Friday 28 November 2008

Apache, Mono and Virtual Hosts

I run some ASP.Net applications under mono on a Debian host. Mono has come a long way in the last few years and is now making progress in support for C# 3.0 and even WCF. This adds appeal to the platform and increases the scope of it's usefullness.


One thing I had trouble finding documentation for and ended up accomplishing through trial and error was setting up my Mono applications to run in an Apache (2) Virtual Host. The most difficult bit turned out to be the configuration of my sites-available file in /etc/apache2/sites-available. So I've decided to share it here:


<VirtualHost *>
ServerName example.com
ServerAlias www.example.com example.net www.example.net example.co.uk www.example.co.uk
DocumentRoot /var/www/example.com
ServerAdmin serveradmin@example.com
ErrorLog /var/log/apache2/example.com-error_log
#RewriteEngine on
#RewriteRule ^/([A-Za-z]*)/?$ /?Page=$1 [PT]
AddMonoApplications example "/:/var/www/example.com"
MonoUnixSocket example /tmp/.example_mod_mono_server2
MonoServerPath example /usr/lib/mono/2.0/mod-mono-server2.exe
AddType application/x-asp-net .aspx .ashx .asmx .ascx .asax .config .ascx .svc
MonoApplicationsConfigDir example /etc/mono-server2
MonoPath example /usr/lib/mono/2.0:/usr/lib:/usr/lib/mono/2.0
<Directory "/var/www/example.com">
MonoSetServerAlias example
SetHandler mono
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

As you can see, I have commented out the RewriteEngine and RewriteRule directives but there is enough there for you to see what new tricks we .Net devs have to play with when we write code for a Mono deployment...

3 comments:

Anonymous said...

May I ask why you commented out the rewite section. I am trying to figure out why rewriting isn't working on my installation. have you commented out your rewrites because they don't work?

Anonymous said...

I just came back to say thank for sharing this information sir! It's 2.30AM and I've been trying to solve my problem the whole day. I added [PT] to the end of my RewriteRules and now they work! No I just need to find out what [PT] means. Thanks again!

Unknown said...

Hi leftbrainlogic,
I only commented out the rewrite stuff because it's not useful to everyone who is just after a VirtualHost config. It does work for me. Apache needs to have mod rewrite enabled. I don't remember what the [PT] tag is for but it should be documented in mod rewrite.