Installing Asp.Net on Linux (nginx+mono+xsp)

In this article I’ll show how to set up a simple nginx + Asp.Net combo. By simple I mean that project-specific quirks, user permission separation, high load and so on need to be configured separately (this goes especially for Asp.Net). Article written by habrahabr user gouranga.

At some point, wrestling with hosting small Asp.Net projects, I realized one simple thing: buying a Windows Server license and then renting a dedicated/virtual server powerful enough for some home projects/experiments is extremely unreasonable. The solution popped into my bald head right away: there’s Mono! A short search on mono-project.com led to the Asp.Net FAQ.

So, we have a freshly installed Debian Squeeze x64 Minimal. The easiest way to install the latest nginx versions is from the dotdeb.org repositories. By the way, it’s a really good repository: besides nginx it always has the latest versions of php, mysql (percona) and redis. Installing from source is a bit harder - more on that below.

Installing nginx

Add the new repository to sources.list, register the GnuPG key and update the sources:

echo deb http://packages.dotdeb.org stable all » /etc/apt/sources.list
wget -q http://www.dotdeb.org/dotdeb.gpg -O- | apt-key add -
apt-get update

There are three different packages built with different sets of modules: nginx-lite, nginx-full (the nginx package is actually an alias for it) and nginx-extras. Which one to install is up to you - lite would be enough for us (it has Proxy, that’s fine), but at the time of writing this article I’d already installed full, so we’ll go with that:

apt-get install nginx

If you’re installing nginx from source, ./configure needs to run with these parameters:

./configure -conf-path=/etc/nginx/nginx.conf -error-log-path=/var/log/nginx/error.log -http-client-body-temp-path=/var/lib/nginx/body -http-fastcgi-temp-path=/var/lib/nginx/fastcgi -http-log-path=/var/log/nginx/access.log -http-proxy-temp-path=/var/lib/nginx/proxy -http-scgi-temp-path=/var/lib/nginx/scgi -http-uwsgi-temp-path=/var/lib/nginx/uwsgi -lock-path=/var/lock/nginx.lock -pid-path=/var/run/nginx.pid -with-debug -with-http_addition_module -with-http_dav_module -with-http_geoip_module -with-http_gzip_static_module -with-http_image_filter_module -with-http_realip_module -with-http_stub_status_module -with-http_ssl_module -with-http_sub_module -with-http_xslt_module -with-ipv6 -with-sha1=/usr/include/openssl -with-md5=/usr/include/openssl -with-mail -with-mail_ssl_module

This will basically match the nginx-full package, except for missing the third-party Upstream Fair Queue and Echo modules.

Installing Mono

Mono doesn’t have that repository convenience, so we’ll have to compile the latest source right away.
Download the latest mono and xsp source (2.10.2 at the time of writing):

wget http://ftp.novell.com/pub/mono/sources/mono/mono-2.10.2.tar.bz2
wget http://ftp.novell.com/pub/mono/sources/xsp/xsp-2.10.2.tar.bz2

Now let’s unpack the archives:

tar xf *.tar.bz2

If tar complains about missing bzip2 (it will on minimal for sure), install it:

apt-get install bzip2

To compile mono and xsp we’ll need the following:

apt-get install build-essential gawk bison gettext libgdiplus pkg-config libglib2.0-0 libglib2.0-dev

Let’s move on to configuring and compiling:

cd mono-2.10.2 && ./configure -prefix=/usr -sysconfdir=/etc/mono && make && make install && cd ..

If the installation went well, running mono -version will show us the coveted and long-awaited:

mono -version

Mono JIT compiler version 2.10.2 (tarball Sat Jun 11 15:54:39 MSD 2011)
Copyright (C) 2002-2011 Novell, Inc and Contributors. www.mono-project.com
			        TLS:           __thread
			        SIGSEGV:       altstack
			        Notifications: epoll
			        Architecture:  amd64
			        Disabled:      none
			        Misc:          softdebug
			        LLVM:          supported, not enabled.
			        GC:            Included Boehm (with typed GC and Parallel Mark)
```


### Now let's install xsp: > cd xsp-2.10.2 && ./configure -prefix=/usr -sysconfdir=/etc/xsp && make && make install && cd .. All of xsp is written in C#, so compilation should be quick. Let's check what it outputs: > xsp4 -version
xsp4.exe 2.10.2.0
Copyright (C) 2002-2011 Novell, Inc.
Minimalistic web server for testing System.Web
```


Looks like it's all set. Time to move on to configuration.

### Configuring Xsp

Let's create a directory for our future Hello World page: 

> mkdir -p /var/www/asptest

Let's go ahead and create the classic "Hello World" to test it:

> nano /var/www/asptest/Default.aspx

In the file, write these lines:

```bash
<%@ Page language="C#" %>
<html>
				    <head>
								        <title>Hello C#</title>
				    </head>
				    <body>
								        <p><% Response.Write("Hello World");%></p>
				    </body>
</html>
```


Since xsp was originally designed as a test server, there are no daemon startup scripts there. We'll fix that. Let's create the file `/etc/default/xsp` and write the default variables into it:

> user=www-data  
> group=www-data
> 
> port=8080  
> address=0.0.0.0

Let's create the startup file and give it execute permissions:

> wget /wp-content/uploads/2014/04/xsp  
> mv xsp /etc/init.d/  
> chmod +x /etc/init.d/xsp 

Let's add it to autostart with the default runlevels (adjust as needed) and start our new daemon:

> update-rc.d xsp defaults  
> /etc/init.d/xsp start

By default, the server listens on all interfaces - easy to check:

> netstat -nlp | grep 8080

### Configuring nginx The point of the nginx setup is to proxy requests only for asp files. All other static files should be served by nginx directly. ```bash server { listen 80; server_name serveraddr.ru; location / { root /var/www/asptest #root /usr/lib/xsp/test; index index.html index.htm index.aspx default.aspx Default.aspx; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location ~ \.(aspx|asmx|ashx|axd|asax|ascx|soap|rem|axd|cs|config|dll)$ { root /var/www/asptest proxy_pass http://127.0.0.1:8080; } } ``` That's it. Again, the Mono wiki clearly states that xsp is best used for testing! [Original](http://habrahabr.ru/post/121159/)
Share Button