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

Saturday July 18, 2009 by  

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-apache-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.

What people search:

Other Interesting Articles:

Post comment as twitter logo facebook logo
Sort: Newest | Oldest
martinopresnik 5 pts

I still don't get it. How to install correct handler for aspx file?

ivankrisdotcom 7 pts moderator

martinopresnik See step 2 on my article!

martinopresnik 5 pts

ivankrisdotcom I DID THIS TOOOOOO. But still NOT WORKING

martinopresnik 5 pts

Hello! When I go on my web page I get source code text and NOT web page. Can somebody tell me how to solve this? Thanks!

ivankrisdotcom 7 pts moderator

martinopresnik It is because your web server doesn't have the handler for you aspx file. You should install mono and add the correct handler for your aspx file.

martinopresnik 5 pts

ivankrisdotcom Can yo tell me how to install correct handler?

SauravGautam 5 pts

I installed mono tools for visual studio... I have prepared a application on asp.net. But i have hosting space of APACHE server. Can i upload my aspx files there. Please help me

ivankrisdotcom 7 pts moderator

SauravGautam You need to instal mono and mod_mono to run aspx on apache.

why do not work in firefox and chrome.
these http clients show resource code.
pls help me?

It's not your browser. It is your apache configuration.You need to add apache handler for your aspx script. otherwise it will consider as text.

I'm new at this, can you what handler should I write for .aspx extension?

ty

really nice article.........

Thanks that worked for me..................

I want to run dotNET application on RHEL 5. Iam unable to get all the packages for apache2-mod_mono. Plz Help...
thanx.....

have you tried to update your repository?

try to update your yum source list.
i haven't play with rhel 5 for a while.

Please provide me the link from where i can download all the required packages.

I want to run dotNET application on RHEL 5. Iam unable to get all the packages for apache2-mod_mono. Plz Help...

Thank You

Thanks mate.
Glad to hear that :)

Thank you sooo much, this is the only guide that actually worked for me! i got it working in a few minutes, instead of beating my head against the ground following other peoples tutorials!

Same here, top notch job !!

thanks for the article XD

Hi i'm sorry for that.
Before i was host on my friend VPS, and i installed Mono there.
But now i move to shared hosting, that's doesn't have Mono. So the code won't running.
But it used to work.

Hi your sample link is just the code , not running.

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

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

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!

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!

Does this also works for vb language

Does this also works for vb language

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?

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

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?

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

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 .....

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 .....

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.

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.

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.

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.

Thanks it helps!

Trackbacks

  1. [...] point of view). After installing the LAMP stack (click here if you need help with this) and Mono (click here for the step by step installation that I used for my investigations) I set about downloading Screwturn [...]