Dot Net on Linux

Published 1/22/2017 10:41:49 AM  |  Last update 4/30/2023 09:28:11 AM
Tags: linux, asp.net, mono, .net

Mono, an implementation of Microsoft dotNet framework on the ECMA standards for C# and the Common Language Runtime, has been around for a while and got support from a large community. With recent implements to fully support ASP.NET 2.0, ASP.NET MVC and ASP.NET AJAX, it guarantees to run smoothly C# based applications which do not heavily depend on Windows Server and MSSQL, making possible deployment of Asp.Net websites on non-Windows servers. Asp.Net web developers now can benefit much from these advantages of Mono, saving a lot of money on their website hosting; instead of paying 50 bucks a month for Windows VPS hosting, they just have to pay 10 bucks or less using Linux VPS. I will show how to set up Mono web server on Linux VPS to run multiple Asp.Net websites.

There are three different options for running Asp.Net web apps with Mono on Linux. Of the threes, XSP is the fastest one (please see Benchmark on Asp.Net with Mono Options) but least stable due to lack of handling unloading events in a web app cycle, and is most appropriate for learning purpose. In this tutorial, I choose FastCGI mode with NGINX. This option brings more flexibility and ease to adapt future improvements and requirements.

First, you need a VPS. Just check lowendbox community, where you should not spend more than $10 per month for a good VPS, and make a deal from this market. Then, please refer this article for how to install NGINX, with MySql if needed, on a VPS.

Now, your VPS is ready for installation of Mono web server. Please make sure the Linux distro on your VPS is up-to-date:

apt-get update
# if you get the error “There is no public key available for the following key” then please try:
  apt-get install debian-keyring debian-archive-keyring
  apt-get update
# some times the problem is because IPv6 is set to default.
# please open: /etc/gai.conf, then locate and uncomment the last line:
  #
  #    For sites which prefer IPv4 connections change the last line to
  #
  #precedence ::ffff:0:0/96  100
# for some reasons that you do not wanna go with the current unstable version
# of mysql with your Linux distro, before installing it, please use:
  apt-get dist-upgrade

Because Debian 7 wheezy is popular nowadays, I choose this Linux distro to show how to set up Mono web server. Please check this website for other distros of Linux. Followings are setup steps in details:

  1. Update Mono repos
    apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
    echo "deb http://download.mono-project.com/repo/debian wheezy main" | tee /etc/apt/sources.list.d/mono-xamarin.list
    apt-get update
    # the following command is for mod_mono mode only, please skip it
    # echo "deb http://download.mono-project.com/repo/debian wheezy-apache24-compat main" | tee -a /etc/apt/sources.list.d/mono-xamarin.list
    # add libgdiplus repo:
    echo "deb http://download.mono-project.com/repo/debian wheezy-libjpeg62-compat main" | tee -a /etc/apt/sources.list.d/mono-xamarin.list
  2. install complete Mono packages
    apt-get -y install mono-complete
  3. Install Mono web server for Asp.Net 4.0 and former versions
    apt-get install mono-fastcgi-server4
    Note: HyperFastCgi is an alternative to Mono Fastcgi web server. However, I have neither much experiences nor technical documents on it. I hope to have time to give it a try soon.
  4. Mono virtual servers config file Create an XML file with multiple "web-application" entries as below, each for a virtual server. Save the file with .webapp extension
    <apps>
      <web-application> 
        <name>server-nickname</name>
        <vhost>server-[sub-]domainname</vhost>
        <vport>server-port(e.g.80)</vport>
        <vpath>/</vpath>
        <path>server-physical-path(e.g./var/www/aspnet)</path>
        </web-application>
    </apps>
    Note: You may also place each entry in one XML file and store all the files in a folder. Just copy your websites to the designated folders, DLLs should be placed in bin sub-folder as with Windows server.
  5. Mono web server log file Create Mono web server log file at /var/log/mono/ using the following commands
    mkdir -p /var/log/mono/
    > /var/log/mono/fastcgi.log
  6. Start Mono web server
    fastcgi-mono-server4 --appconfigfile <path-to-webapp-file> /socket=tcp:127.0.0.1:9005 /logfile=<path-to-log-file> &
    I used port 9005 for Mono web server because I reserve the one of 9000 for php-fpm. Mono server is now ready to process Asp.Net web requests through port 9005. You may open this port to let Mono work on the internet. In this tutorial, I set Mono as a back-end web server to process requests dispatched by NGINX proxy server. Please add the following settings to the corresponding server entry in NGINX config file:
    location ~ \.(aspx|axd)$ {
      index index.html index.htm Default.aspx;
      fastcgi_pass 127.0.0.1:9005; # i used port 9005
      fastcgi_index Default.aspx;
      include /etc/nginx/fastcgi_params;
      fastcgi_param PATH_INFO $fastcgi_path_info;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
    # The last two lines can be added to /etc/nginx/fastcgi_params instead
    
  7. Auto-started-on-boot Mono web server You need to make a Linux shell script to start Mono web server using the command in step 6 using permissions of an user other than root. On Debian 7, you can use start-stop-daemon command. You may want to use my script, mymono which is made available here. Please copy the script into /etc/init.d/ folder and use the following command to have it started on system boot.
    update-rc.d mymono defaults

Update will come up with cross-platform Asp.Net 5 and the .NET Core runtime. Stay tuned for more about this issue. Thank you for reading the article. Please shoot me an email for any inquiry.

© 2024 blog.tinyray.com  by tinyray