ASP.NET on Ubuntu with up to date Mono, Nginx, and MariaDB - ScottRFrost/scottrfrost.github.io GitHub Wiki

Log in to your Ubuntu box and then type "sudo su -" to switch to root.

The default ubuntu packages for Nginx and Mono are prehistoric, so run "nano /etc/apt/sources.list" then add the following lines to the bottom of the file:

#Nginx
deb http://nginx.org/packages/mainline/ubuntu/ trusty nginx

#Mono
deb http://download.opensuse.org/repositories/home:/tpokorra:/mono/xUbuntu_14.04/ ./

#MariaDB
deb http://nyc2.mirrors.digitalocean.com/mariadb/repo/10.0/ubuntu trusty main

Then run the following commands (still as root):

apt-get update && apt-get install nginx mono-opt mono-xsp-opt mariadb-server

Create the following configuration file by typing "mkdir /etc/mono/fastcgi4 && nano /etc/mono/fastcgi4/default.webapp". This file is where you'll add more apps later if you add more nginx sites:

<apps>
<web-application>
		<name>default</name>
		<vhost>*</vhost>
		<vport>80</vport>
		<vpath>/</vpath>
		<path>/usr/share/nginx/www</path>
</web-application>
</apps>

Create the following file by typing "nano /etc/init.d/mono-fastcgi" and pasting the text below:

#!/bin/sh

### BEGIN INIT INFO
# Provides: monoserve.sh
# Required-Start: $local_fs $syslog $remote_fs
# Required-Stop: $local_fs $syslog $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start fastcgi mono server with hosts
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/mono/bin
DAEMON=/opt/mono/bin/mono
NAME=monoserver
DESC=monoserver

MONOSERVER=$(which fastcgi-mono-server4)
MONOSERVER_PID=$(ps auxf | grep fastcgi-mono-server4.exe | grep -v grep | awk '{print $2}')
LD_LIBRARY_PATH=/opt/mono/lib:$LD_LIBRARY_PATH
PKG_CONFIG_PATH=/opt/mono/lib/pkgconfig:$PKG_CONFIG_PATH

WEBAPPS="/:/var/www/ofa"

case "$1" in
start)
if [ -z "${MONOSERVER_PID}" ]; then
echo "starting mono server"
${MONOSERVER} /appconfigdir=/etc/mono/fastcgi4 /root=/opt/mono /socket=unix:/var/run/mono.sock /logfile=/var/log/mono/fastcgi.log &
sleep 2
chmod 666 /var/run/mono.sock
echo "mono server started"
else
echo ${WEBAPPS}
echo "mono server is running"
fi
;;
stop)
if [ -n "${MONOSERVER_PID}" ]; then
kill ${MONOSERVER_PID}
sleep 1
rm /var/run/mono.sock
echo "mono server stopped"
else
echo "mono server is not running"
fi
;;
esac

exit 0

Type the following command to make the new script run during startup:

ln -s /etc/init.d/mono-fastcgi /etc/rc2.d/S80mono-fastcgi

Start the mono-fastcgi server by typing (this will run automatically on boot):

/etc/init.d/mono-fastcgi start

Modify the configuration for nginx "nano /etc/nginx/sites-available/default":

	server {
    #Config
    listen       [::]:80;
    listen       *:80;
    server_name  yourservername.whatever.com;

    #Root
    location / {
            root   /usr/share/nginx/www;
            index  index.html index.htm index.aspx default.aspx;
    }

    #Discard favicon.ico errors
    location = /favicon.ico {
            log_not_found off;
            access_log off;
    }

    #Discard robots.txt errors
    location = /robots.txt {
            allow all;
            log_not_found off;
            access_log off;
    }

    #ASP.NET
    location ~ \.(aspx|asmx|ashx|asax|ascx|soap|rem|axd|cs|config|dll)$ {
            include /etc/nginx/fastcgi_params;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_pass   unix:/var/run/mono.sock;
    }

    #Errors
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
            root   /usr/share/nginx/html;
            index index.php;
    }
}

Create a text page by typing "nano /usr/share/nginx/www/default.aspx" Add the following contents:

<%@ Page Language="C#" AutoEventWireup="true" %>

<!DOCTYPE html>
<html>
<head runat="server">
	<title>Mono Test</title>
</head>
<body>
	<div>
	<% var test = "ASP.NET via Mono FastCGI, served up by nginx at " + DateTime.Now; %>

	<%= test %>
	</div>
</body>
</html>

Restart nginx by typing:

service nginx restart

Web browse to the IP address of your linux box and it should be up.

⚠️ **GitHub.com Fallback** ⚠️