I wanted to see how the .NET implementation on Linux works. It was decided to deploy Ubuntu Server on our hypervisor, install the fresh mono package and run the ASP.NET MVC4 site via nginx.
On the official mono website, a ready-made package for Ubuntu is only 2 years old. From this moment began the search on the network ways to implement his plan. The result of this experience was a script for automatic deployment on bare Ubuntu Server 12.04.3 or 13.04 of everything necessary for running ASP.NET MVC4 sites:
- Let's build from sources and install mono 3.2.1
- Add a monoserve script to init.d to automatically start and manage sites.
- Install nginx 1.4.1
- Create and configure a simple site to test the performance of the entire bundle.
Immediately under the cut line is a line to automatically perform the entire procedure described in the article.Installing just one line in the console
One line for downloading the script, setting the right to launch it and the actual launch. First, it will ask to press [Enter] to add the repository, and later it will again ask for the password for sudo.
wget https://bitbucket.org/mindbar/install-mono/raw/master/install-nginx-mono.sh && sudo chmod +x install-nginx-mono.sh && ./install-nginx-mono.sh
The execution of the entire script depends on the speed of the Internet and the power of iron. On average about 40 minutes.Requirements
It is supposed to install on a clean Ubuntu Server 12.04.3 x64 or Ubuntu Server 13.04 x64.
OS is installed without a role:

')
I recommend immediately after installing the OS in a virtual machine to keep snapshot clean system. Very convenient for experiments.
The script was tested in the home directory from an unprivileged user, but with sudo rights.
Install mono & co
To install mono, we need some dependencies and utilities. Install them:
sudo apt-get -y install build-essential git autoconf libtool automake gettext libglib2.0-dev libjpeg-dev libpng12-dev libgif-dev libexif-dev libx11-dev libxrender-dev libfreetype6-dev libfontconfig1-dev
After that, we clone and
compile libgdiplus , mono, and xsp in that order. All files will be kept in a separate
~ / monobuild directory , and installed in
/ usr / localAt the time of this writing, the libgdiplus version was
2.10.8 . The latest version can be found here:
github.com/mono/libgdiplus/releases mkdir monobuild cd monobuild git clone https://github.com/mono/libgdiplus.git cd libgdiplus git checkout 2.10.8 ./autogen.sh --prefix=/usr/local make && sudo make install cd ..
Actually
mono . New releases are released quite often - once a month or two. The current version is
mono-3.2.1 . The latest version can be found here:
github.com/mono/mono/releasesAn interesting caveat: to build mono you must first build a monolite compiler. Fortunately, it comes bundled, you only need to first assemble it and then pass the path to it to build the mono itself:
git clone https://github.com/mono/mono.git cd mono git checkout mono-3.2.1 ./autogen.sh --prefix=/usr/local make get-monolite-latest && make EXTERNAL_MCS=${PWD}/mcs/class/lib/monolite/gmcs.exe && sudo make install cd ..
The queue reached the
xsp server - a web server for debugging sites on mono. Written in C #. Along with him are the FastCGI components. The current version is
3.0.11 . The latest version can be found here:
github.com/mono/xsp/releases git clone https://github.com/mono/xsp.git cd xsp git checkout 3.0.11 ./autogen.sh --prefix=/usr/local make && sudo make install cd .. cd ..
Check version
mono -V ?

