How to Host and ASP.NET Application with Mono - Part 3: Hosting on Linux

This is the grand finale of our three part series covering how to run ASP.NET applications utilizing Mono.  We are sure by now you have read Part 1 of our series, but if you haven't keep in mind that Mono doesn't currently support .NET Framework 4.5, so follow the instructions in Part 1 of this series if you need to downgrade to .NET Framework 4.0.  In this post we will be installing the ASP.NET MVC 4.0 WebApi application on Linux.  This process was more complicated than setting up Mono on Mac OSX, so be sure to follow the steps below closely in your implementations.



We have code running on Windows 8.1 and Mac OSX, but now it is time to test Mono and Linux together.  We set up an Ubuntu environment using ami-9a562df2 in AWS which can be found on the marketplace by searching for Ubuntu Server 14.04 LTS (HVM).  This AMI is free tier eligible.

We assigned a public IP address to the instance and after the environment became available we connected to it using PuTTY.  You have to use ubuntu@<<public ip address>> and your private key to connect to the instance.  More information can be found here.

Once connected we needed to install new software for Mono and Apache.  We used the mono install instructions as reference.  The first issue we ran into was that we received a message when running:

sudo su

sudo: unable to resolve host ip-10-0-0-73

To fix, we ran 

nano /etc/hosts

We added an entry for our private IP (i.e. " ip-10-0-0-73") and saved the hosts file.

We can now start the process of setting up repositories and installing the new software.

Repository set up:

sudo apt-key adv --keyserver --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
echo "deb wheezy main" | sudo tee /etc/apt/sources.list.d/mono-xamarin.list
echo "deb wheezy-apache24-compat main" | sudo tee -a /etc/apt/sources.list.d/mono-xamarin.list

Installation commands:

apt-get update
apt-get upgrade
apt-get install mono-complete
apt-get install apache2
/etc/init.d/apache2 stop

Once apache is stopped we installed mono-apache-server2, supporting packages, and mono-apache-server4:

apt-get install mono-apache-server2 libapache2-mod-mono libmono-i18n2.0-cil
apt-get install mono-apache-server4



We downloaded winscp and connected to the instance, so that we can easily transfer files from the windows VM and the Ubuntu server.

We needed to grant permissions to the ubuntu user so that the user could copy files to the html directory.  For our purposes we made the ubuntu user the owner of the directory:

chown -R -v ubuntu /var/www/

We copied our ASP.NET MVC application to the Ubuntu server in the /var/www/html/ directory.

We needed to let Apache know to use Mono when a request came in on port 80.  To do so, we edited the 000-default.conf file located in the /etc/apach2/sites-available directory using:

nano /etc/apache2/sites-available/000-default.conf

We added VirtualHost entries are as follows:

<VirtualHost *:80>
  ServerName testapi
  ServerAdmin <<email address>>
  DocumentRoot /var/www/html/<<location of bin directory - do not include bin in path>>
  MonoServerPath testapi "/usr/bin/mod-mono-server4"
  MonoDebug testapi true
  MonoSetEnv testapi MONO_IOMAP=all
  MonoApplications testapi "/:/var/www/html/<<location of bin directory - do not include bin in path>>"

  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined

  <Location "/">
    Allow from all
    Order allow,deny
    MonoSetServerAlias testapi
    SetHandler mono
    SetOutputFilter DEFLATE
    SetEnvIfNoCase Request_URI "\.(?:gif|jpe?g|png)$" no-gzip dont-vary
  <IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/javascript

Finally, we need to start apache:

/etc/init.d/apache2 start

You should be able to hit your application using http://<<public ip address>>/api/test.  Replace "test" with the name of your controller.

We hope you have enjoyed our three part series on Mono.  We walked through downgrading a ASP.NET MVC Application from framework 4.5 to 4.0, Installing and running that application on Mac OSX, and finally installing and running that application on Linux.