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

Update Sep 18, 2016: This was my very old article. It might not work anymore. And it is obsolete with today technology. Please use other technology rather than using this. Thanks.

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

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

Comments

  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.

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

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

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

      • Dan says:

        Hi Ivan. First, thanks for the straight forward tutorial. It is sad that some are jealous and angry about their lack of skills or knowledge. I’m not hating on all beginners. a beginner who will soon achieve greatness will ask intelligent questions and learn from people who have the knowledge they seek. These people are already in the right mindset.

        Then you have the amateurs out there that will try to sound intelligent by BSing and putting others down, They are unlikely to learn anything. And don’t worry, the people out there who know what they are doing can instantly spot those people and know they are just bitter wannabes who lack the brain power to even learn enough to one day make quality comments. Just ignore these people. The people who actually have experience can see right through them.

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

  5. Socan says:

    Because he must 🙁

  6. Socan says:

    Because he must 🙁

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

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

  9. Alex says:

    Thanks!

  10. Alex says:

    Thanks!

  11. Naveesh says:

    Does this also works for vb language

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

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

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

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

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

  17. Thanks mate.
    Glad to hear that 🙂

  18. Papuyadav says:

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

    Thank You

  19. Papuyadav says:

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

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

  21. Kakkar Anchal says:

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

  22. Prashanth says:

    Thanks that worked for me………………

  23. HirenPatel93 says:

    really nice article………

  24. Barisbar says:

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

  25. SauravGautam says:

    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

  26. martinopresnik says:

    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!

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

  28. martinopresnik says:

    @ivankrisdotcom I DID THIS TOOOOOO. But still NOT WORKING

  29. abdul1 says:

    Very nice article thanks, I tried it and it worked, it would be great if you can update it for mono 2.8.

  30. rstoffers says:

    Thanks for the article.
     
    If you want asp.net 4.0 then also do:
     
    sudo apt-get install mono-apache-server4
     
    in your mod_mono.conf put the following:
     
    AddType application/x-asp-net .aspx .ashx .asmx .ascx .asax  .config .ascxDirectoryIndex index.aspxMonoAutoApplication enabledMonoServerPath "/usr/bin/mod-mono-server4"

  31. rstoffers says:

    Oh, and if you want to give yourself access to change files in the /var/ww directory, do:
     
    cd /var/www
    sudo chown -R root:www-data .
    usermod -a -G www-data <yourusername> 
     

  32. rstoffers says:

    Oh, and if you want to give yourself access to change files in the /var/ww directory, do:
     
    cd /var/www
    sudo chown -R root:www-data .
    sudo chmod -R 774 .
    sudo usermod -a -G www-data <yourusername> 
     
     

  33. Indra Bayu says:

    Thanks for posting this. I will be great if you can also post tutorials about this web server you’re using in Linux, specifically in handling ASP.NET Websites like what we deal with using ISS.

    Thanks!

  34. Vivek Patwardhan says:

    what was sestatus then?
    I am working on same thing, having trouble in SELinux and mono configuration.
    My .aspx loads when selinux is disabled; and can’t when it is disabled or permissive.

  35. netflow says:

    Really wish google wouldn’t put obsolete articles at the very top of search results. Most of these packages are obsolete or unavailable

Give me your feedback

This site uses Akismet to reduce spam. Learn how your comment data is processed.