Mandy's Tech Blog

View My GitHub Profile

Follow me on Twitter

Come work with me!

Site feed

Developing for .NET on the Mac, Part 2: Serving ASP.NET Pages

[Updated 2/11/09: Included information about my experience in making a fresh build on a fresh install of Leopard]

Getting a Macintosh to serve up ASP.NET content is actually pretty simple. Mono's primary support for ASP.NET comes through an Apache plugin called mod_mono. This is very good news for the Mac user because Apache version 1.3 comes as part of Tiger. As long as you don't need to run other plugins that require newer versions of Apache (like Subversion), setting up ASP.NET on a Mac could scarcely be easier.

Prerequisites:

  • I'm assuming that you've already read and followed Part 1 of this series. At a minimum, you should have:
    • The Subversion client
    • A directory for Subversion checkouts (which I refer to as ~/src/mono)
    • A binary distribution of Mono from their website

Here's what you need to do:

  1. Open a terminal and go to ~/src/mono.
  2. Check out the code for mod_mono. This command should get the job done: svn checkout svn://anonsvn.mono-project.com/source/trunk/mod_mono.
  3. Go to ~/src/mono/mod_mono and run the command ./autogen.sh --prefix=/Library/Frameworks/Mono.framework/Versions/Current.
  4. Update: on a fresh install of Leopard, I found that autogen.sh and configure were failing because a tool named libtoolize was renamed to glibtoolize, and the configuration scripts were not configured to handle this. You can fix the problem either by creating an alias for libtoolize or by editing the single line in autogen.sh that refers to libtoolize to point to glibtoolize, instead; furthermore, based on what I read here, it seems that the configuration script fails to detect the 64-bit architecture on some Macs. If this happens to you (you'll know because Apache will fail to start up), run CFLAGS="-m64" ./autogen.sh --prefix=/Library/Frameworks/Mono.framework/Versions/Current followed by a make clean and make.
  5. Follow this by running make && make install. Note that you will probably need elevated permission to complete the installation ste.
  6. Edit /etc/httpd/httpd.conf and add this to the end of the file: Include /private/etc/httpd/mod_mono.conf. Update: in Leopard, this is /etc/apache2/httpd.conf.
  7. Open System Preferences and select "Sharing".
  8. Check the box next to "Personal Web Sharing".

At this point, you should have basic support for ASP.NET on your Mac. The last step that you performed turned on Apache, which by default will look for content in /Library/WebServer/Documents, and putting aspx files ino this folder should result in them being handled by mod_mono.

It should be noted that many applications will require more advanced setup than this (for example, applications like the CruiseControl.NET web dashboard want to be able to handle all web requests for their directory, not just the ones pointing to aspx files), but this is a topic for another post.

Developing for .NET on the Mac, Part 1: Building Mono for the First Time

Update: the process for building Mono has changed significantly since I first wrote this. You can view updated instructions here.

Ever since I first heard about Mono, I've been fascinated by the potential of .NET to become a viable platform for software that targets multiple OSes. Unfortunately, I've discovered (as seems to be the case with many OSS projects), the documentation aimed towards beginners is quite limited, especially when one ventures away from running Mono on Linux.

I've spent many hours trying to get my development environment for the Mac just right, and at times, it's been a rather painful process. So, in the interest of reducing the pain felt by any beginners following after me, I've decided to start recording my processes for getting things set up. At best, this blog will become a useful resource for anyone doing .NET development on the Mac. At worst...well, at least I'll be able to set things up again the next time I have to wipe a hard drive or replace a machine.

One of the first things that I've found in my work is that the Mono installer available from the Mono Project is rarely sufficient for doing any serious development work: bugs abound in the new parts of Mono (I've found a number of problems in generics), and sometimes, I simply can't resist the urge to dig in and fix them. Therefore, when setting up a new development environment, my first goal is generally to get a custom build of Mono up and running.

Here's how I do it:

Prerequisites:

  • Install Mac OS X—this one's kind of a no-brainer, but just for the record, I'm currently running version 10.4.10. This is the case both for my first-generation Mac Mini and my Mac Pro.
  • Install XCode—again, this shouldn't be too surprising, but I'm aiming for completeness, here :).
  • Install Fink—I've found that this is the best way to get a lot of the common Unix tools you'll need along the way. Personally, I prefer using FinkCommander, which puts a nice GUI on top of Fink. Be sure to follow all of the installation instructions on their website, or things can go mysteriously and confusingly wrong.
  • Install pkg-config, glib2-dev, and svn-client; you can get these through Fink.
  • Install and install an official distribution of Mono—sometimes it comes in handy.

The actual work:

  1. Create a folder for your mono source. For the purpose of these instructions, I'll assume you're going to put it in ~/src/mono.
  2. Open a Terminal window and go to ~/src/mono. Run the command svn checkout svn://anonsvn.mono-project.com/source/trunk/mono svn://anonsvn.mono-project.com/source/trunk/mcs. This will download the code for the core Mono libraries and the Mono Compiler System. They'll end up in ~/src/mono/mono and ~/src/mono/mcs.
  3. Create a folder into which you can install your custom build. I put mine in /opt/mono.
  4. Run the command cd ~/src/mono/mono && ./autogen.sh —prefix=/opt/mono. This step should get Mono ready to build.
  5. You're now ready to build and install: make && make install. I'd love to be able to recommend that you also insert make check in that list, but the sad fact is that Mono on MacOS actually fails a number of tests.

