How to run Asp.Net Web Forms, MVC2 and MVC3 applications on FreeBSD

Would you like to share some of your solutions for certain problems? Tips or tricks? Post here. All new topics are automatically moderated.

How to run Asp.Net Web Forms, MVC2 and MVC3 applications on FreeBSD

Postby vand777 » 17 Aug 2011, 19:57

1. Install Mono

Code: Select all
# cd /usr/ports/lang/mono
# make config-recursive
# make install clean


In order to test Mono installation, create a new file [FILE]/tmp/test.cs[/FILE]:

Code: Select all
using System;
 
class Program {
    static void Main ()
    {
        Type t = Type.GetType ("Mono.Runtime");
        if (t != null)
             Console.WriteLine ("You are running with the Mono VM");
        else
             Console.WriteLine ("You are running something else");
    }
}


Then compile it and run:

Code: Select all
# cd /tmp
# mcs test.cs
# mono test.exe
You are running with the Mono VM


2. Install Nginx web server
Code: Select all
# cd /usr/ports/www/nginx
# make config-recursive
# make install clean
# echo "nginx_enable=YES" >> /etc/rc.conf
vand777
Member
 
Posts: 292
Joined: 20 Jun 2010, 17:26
Location: London

Postby vand777 » 17 Aug 2011, 19:59

3. Install Xsp

Code: Select all
# cd /usr/ports/www/xsp
# make config-recursive
# make install clean


4. Configure Nginx

Edit [FILE]/usr/local/etc/nginx/nginx.conf[/FILE] file:

Code: Select all
...
user  [color="Red"]asp[/color];
worker_processes  [color="Red"]4;  # should be equal to number of cores on your server[/color]
...
 
        [color="Red"]location / {
            fastcgi_pass   192.168.1.100:9000; # change this IP to the IP and port of your fastcgi-mono-server (see below)
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }[/color]
 
        [color="Red"]#[/color]location / {
        [color="Red"]#[/color]    root   /usr/local/www/nginx;
        [color="Red"]#[/color]    index  index.html index.htm;
        [color="Red"]#[/color]}
...


Create a new user asp (shell [FILE]nologin[/FILE], random password):

Code: Select all
# adduser
...
# mkdir /home/asp/www
# chown asp:asp /home/asp/www/
# chmod 555 /home/asp/www/
# touch /var/log/mono-server4.log
# chown asp:asp /var/log/mono-server4.log
vand777
Member
 
Posts: 292
Joined: 20 Jun 2010, 17:26
Location: London

Postby vand777 » 17 Aug 2011, 20:03

5. Configure startup scripts

Create a new file [FILE]/home/asp/fcgi[/FILE]:

Code: Select all
fastcgi-mono-server4 /socket=tcp:[color="Red"]9000[/color] /address=[color="Red"]192.168.1.100[/color] /applications=/:. /root=/home/asp/www /logfile=/var/log/mono-server4.log &


Adjust its permissions:

Code: Select all
# chmod ug+x /home/asp/fcgi


Create a startup script for fastcgi-mono-server4 in the [FILE]/home/asp/fcgi_startup[/FILE] file:

Code: Select all
#!/usr/local/bin/bash
 
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr/local/bin:$HOME/bin:; export PATH
PID=`/bin/ps waux | /usr/bin/grep mono | /usr/bin/grep -v grep | /usr/bin/awk '{ print $2 }'`
 
if  ps waux | grep mono | grep -v grep > /dev/null
 then
        echo "fastcgi-mono-server4 seems to run on PID $PID"
        exit
else
    echo "fastcgi-mono-server4 is not running. Starting it"
    /home/asp/fcgi
fi


Make it executable:

Code: Select all
# chmod ug+x /home/asp/fcgi_startup


Now add the job to crontab:

Code: Select all
# crontab -uasp -e


And then add the following line to crontab file:

Code: Select all
* * * * * /home/asp/fcgi_startup > /home/asp/mono_laststate


Start Nginx:

Code: Select all
# /usr/local/etc/rc.d/nginx start


[color="Red"]Do not forget to adjust firewall rules.[/color]
vand777
Member
 
Posts: 292
Joined: 20 Jun 2010, 17:26
Location: London

Postby vand777 » 17 Aug 2011, 20:05

Test it

Create a new file [FILE]/home/asp/www/hello.aspx[/FILE]:

Code: Select all
<html>
<body>
<p>It works! <% =Environment.OSVersion.ToString() %></p>
</body>
</html>


And then request it at [FILE]http://your.ip.address/hello.aspx[/FILE]. It should give you the version of your operating system. It displays on my system:

Code: Select all
It works! Unix 8.2.2.0
vand777
Member
 
Posts: 292
Joined: 20 Jun 2010, 17:26
Location: London

Postby vand777 » 17 Aug 2011, 20:11

Asp.Net Web Forms and MVC2 applications

Asp.Net Web Forms and MVC2 applications should work automatically under this setup (Mono 2.10.2). Just copy files into the [FILE]/home/asp/www/[/FILE] folder.

Asp.Net MVC3 applications