nginx
nginx is one of the recommended web servers for bundling with mono. Interaction of nginx + mono via FastCGI (preferred method) or as reverse proxy for xsp is possible.
xsp is a server for testing and debugging sites on mono. For the prom medium it is not recommended to use it.
There is nothing special about the nginx installer. We connect the repository, update the list of packages and install nginx.
sudo apt-get -y install python-software-properties sudo add-apt-repository ppa:nginx/stable sudo apt-get update sudo apt-get -y install nginx
monoserve
Sites should run with the server.
The proposed configuration is similar to that used in nginx:
- The / usr / local / etc / mono / fcgi / apps-available / directory stores the settings of the available servers
- In the / usr / local / etc / mono / fcgi / apps-enabled / directory, symlinks are created for those that should work now
A monoserve script was written that takes a list of sites from files in the / usr / local / etc / mono / fcgi / apps-enabled directory and runs them as demons from the user www-data.
An example of the content of such a file (one line):
/:/home/anvol/www
As you can see, this is just a "/:" + path to the application. Unfortunately, the current version of the script correctly starts only one such server. It is planned to finalize monoserve, so that in the configuration files you can also specify the application port or the name of the unix-socket. Then it will be possible to run multiple sites on the same server.
In the meantime, from the repository prepared by me, we’ll load the slightly revised ASP.NET MVC4 template of the site into the ~ / www folder and configure monoserve to launch it.
git clone https://mindbar@bitbucket.org/mindbar/mono-mvc4-default.git www sudo mkdir /usr/local/etc/mono/fcgi sudo mkdir /usr/local/etc/mono/fcgi/apps-available sudo mkdir /usr/local/etc/mono/fcgi/apps-enabled sudo touch /usr/local/etc/mono/fcgi/apps-available/default echo "/:`pwd`/www" | sudo tee -a /usr/local/etc/mono/fcgi/apps-available/default sudo ln -s /usr/local/etc/mono/fcgi/apps-available/default /usr/local/etc/mono/fcgi/apps-enabled/default wget https://bitbucket.org/mindbar/install-mono/raw/master/monoserve sudo cp monoserve /etc/init.d/monoserve sudo chmod +x /etc/init.d/monoserve sudo update-rc.d monoserve defaults sudo /etc/init.d/monoserve start rm monoserve
After that we have running fastcgi-mono-server4 on port 9001, which is ready to run the site from the ~ / www folder
Nginx configuration
The final chord will be the nginx setting. First of all, we will disable the default server by removing the symlink to it from / etc / nginx / sites-enabled /:
sudo rm /etc/nginx/sites-enabled/default
Now add the necessary settings to / etc / nginx / fastcgi_params:
echo "# mono config" | sudo tee -a /etc/nginx/fastcgi_params echo "fastcgi_param PATH_INFO \"\";" | sudo tee -a /etc/nginx/fastcgi_params echo "fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;" | sudo tee -a /etc/nginx/fastcgi_params
We write the configuration for our mono server:
echo "server {" | sudo tee -a /etc/nginx/sites-available/mono-default echo " listen 80;" | sudo tee -a /etc/nginx/sites-available/mono-default echo " server_name localhost;" | sudo tee -a /etc/nginx/sites-available/mono-default echo " location / {" | sudo tee -a /etc/nginx/sites-available/mono-default echo " root `pwd`/www/;" | sudo tee -a /etc/nginx/sites-available/mono-default echo " index index.html index.htm default.aspx Default.aspx;" | sudo tee -a /etc/nginx/sites-available/mono-default echo " fastcgi_index Home;" | sudo tee -a /etc/nginx/sites-available/mono-default echo " fastcgi_pass 127.0.0.1:9001;" | sudo tee -a /etc/nginx/sites-available/mono-default echo " include /etc/nginx/fastcgi_params;" | sudo tee -a /etc/nginx/sites-available/mono-default echo " }" | sudo tee -a /etc/nginx/sites-available/mono-default echo "}" | sudo tee -a /etc/nginx/sites-available/mono-default
We enable our configuration by creating a symlink in / etc / nginx / sites-enabled, and we ask nginx to accept changes to work
sudo ln -s /etc/nginx/sites-available/mono-default /etc/nginx/sites-enabled/mono-default sudo /etc/init.d/nginx restart
We go to the browser on the ip-your-virtualka or check the work of the site in the console:
wget localhost && cat index.html

Use on health.
Plans
- First of all - modify the monoserve script.
- Further, in my opinion, it is worth adding database support to the site template. In our case, we will deploy PostgreSQL and be friends with the ASP.NET MVC4 template.
- Instead of git clone repository download the archive of the required release. This is faster, but on the other hand, the local repository can be updated more quickly, switched to another version.
- Add error handling with the termination of further execution of the script.
Comments to the script, wishes for its future improvements are welcome. Thank.
Links to materials used
FastCGI - MonoRun ASP.Net MVC4 on Ubuntu 12.10Mono / FastCGI Startup Script