Serving up ASP.NET from Apache with Mod_Mono

Casting Net

© morganimation, Fotolia

© morganimation, Fotolia

Article from Issue 102/2009
Author(s):

Mod_Mono lets you host .NET applications on your Linux Apache 2 server.

What if your company invested in some elaborate ASP.NET web applications back in the days before they discovered Linux? What if your heterogeneous environment requires that the same dynamic pages run on multiple platforms? Do you need to ditch your Linux Apache web server just to serve up web content developed for Microsoft's .NET framework?

The Mono Project [1] has been hard at work on the problem of supporting the .NET framework in Linux, and part of their attention has been on the issue of ASP.NET. Mono now offers an Apache module known as Mod_Mono for hosting ASP-based .NET web applications from Apache servers [2]. In this article, I take a quick look at how to set up your Apache server to support ASP.NET. Of course, this discussion isn't intended as a complete tutorial on setting up Apache – a subject that could easily fill a very large book – but if you already have some basic knowledge of Apache configuration, read on for more on bringing .NET to your web environment.

When you install Mod_Mono, it is a good idea to use the package provided by your distro. On Ubuntu and Debian systems, look for libapache2-mod-mono, and for Fedora, use mod_mono plus mono-web. Also, you will need the XSP package. In Fedora, you must install XSP from rawhide; in Ubuntu and other Debian-based distros, install XSP directly as a package dependency. (If you prefer, you can install Mod_Mono from source [2], but keep in mind that you're less likely to run into compatibility issues if you use a package.)

Activate the module by inserting an appropriate section in apache2.conf. In Ubuntu/Debian, the module is normally enabled as part of the install, but if not, run the command sudo a2enmod mod_mono to enable it. Alternatively, see Listing 1 for a sample configuration. After you've made the necessary changes, reload Apache with /etc/init.d/apache2 force-reload.

If you only want a basic setup, you're done. Your server now handles ASP.NET pages. To test it, either set up a folder in your web root directory with one of your own ASP.NET applications, or install the asp.net-examples package and go to http://your.server.net/samples to see some sample applications (Figure 1).

If you installed from source, copy the XSP test directory (/lib/xsp/test or /usr/lib/xsp/test, depending on where you did your install) to the Apache home directory. Make sure you have the permissions set correctly. (The Apache user must be able to read the directory and files.)

The Debian/Ubuntu version automatically provides a configuration similar to the sample config file in Listing 1. First, Apache loads the module, then it sets up a socket for the server and makes the path to the server executable. The MonoAutoApplication line means you shouldn't need to explicitly specify MonoApplications lines to add an app to your server (see discussion later if you don't want apps to be handled automatically), and the AddType line tells Apache to set the correct application type for files with the given extensions.

Listing 1

Apache Module Config

 

The DirectoryIndex line tells Apache to treat files with the specified names as an index. This command means when you go to, say, http://your.server.net/test/ and there is a default.aspx file in that directory, that file is served up to the browser.With these settings, Mod_Mono will automatically start mod_mono_server and kick off the web application required when a request is sent to the browser.

Web-Based Control Panel

A basic web control panel available with Mod_Mono lets you restart mod-mono-server, which is handy when you need to reload modified assemblies (Figure 2). The control panel also gives you information about the requests currently in process.

To use the control panel, add the lines in Listing 2 to your Apache configuration. If you have a Debian-style system with the Mod_Mono configuration in /etc/apache2/mods-available/mod_mono.conf, it's probably best to add the code to the file sourced in there: /etc/mono-server/mono-server-hosts.conf. Otherwise, put this code in the mod_mono section of your Apache config file.

In Listing 2, replace 127.0.0.1 with the IP address of the computer from which you want to access the control panel (if it isn't just the localhost). Be aware that the IP address is the only restriction. In other words, any user with an account on the specified machine will be able to use the control panel.

Listing 2

Web Control Panel Config

 

Now reload Apache and take a look at http://my.domain.com/mono

Several additional options are available if you want to tweak your Mod_Mono configuration. For instance, if you want to limit the number of concurrent requests, use:

MonoMaxActiveRequests 20
MonoMaxWaitingRequests 20

The Apache ThreadPool directive limits the number of requests that the Mod_Mono server back end can handle at one time. If too many requests arrive at once, it can cause deadlocks. The preceding directives avoid this problem by limiting the number of requests that are passed in at one time. The value of 20 is the default; alter it according to your ThreadPool limit (0 disables the limits).

The following commands restart the Mono server every three hours (the time format is DD[:HH[:MM[:SS]]]):

MonoAutoRestartMode Time
MonoAutoRestartTime 00:03

If you find that the Mono server process is growing over time, you might want to periodically restart the system. Also, you can trigger the restart on the basis of the number of requests served rather than on time:

MonoAutoRestartMode Requests
MonoAutoRestartRequests 10000

Another possibility is to set up the server to listen on a particular port:

MonoListenPort 8080

This feature lets you run your ASP.NET-ready Mono server on a different port from your regular Apache server, although you'll probably need to modify your firewall settings for this to work.

Multiple Applications

For most people, enabling MonoAutoApplication in the configuration file (Listing 1) should work fine. However, if you prefer to register applications explicitly, you can do so by editing your Apache configuration so that the mod_mono section looks like the code in Listing 3. (You'll need to leave the LoadModule line intact.)

Listing 3 disables autohosting, adds the Mono handler for those filetypes, and defines two locations where Mono apps can be found. Each is specified as virtual-location:disk-location. Reload Apache and try that sample page again – it should still work fine.

Listing 3

Mono Without Automatic Config

 

For different applications, you also can run named independent instances of the server – perhaps to separate testing and production servers or to limit user usage independently. In addition, you'll need the LoadModule and AddHandler lines as above, then see Listing 4 for an example of the configuration for two servers. Again, this code goes in the mod_mono section of your Apache 2 configuration.The additional argument to MonoApplications is the alias for a particular instance of the server, which is then associated with a particular directory.

Listing 4

Multiple Servers

 

By adding the server alias as the second argument, you can apply any of the options discussed above. For instance, MonoListenPort my-home 8080 sets the my-home server to listen on port 8080. Again, this approach can be useful to separate out servers and also because different applications might have different requirements.

ASP.NET Version 2

The default Mono server, and the one discussed in this article, is version 1.1. If you want to run version 2, you need to install it (packages are available) and then edit your Mod_Mono configuration to use version 2 – the directives to edit are MonoServerPath and MonoPath. Then, reload Apache. If you're using Debian/Ubuntu, the configuration details are in the mod_mono.conf file.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Java and .NET Intro

    In today's heterogeneous environments, it is hard to escape the influence of Java and .NET. So why not come prepared? This month we examine some tools and techniques for integrating the Java and .NET frameworks with Linux.

  • Novell Releases Mono 2.6 and MonoDevelop 2.2

    The Mono framework compatible with Microsoft's C# .NET is available in version 2.6. Developers also upgraded its associated MonoDevelop environment to version 2.2.

  • SUSE Linux Enterprise 11 Arrives

    Novell announced today that SUSE Linux Enterprise Server (SLES) is officially out in version 11. The SLES extensions for Mono and the desktop version SLED 11 should also concurrently hit the market. A high-availability extension is to follow.

  • Mono 2.0 is Ready: Compilers for C# 3.0 and Visual Basic 8

    The developers of Mono, the free implementation of the .NET framework, have achieved a major milestone with version 2.0.

  • Mono Development for Visual Studio from Novell

    What a few years ago might have been considered a joke has now become reality: thanks to the Mono Tools for Visual Studio, developers can create RPM packages directly from the MS environment.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News