📜 ⬆️ ⬇️

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

In this article I will show how to configure a simple bunch of nginx + Asp.Net. By idle time, it should be understood that some specific features of projects, the delineation of user rights, high loads, etc. need to be configured separately (especially Asp.Net). Article is written at the request of habrauzera mace .

At one time, puzzled by the problem of hosting small Asp.Net projects, I realized one simple thing: buying a license for Windows Server, and then renting a powerful enough dedicated / virtual server for some homework / experiments is extremely unwise. The decision immediately surfaced in my bald head: there is Mono! A short search on mono-project.com brought to the Asp.Net FAQ . In fact, the documentation showed me three possible options:

You need to be able to support it.

A quick analysis of the Internet, for choosing one of these methods, led me to an article by Maxim Krentovsky ( mkrentovskiy ), which is quite funny, because ask Maxim in ICQ for some reason I did not guess.

Conclusions Maxim seemed to me quite convincing. I did not conduct any additional tests or look for other sources, for my problem the answer was already obvious: nginx + xsp. Remember, Mono's wiki says that xsp should be used for debugging and development!
')
Nginx installation

So, we have a freshly installed Debian Squeeze x64 Minimal. The easiest way to install the latest versions of nginx is to install from the dotdeb.org repositories. By the way, a very good repository: in addition to nginx , there are always the latest versions of php , mysql (percona) and redis . Slightly more difficult to install from source - about it just below.

Add a new repository to sources.list , prescribe the GnuPG key and update the sources:
 root@falco:~# echo "deb http://packages.dotdeb.org stable all" >> /etc/apt/sources.list root@falco:~# wget -q http://www.dotdeb.org/dotdeb.gpg -O- | apt-key add - root@falco:~# apt-get update 

If everything is done correctly, then on the apt-cache policy nginx command we will see the following:
 root@falco:~# apt-cache policy nginx nginx: : () : 1.0.2-1~dotdeb.1  : 1.0.2-1~dotdeb.1 0 500 http://packages.dotdeb.org/ stable/all amd64 Packages 0.7.67-3 0 500 http://mirror.yandex.ru/debian/ squeeze/main amd64 Packages 

There are three different packages assembled with different sets of modules: nginx-lite , nginx-full (the nginx package is just its alias) and nginx-extras . It’s up to you to install which one, we’d have enough lite versions too (Proxy is okay), but at the time of this writing I’ve already installed full, so we’ll install it:
 root@falco:~# apt-get install nginx 

Here are all the steps for installing from packages.

If you install nginx from source, then ./configure must be run with the following 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 almost correspond to the nginx-full package, except for the absence of third-party modules Upstream Fair Queue and Echo.

Mono installation

For Mono there are no such conveniences with repositories, it is necessary to immediately compile the latest sources.

upd . As Net_Rat rightly pointed out , I was a little tricky: you can connect an experimental repository and adjust the mono and xsp from there through pining .

Downloading the latest mono and xsp sources (at the time of writing 2.10.2):
 root@falco:~# wget "http://ftp.novell.com/pub/mono/sources/mono/mono-2.10.2.tar.bz2" root@falco:~# wget "http://ftp.novell.com/pub/mono/sources/xsp/xsp-2.10.2.tar.bz2" 

Now unpack the archives:
 root@falco:~# tar -xvf mono-2.10.2.tar.bz2 root@falco:~# tar -xvf xsp-2.10.2.tar.bz2 

If tar scolds for the absence of bzip2 (in minimal exactly scolds), you need to put it:
 root@falco:~# apt-get install bzip2 

To compile mono and xsp we need the following:
 root@falco:~# apt-get install build-essential gawk bison gettext libgdiplus pkg-config libglib2.0-0 libglib2.0-dev 

Let's start the configuration and compilation (the process of compiling directly can take a long time, up to 20-30 minutes):
 root@falco:~/mono-2.10.2# ./configure --prefix=/usr --sysconfdir=/etc/mono root@falco:~/mono-2.10.2# make && make install 

If the installation was successful, launching the mono --version command will show us the long-awaited and cherished:
 root@falco:~/mono-2.10.2# mono --version Mono JIT compiler version 2.10.2 (tarball   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 install xsp:
 root@falco:~/xsp-2.10.2# ./configure --prefix=/usr --sysconfdir=/etc/xsp root@falco:~/xsp-2.10.2# make && make install 

All xsp is written in C #, the compilation should go very fast. Check that it will give us:
 root@falco:~# xsp4 --version xsp4.exe 2.10.2.0 Copyright (C) 2002-2011 Novell, Inc. Minimalistic web server for testing System.Web 

It seems that everything is grabbed. It's time to move on to customization.