After you've done all this, you should have a working (custom) build of Mono working on your machine. You can, at any time, upgrade to the latest code by going to ~/src/mono and running the command svn update mono mcs. Generally, you'll be able to jump straight to building Mono again, but you may sometimes be required to also run autogen.sh.

Also, it's often very handy to have a script around that will set up all of your paths and whatnot to your custom build of mono. The Parallel Mono Environments page on the Mono website describes this, although I had to modify mine a bit to get it working right. Here's what it looks like now:

#!/bin/bash
MONO_PREFIX=/opt/mono
GNOME_PREFIX=/opt/gnome
export LD_LIBRARY_PATH=$MONO_PREFIX/lib:/sw/lib:$LD_LIBRARY_PATH
export C_INCLUDE_PATH=$MONO_PREFIX/include:$GNOME_PREFIX/include:/sw/include:/Library/Frameworks/Mono.framework/Versions/1.2.2.1/include/libpng
export ACLOCAL_PATH=$MONO_PREFIX/share/aclocal
export PKG_CONFIG_PATH=$MONO_PREFIX/lib/pkgconfig:$GNOME_PREFIX/lib/pkgconfig:/sw/lib/pkgconfig:/usr/lib/pkgconfig:/usr/local/lib/pkgconfig:/usr/X11R6/lib/pkgconfig:/Library/Frameworks/Mono.framework/Versions/1.2.2.1/lib/pkgconfig
PATH=$MONO_PREFIX/bin:$PATH:/sw/bin
PS1="[mono] \w @ "

Mine is stored in ~/mono-dev-env and is invoked via source ~/mono-dev-env

That's it for my first post on setting up a development environment for Mac/.NET development. In the future, I'll go into setting up mod_mono (for ASP.NET), CruiseControl.NET (you do have unit test you'd like to run regularly, don't you?), and Subversion (I realize that Subversion isn't strictly related to .NET, but I didn't have a Subversion server when I started this project, and trying to figure out how to get it set up wasn't terribly fun).

Just switched to SubText 1.9.5

Update: Google Analytics tells me that people are coming to my blog for information about installing Subtext on GoDaddy's servers. Because I'm such a wonderful person, I'm adding instructions for how to do this. (7-4-07)

Update 2: Mikeas has posted more detailed instructions on how to modify the Web.config that comes with Subtext, including a couple of things that I had forgotten to mention (like e-mail). You can view his instructions here.

I used to be on dasBlog, but it was difficult to install onto GoDaddy's servers, and the version I was using didn't have support for ASP.NET 2.0 (which is needed for some other development that I want to do), so I switched to SubText.

With any luck, I'll actually get around to posting real content here in the future…

Installing SubText on GoDaddy's Servers

This wasn't originally what I had intended to blog about, but it seems to be why people are coming to my site right now, so here's how I did it. For the sake of completeness, I'm using GoDaddy's Deluxe Windows Hosting for this site, and it runs me about seven bucks a month.

  1. Log into your GoDaddy account; then, go to My Account -> Hosting & Servers -> My hosting account -> Open Control Panel.
  2. Go to Settings -> ASP.NET Runtime
    1. Open the properties for Content Root and make sure that your ASP.NET version is 2.0. If you have to change it, be aware that it may take some time for GoDaddy to update its servers, and it's not a terribly great idea to upload files to your server until this is done.
    2. Create a directory (I'm assuming you'll call it 'blog'). Give it Read, Web, Write, and SetRoot permissions.
    3. If you want to use Photo Galleries or the MetaWeblog API's MediaObject, you'll also need to create a directory called blog/Images and make sure that it has similar permissions (I haven't actually done this part, since the setup guide says it's optional)
    4. Be aware that steps 2 and 3 may take a while to complete on GoDaddy's end. I don't recommend uploading files until the Control Panel says that everything is up to date.
  3. Go to Databases -> SQL Server
    1. Create a database that uses SQL Server 2005. Most of the settings won't matter a whole lot, but you'll want to make sure that you remember the password, at least for a few minutes. :)
    2. Once the database is set up, grab the ODBC connections string for your new database (you can find this by going to the details page of the new database and clicking the configuration button). Remove the "Driver" attribute from this string, and put your password into it.
  4. Modify Subtext's Web.Config file and insert the connection string that you got in step 3. The location for this should be somewhere around line 40. The name of the string is "subtextData".
  5. Once GoDaddy is done setting up all of your directories and databases, ftp to your site and upload all of your content into /blog.
  6. Navigate to http://yourdomain.sometld/blog. Subtext should make the rest of the process easy.

A note about automatic redirecting:

For whatever reason, GoDaddy has elected to not allow you to change the ASP.Net permissions on your root folder, and 404 redirects won't work for visiting http://yourdomain.sometld/. If, like me, your blog is the primary content on your site, and this is the first thing that you'd like visitors to see, you can configure a Default.aspx that will redirect people to http://yourdomain.sometld/blog. Here's what mine looks like:

<%@ Page language="c#" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

<html>
	<head runat="server">
		<title></title>
		<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
		<meta name="CODE_LANGUAGE" Content="C#">
		<meta name=vs_defaultClientScript content="JavaScript">
		<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
	</head>
	<body MS_POSITIONING="GridLayout">
		<form id="Form1" method="post" runat="server">
			<% Response.Redirect("http://www.fallingcanbedeadly.com/blog"); %>
		</form>
	</body>
</html>

Anyway, this is how I have my blog set up, and I would assume that it would work for most users, but I make no guarantees to this effect. Let me know how it turns out for you!