logo

Configuring Mono for C# on CentOS6 and Nginx

I'm working on some C# project components that I need to run under CentOS6. Mono (http://www.mono-project.com) is an open source implementation of Microsoft's .NET Framework based on the ECMA standards for C# and the Common Language Runtime. With Mono installed, many of the .NET features will be available under the CentOS platform.

Install Mono

To install and configure on the CentOS instance, first install the necessary prerequisites:

yum -y install bison gettext glib2 freetype fontconfig libpng libpng-devel libX11 libX11-devel glib2-devel libgdi* libexif glibc-devel urw-fonts java unzip gcc gcc-c++ automake autoconf libtool make bzip2

Now download the source, configure, make & install (this will take some time, go make a coffee or ten):

cd /home
curl -L http://download.mono-project.com/sources/mono/mono-2.10.8.1.tar.gz | tar xz
cd /home/mono-2.10.8.1
./configure --prefix=/opt/mono
make && make install

Now we need to add the Mono install location to the PATH variable. The example below ensures that this will be persisted between CentOS republishes of this file:

echo 'pathmunge /opt/mono/bin' > /etc/profile.d/ree.sh
chmod +x /etc/profile.d/ree.sh
. /etc/profile

Now we can test:

mono --version

Install the FastCGI Server for Mono

We now need to give Nginx something to proxy requests for FastCGI support to. First install the prerequisites from the EPEL library.

cd /home
curl http://www.mirrorservice.org/sites/dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm >> epel-release-6-8.noarch.rpm
rpm -ivh epel-release-6-8.noarch.rpm
yum -y install mono-devel mono-nunit-devel

Now get the XSP source, configure and install.

curl -L http://download.mono-project.com/sources/xsp/xsp-2.10.tar.bz2 | tar xj
cd /home/xsp-2.10
./configure --prefix=/opt/mono
make && make install

The Mono FastCGI server requires initialisation for each app. This can be done collectively once, by specifying the default parameters for each application. In our scenario, we're going to configure a single application, but rather than start this manually, we're going to auto-start with chkconfig.

Tomas (http://yojimbo87.github.com/2010/03/14/mono-startup-script.html) has written a start-up script that we are going to use and modify to start a single application (www.mywebsite.com).

We're going to amend line 20 to reflect the application that we want to start, as follows:

WEBAPPS="www.mywebsite.com:/:/var/www/www.mywebsite.com/"

Now enable execution on the script, configure for start-up and start the daemon:

chmod +x /etc/init.d/monoserver
chkconfig monoserver on
service monoserver start

Configure Nginx

Once Mono has been installed, we need to configure Nginx to understand what to do with requests for .NET services. This is done by modifying the Nginx configuration file (normally at /etc/nginx/nginx.conf) as follows:

location / {
    try_files $uri $uri/ /index.aspx;
}
location ~* .(js|css|png|jpg|jpeg|gif|ico)$ {
    expires max;
    log_not_found off;
}
location ~ .(aspx|asmx|ashx|asax|ascx|soap|rem|axd|cs|config|dll)$ {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param SERVER_PORT $server_port;
    fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    fastcgi_param PATH_INFO "";
    include fastcgi_params;
}

Finally, a restart of the Nginx service (service nginx restart), and you will be able to test by uploading the following into the website root, titled Default.aspx:

<% Response.Write("Hello World!"); %>