Xsp customization

Create the directory of our future Hello World page:
 root@falco:~# mkdir -p /var/www/asptest 

Immediately create a classic "Hello World" for the sample:
 root@falco:~# cd /var/www/asptest root@falco:/var/www/asptest# nano Default.aspx 

In the file we write the following lines:
 <%@ Page language="C#" %> <html> <head> <title>Hello C#</title> </head> <body> <p><% Response.Write("Hello World");%></p> </body> </html> 

Since xsp was originally conceived as a test server, the daemon launch scripts are not there. We will correct this situation. Create the file /etc/default/xsp , and write variables to it by default:
 user=www-data group=www-data port=8080 address=0.0.0.0 

Let's create the file /etc/init.d/xsp , give it execution rights ( chmod +x /etc/init.d/xsp ) and write to it:
 #!/bin/sh ### BEGIN INIT INFO # Provides: xsp # Required-Start: $local_fs $remote_fs $network # Required-Stop: $local_fs $remote_fs $network # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Asp.Net testing server startup script. # Description: Asp.Net testing server startup script. ### END INIT INFO PATH=/sbin:/usr/sbin:/bin:/usr/bin DESC=xsp NAME=xsp DAEMON=/usr/bin/xsp4 DEFAULT=/etc/default/$NAME DAEMON_ARGS="--nonstop --root /var/www/asptest" #DAEMON_ARGS="--nonstop --root /usr/lib/xsp/test" MONO_SHARED_DIR=/var/run/$NAME PIDFILE=/var/run/$NAME.pid SCRIPTNAME=/etc/init.d/$NAME [ -x "$DAEMON" ] || exit 0 [ -f "$DEFAULT" ] && . $DEFAULT . /lib/lsb/init-functions if [ ! -e $MONO_SHARED_DIR ]; then mkdir $MONO_SHARED_DIR chown $user:$group $MONO_SHARED_DIR fi chk_start() { if [ -f "$PIDFILE" ]; then xpid=`head -1 "$PIDFILE"` xps=`ps -p $xpid | wc -l` if [ "$xps" != "1" ]; then log_action_msg "Xsp is running" return 1 fi fi return 0 } do_start() { export MONO_SHARED_DIR start-stop-daemon --start --background --make-pidfile \ --quiet --pidfile /var/run/$NAME.pid \ --user $user --group $group --chuid $user \ --exec $DAEMON -- \ --port $port --address $address \ $DAEMON_ARGS } do_stop() { if [ -e "$PIDFILE" ] ; then kill -9 `head -1 "$PIDFILE"` 1>/dev/null 2>&1 rm -f "$PIDFILE" fi } case "$1" in start) if chk_start ; then log_daemon_msg "Starting $DESC" "$NAME" do_start log_end_msg $? fi ;; stop) log_daemon_msg "Stopping $DESC" "$NAME" do_stop log_end_msg $? ;; status) ;; restart|force-reload) log_daemon_msg "Restarting $DESC" "$NAME" do_stop do_start log_end_msg $? ;; *) echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2 exit 3 ;; esac : 

The startup script also has a commented out line for the test folder from the xsp distribution, for a more complete picture.

Add it to the autorun with default launch levels (correct as needed) and launch our new demon:
 root@falco:~# update-rc.d xsp defaults root@falco:~# /etc/init.d/xsp start 

By default, the server listens on all interfaces, check is easy:
 root@falco:~# netstat -nlp | grep 8080 tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 20839/mono 

If you go to http: // serveraddr: 8080 / you can see the long-awaited “Hello World”:


And if you switch to the xsp test page, then:


Nginx configuration

The essence of nginx settings in proxying accesses only for asp-files. The rest of the static files should be distributed by nginx . Without further thinking, let's take the configuration that Maxim Krentovsky proposed in his article and slightly modify it for us:
 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 #root /usr/lib/xsp/test; proxy_pass http://127.0.0.1:8080; } } 

How to manage virtual hosts nginx best knows Google.

Having opened http://serveraddr.ru/ we get the long-awaited “Hello World”:


Or, for test settings:


That's all. Again, Mono’s wiki clearly states that xsp best used for tests!

This is my first post on Habré, please do not throw bricks and other cobblestones at me ... I would be very happy with any criticism, both in the content of the article and in the spelling with punctuation. And yes, I could be mistaken with the choice of the blog. If so, tell me where to put it.

PS I myself, though I love .Net / C #, are completely unfamiliar with Asp.Net, even wrote Hello World from an article using Google. So to ask questions on Asp.Net and Asp.Net MVC is useless to me. :-)

Source: https://habr.com/ru/post/121159/


All Articles