Friday, March 12, 2010

Bookmark and Share

July 18, 2009 by Ivan  

[HowTo] Run ASP.NET on Linux With Apache Web Server (Mono + Mod_Mono)

Mono is an open source project led by Novell (formerly by Ximian) to create an Ecma standard compliant, .NET-compatible set of tools, including among others a C# compiler and a Common Language Runtime. Mono can be run on Linux, BSD, UNIX, Mac OS X, Solaris and Windows operating systems.

With Apache mod_mono we can run ASP.NET with C# on Linux machine. Mono is not just an ASP.NET interpreter, it also build a windows form and run on Linux, BSD, UNIX, Mac OS X, Solaris and Windows operating systems. But for now i just want to show you how to run an ASP.NET application on Linux machine.

Ok, get let’s start. Follow these steps (for ubuntu):
1. You need to install several library for mono. Type this command:

sudo apt-get install mono-xsp2 mono-<a href="http://www.ivankristianto.com/recommends/hosting" style="color:#FF0000;color:#FF0000;color:#FF0000;" target="_blank" rel="nofollow" onmouseover="self.status='apache';return true;" onmouseout="self.status=''">apache</a>-server2 libapache2-mod-mono mono-gmcs mono-utils

2. After it finish you need to edit your mod_mono.conf. The easiest way to run Asp.Net application is using
AutoHosting configuration as example below:

AddType application/x-asp-net .aspx .ashx .asmx .ascx .asax .config .ascx
DirectoryIndex index.aspx
MonoAutoApplication enabled
MonoServerPath "/usr/bin/mod-mono-server2"

You can read more about AutoHosting here or manual hosting configuration here, for ubuntu user please refer to this documentation.
3. Save the file and restart your apache.
4. Now let’s start write the first Asp.Net application. You can copy the text below:
Filename: hello.aspx

<%@ Page language="c#" src="hello.aspx.cs"
Inherits="HelloApp.HelloPage" AutoEventWireup="true" %>
<html>
  <head>
	<title>First Mono ASP.NET Application</title>
  </head>
  <body>
	<form  runat="server">
	  Enter your name: <asp:TextBox id="name" runat="server" />
	  <asp:Button id="greet" Text="Greet" onClick="OnGreetClick" runat="server"/>
	</form>
	<br /><strong><asp:Label id="message" runat="server">Hello, World!
			</asp:Label></strong>
  </body>
<html>

Filename: hello.aspx.cs

using System;
using System.Web.UI.WebControls;
namespace HelloApp
{
public class HelloPage : System.Web.UI.Page
{
protected Label message;
protected Button greet;
protected TextBox name;
public void OnGreetClick(Object sender, EventArgs e)
{
message.Text = "Hello, " + name.Text;
}
}
}

5. Ok now save file to your web server. Mine is at the /var/www/firstaspnet/
6. And you will see your first Asp.Net application run on Linux OS.

You can see the sample here.

If you like this post, please leave me a comment. Have a nice day.

Bookmark and Share

Related Post(s)

Comments

10 Responses to “[HowTo] Run ASP.NET on Linux With Apache Web Server (Mono + Mod_Mono)”
  1. Rob says:

    Why would you want to? There are far, far more adept tools already available on *nix without that and its included non-native bloat. Plus .NET/Mono is not known for standards compliance or producing good code.

    All in all, this is a stupid idea.

  2. Ivan says:

    Hi Rob,
    In my case, i have an ASP.Net application and only have a Linux Server. And need to deploy as soon as possible. So i think mono would be the quick and dirty solution for that. Rather than i rebuild it into java project.

  3. matt says:

    Yes how stupid. why would anyone want to develop tools to allow you to port .net and other applications from expensive IIS servers to cheap / free linux servers …..

  4. Socan says:

    Because he must :-(

  5. Alex says:

    I have a (probably stupid) question- if the hosting company on whose linux servers i’m hosting my page is using mono, do i have to configure something in my asp.net project or it should work already?

  6. Ivan says:

    Hi Alex,
    No you don’t have to. Just upload to the server and it’s ready to run.

  7. Alex says:

    Thanks!

  8. Naveesh says:

    Does this also works for vb language

  9. RobertG says:

    Works great! Very simple, concise directions! One small glitch: In step 5, you save BOTH files to the target directory, not just one.

    Another answer to some who ask why run .NET on Linux: Being knowledgeable in areas across disciplines is critical to long term career success in IT. Being able to make very different systems talk to one another, or to write applications in .NET, Java for desktops, servers or even embedded applications makes you very employable in this economy.

    Cheers!

  10. Tohid says:

    Hi Ivan,

    Thanks for the useful Article.
    If I have a compiled asp.net application (I mean just some .aspx empty files and a /bin folder full of .dll), does it run as well on linux with your manual?

    Regards,
    -T

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!