In order to make Asp.Net MVC3 applications working under Mono 2.10.2, you have to copy additional files from your Windows development workstation into your project's [FILE]bin[/FILE] directory (these files will finally end up in [FILE]/home/asp/www/bin[/FILE] folder):
  • [FILE]c:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 3\Assemblies\System.Web.Mvc.dll[/FILE]
  • [FILE]c:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 3\Assemblies\System.Web.Mvc.xml[/FILE]
  • [FILE]c:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies\NuGet.Core.dll[/FILE]
  • [FILE]c:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies\System.Web.Helpers.dll[/FILE]
  • [FILE]c:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies\System.Web.Helpers.xml[/FILE]
  • [FILE]c:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies\System.Web.Razor.dll[/FILE]
  • [FILE]c:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies\System.Web.Razor.xml[/FILE]
  • [FILE]c:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies\System.Web.WebPages.dll[/FILE]
  • [FILE]c:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies\System.Web.WebPages.xml[/FILE]
  • [FILE]c:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies\System.Web.WebPages.Administration.dll[/FILE]
  • [FILE]c:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies\System.Web.WebPages.Deployment.dll[/FILE]
  • [FILE]c:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies\System.Web.WebPages.Deployment.xml[/FILE]
  • [FILE]c:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies\System.Web.WebPages.Razor.dll[/FILE]
  • [FILE]c:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies\System.Web.WebPages.Razor.xml[/FILE]
  • [FILE]c:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies\WebMatrix.Data.dll[/FILE]
  • [FILE]c:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies\WebMatrix.Data.xml[/FILE]
  • [FILE]c:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies\WebMatrix.WebData.dll[/FILE]
  • [FILE]c:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies\WebMatrix.WebData.xml[/FILE]


Also you have to delete [FILE]EntityFramework.xml[/FILE] and [FILE]EntityFramework.dll[/FILE] from [FILE]/home/asp/www/bin[/FILE] folder after you have copied your project there.

Please read the section "ASP.NET MVC3 Support" on Mono 2.10 Release Notes for the details.
vand777
Member
 
Posts: 292
Joined: 20 Jun 2010, 17:26
Location: London

Postby vand777 » 17 Aug 2011, 20:15

Should you have any problems when building or running mono, please do not forget to read [FILE]/usr/ports/lang/mono/pkg-message[/FILE] message. Maybe you will find the solution to your problem.

For example, this part might be important:
Code: Select all
* If you are in a jailed environment, ensure System V IPC are enabled.
  You can rely on the security.jail.sysvipc_allowed  sysctl to check
  this status.  The following enables this feature on the host system:
    # echo "jail_sysvipc_allow=\"YES\"" >> /etc/rc.conf


The above tutorial was tested in jail on FreeBSD 8.2p2, Mono 2.10.2.
vand777
Member
 
Posts: 292
Joined: 20 Jun 2010, 17:26
Location: London

Postby vand777 » 17 Aug 2011, 20:16

Reserved
vand777
Member
 
Posts: 292
Joined: 20 Jun 2010, 17:26
Location: London

Postby vand777 » 17 Aug 2011, 20:16

Reserved2
vand777
Member
 
Posts: 292
Joined: 20 Jun 2010, 17:26
Location: London

Postby Claud » 29 Nov 2011, 06:08

Hello,
Excelent tutorial thanks,
First, i don`t have bash as a default shell script fomr my user and it seems that the startup script needs some adjustemts or it will return bash pid olso , so the script that worked for me was:
Code: Select all
#!/usr/local/bin/bash
 
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr/local/bin:$HOME/bin:; export PATH
PID=`/bin/ps waux | /usr/bin/grep mono | /usr/bin/grep -v grep | /usr/bin/grep -v /bin/bash | /usr/bin/awk '{ print $2 }'`
 
if  ps waux | grep mono | grep -v grep | grep -v /bin/bash > /dev/null
 then
        echo "fastcgi-mono-server4 seems to run on PID $PID"
        exit
else
    echo "fastcgi-mono-server4 is not running. Starting it"
    /home/asp/fcgi
fi

Hope someone finds it useful.
Claud
Junior Member
 
Posts: 10
Joined: 08 Nov 2010, 10:52

Postby vand777 » 03 Dec 2011, 19:16

Thank you, Claud!
vand777
Member
 
Posts: 292
Joined: 20 Jun 2010, 17:26
Location: London

Postby wblock@ » 03 Dec 2011, 21:20

Claud wrote:
Code: Select all
...
PID=`/bin/ps waux | /usr/bin/grep mono | /usr/bin/grep -v grep | /usr/bin/grep -v /bin/bash | /usr/bin/awk '{ print $2 }'`
...


should be replaceable with

Code: Select all
PID=`/usr/bin/pgrep mono`
User avatar
wblock@
Old Fart
 
Posts: 10207
Joined: 07 Sep 2009, 23:23
Location: Milky Way galaxy


Return to Howtos & FAQs (Moderated)

Who is online

Users browsing this forum: No registered users and 1 guest