Running VS Code on Linux

Curtsy/ Original post: https://code.visualstudio.com/docs/setup/linux

Running VS Code on Linux

Installation

  1. Download Visual Studio Code for your distribution, .deb for Debian-based distributions such as Ubuntu or .rpm for Red Hat-based distributions such as Fedora or CentOS. Note that 32-bit binaries are also available on the download page.
  2. Install the package through a GUI package manager by double clicking on the package file, or through the command line:
 # For .deb
 sudo dpkg -i <file>.deb
 # install dependencies
 sudo apt-get install -f

 # For .rpm (Fedora 21 and below)
 sudo yum install <file>.rpm

 # For .rpm (Fedora 22 and above)
 sudo dnf install <file>.rpm
  1. VS Code should now be available to run through the launcher or the command line by running code.

Tip: Run code . in any folder to start editing files in that folder.

Note:

Visual Studio is tightly integrated with Windows and Developing a .NET application using any language (C# or VB) takes more than just having Wine, and since Wine is not capable enough to provide complete development runtime as .NET in Linux.

If you want to develop software specifically in C#, on Linux, you can use MonoDevelop

Since, you’re asking for Visual Studio 2010 (.NET 4.0), with MonoDevelop, you’ll not be able to develop an app that particularly uses .NET 4, as of now MonoDevelop is in version 3.0.2 (somewhat equivalent to .NET 3.0).

You can still use Windows virtually within Ubuntu, using VirtualBox. And then install Visual Studio there, but still a serious app development is not recommended to be done in Virtualized environment.

Download Visual Studio Code

https://code.visualstudio.com/download

http://www.monodevelop.com/download/

 

Running ASP.NET vNext on CentOS 7

Original post/ Curtsy: http://trydis.github.io/2015/01/06/running-aspvnext-centos7/

Running ASP.NET vNext on CentOS 7

For reference, here are the versions I used:

Mono: 3.10.0
KVM: Build 10017
KRE: 1.0.0-beta1
libuv: commit 3fd823ac60b04eb9cc90e9a5832d27e13f417f78

I created a new VM in Azure and used the image provided by OpenLogic. It contains an installation of the Basic Server packages.

Install Mono

Add the Mono Project GPG signing key:

$ sudo rpm --import "http://keyserver.ubuntu.com/pks/lookup?op=get&search=0x3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF"

Install yum utilities:

$ sudo yum install yum-utils

Add the Mono package repository:

$ sudo yum-config-manager --add-repo http://download.mono-project.com/repo/centos/

Install the mono-complete package:

$ sudo yum install mono-complete

Mono on Linux by default doesn’t trust any SSL certificates so you’ll get errors when accessing HTTPS resources. To import Mozilla’s list of trusted certificates and fix those errors, you need to run:

$ mozroots --import --sync

Install KVM

$ curl -sSL https://raw.githubusercontent.com/aspnet/Home/master/kvminstall.sh | sh && source ~/.kre/kvm/kvm.sh

Install the K Runtime Environment (KRE)

$ kvm upgrade

Running the samples

Install Git:

$ sudo yum install git

Clone the Home repository:

$ git clone https://github.com/aspnet/Home.git
$ cd Home/samples

Change directory to the folder of the sample you want to run.

ConsoleApp

Restore packages:

$ kpm restore

Run it:

$ k run
Hello World

That was easy!

HelloMvc

Restore packages:

$ kpm restore

Run it:

$ k kestrel
System.DllNotFoundException: libdl
(Removed big stack trace)

Ouch, so I went hunting for libdl:

$ sudo find / -name libdl*
/usr/lib64/libdl.so.2
/usr/lib64/libdl-2.17.so

Create symbolic link:

$ sudo ln -s /usr/lib64/libdl.so.2 /usr/lib64/libdl

Run it again:

$ k kestrel
System.NullReferenceException: Object reference not set to an instance of an object
  at Microsoft.AspNet.Server.Kestrel.Networking.Libuv.loop_size () [0x00000] in <filename unknown>:0
  at Microsoft.AspNet.Server.Kestrel.Networking.UvLoopHandle.Init (Microsoft.AspNet.Server.Kestrel.Networking.Libuv uv) [0x00000] in <filename unknown>:0
  at Microsoft.AspNet.Server.Kestrel.KestrelThread.ThreadStart (System.Object parameter) [0x00000] in <filename unknown>:0

Progress, but now we need to get libuv working.

Install libuv

$ sudo yum install gcc
$ sudo yum install automake
$ sudo yum install libtool
$ git clone https://github.com/libuv/libuv.git
$ cd libuv
$ sh autogen.sh
$ ./configure
$ make
$ make check
$ sudo make install

Run it again:

$ k kestrel
System.NullReferenceException: Object reference not set to an instance of an object
  at Microsoft.AspNet.Server.Kestrel.Networking.Libuv.loop_size () [0x00000] in <filename unknown>:0
  at Microsoft.AspNet.Server.Kestrel.Networking.UvLoopHandle.Init (Microsoft.AspNet.Server.Kestrel.Networking.Libuv uv) [0x00000] in <filename unknown>:0
  at Microsoft.AspNet.Server.Kestrel.KestrelThread.ThreadStart (System.Object parameter) [0x00000] in <filename unknown>:0

I knew it had to be a path issue or something, so I went hunting for libuv:

$ sudo find / -name libuv.so
/home/trydis/libuv/.libs/libuv.so
/usr/local/lib/libuv.so

I then checked the library name Kestrel was looking for on Linux here and based on that i created a symbolic link:

$ sudo ln -s /usr/local/lib/libuv.so /usr/lib64/libuv.so.1

Run it again:

$ k kestrel
Started

Navigate to http://your-web-server-address:5004/ and pat yourself on the back!

HelloWeb

Restore packages:

$ kpm restore

Run it:

$ k kestrel
Started

Navigate to http://your-web-server-address:5004/.

 

Mono Project

http://www.mono-project.com/docs/web/aspnet/

ASP.NET

Mono has an implementation of ASP.NET 2.0, ASP.NET MVC and ASP.NET AJAX.

Quick Resources:

Mono’s ASP.NET implementations supports two kinds of applications:

  • Web Forms (Web Applications infrastructure).
  • Web Services (the SOAP-based RPC system).

Status and tests for ASP.NET 2.0 are available in our ASPTests page.

Running ASP.NET applications

To run your ASP.NET applications with Mono, you have three classes of options:

  • Apache hosting: use mod_mono, a module that allows Apache to serve ASP.NET applications.
  • FastCGI hosting: use the FastCGI hosting if you have a web server that supports the FastCGI protocol (for example Nginx) for extending the server. You also may use a web server that only has support for CGI using cgi-fcgi.
  • XSP: this is a simple way to get started, a lightweight and simple webserver written in C#.

For deploying applications, we recommend the use of the mod_mono or FastCGI approaches, as that will give you all the configuration options and flexibility that come with using Apache or a FastCGI server.

For quickly getting started, get familiar with Mono and ASP.NET, XSP is the ideal solution. Keep in mind that XSP is a very limited server and is only useful to get acquainted with ASP.NET and Mono, it only support HTTP 1.0 and does not provide much extensibility or configuration.

More advaned users can use the HttpListener and the ASP.NET hosting to create their own hosts for ASP.NET applications.

ASP.NET hosting with Apache

The mod_mono Apache module is used to run ASP.NET applications within the Apache web server.

The mod_mono module runs within an Apache process and passes all the requests to ASP.NET applications to an external Mono process that actually hosts your ASP.NET applications. The external ASP.NET host is called “mod-mono-server” and is part of the XSP module.

To use this, you must download and install the mod_mono and xsp components of Mono. mod_mono contains the actual Apache module, and xsp contains the actual ASP.NET hosting engine, both are available from our download page.

See the mod_mono page for details on installation and configuration.

ASP.NET hosting with Nginx

Nginx is a high-performance HTTP server which support running ASP.NET and ASP.NET MVC web applications through FastCGI protocol. See the FastCGI Nginx page for details on installation and configuration.

ASP.NET hosting with XSP

XSP is a standalone web server written in C# that can be used to run your ASP.NET applications with minimal effort. XSP works under both the Mono and Microsoft runtimes. The code is available from our download page (look for XSP web server) or from the git repository (module name: xsp).

The easiest way to start XSP is to run it from within the root directory of your application. It will serve requests on port 8080. Place additional assemblies in the bin directory. Other XSP options can be set on the command line, such as the application directory and the port to listen on.

XSP comes with a set of pages, controls and web services that you can use to test the server and see what ASP.NET looks like.

For example, once you install XSP, you can try some samples like this:

 $ cd /usr/lib/xsp/test
 $ xsp
 Listening on port: 8080
 Listening on address: 0.0.0.0
 Root directory: /home/cvs/mcs/class/corlib/Microsoft.Win32
 Hit Return to stop the server.

You can now browse to http://localhost:8080 and see various sample programs

SSL support in XSP

XSP supports SSL and TLS Client Certificates. For further details about setting it up, see the UsingClientCertificatesWithXSP document.

Configuration

Applications can be configured through the web.config file, the full documentation is available from MSDN, and also a Mono-specific version is available on this site here.

Additionally, you can configure Mono-specific ASP.NET settings (to have applications that behave differently depending on the operating system they are deployed in) using the ASP.NET Settings Mapping engine.

Other extensions

Check out ASP.NET Modules for details on how to support deflate/gzip encodings and authentication.

Debugging

By default xsp and xsp2 run in Release mode, which means that debugging line-number information will not be available in stack traces when errors occur.

To obtain line numbers in stack traces you need to do two things:

  1. Enable Debug code generation in your page. 2. Run Mono with the –debug command line option.

You must enable debug code generation in your page using the Debug=”true” in the top of your page, or setting the compilation flag in Web.config (compilation option).

Use the –debug command line option to Mono, this is done by setting the MONO_OPTIONS environment variable, like this:

$ MONO_OPTIONS=--debug xsp2
Listening on port: 8080 (non-secure)
Listening on address: 0.0.0.0
Root directory: /tmp/us
Hit Return to stop the server.

To do the same with the Apache mod_mono module, use the MonoDebug true directive in your apache configuration file.

Supported Versions

Mono supports ASP.NET 2.0, ASP.NET AJAX and a handful of 3.5 controls.

Limitations

Mono’s ASP.NET does not implement the following features:

  • Precompiled updatable web sites.
  • WebParts APIs.

Work in Progress

git access

Users interested in the latest version of mod_mono and xsp can retrieve these from our public git repository. The module names are mod_mono and xsp respectively. You will also need to check out the mcs module as the System.Web classes are in mcs/class/System.Web.

Designer

There is work in progress on an ASP.NET Designer the designer will eventually be integrated into the MonoDevelop IDE.

ASP.NET Apache setup on CentOS

Original Post/ Curtsy: http://jharitesh.blogspot.com/2012/06/aspnet-apache-setup-on-centos.html

To run ASP.NET application on Apache mod_mono module should be installed and configured on Apache.
All .NET pages are actually processed by mono which runs in background along with Apache. Apache identify .Net request page with help of mod_mono module. I used centOS as OS and performed following steps to run ASP.NET application on Apache.

  • Mono installation
  • XSP installation
  • Apache installation
  • mod_mono installation
  • Configuration

Mono installation
1. yum install gcc bison pkgconfig glib2-devel gettext make

2. Please check latest mono version on site before installation and edit command with latest version.   wget http://ftp.novell.com/pub/mono/sources/mono/mono-2.10.2.tar.bz2

3. tar jxvf mono-2.10.2.tar.bz2

4. cd mono-2.10.2

5. ./configure -prefix=/opt/mono; make; make install

6. echo export PKG_CONFIG_PATH=/opt/mono/lib/pkgconfig:$PKG_CONFIG_PATH>>~/.bash_profile

7. echo export PATH=/opt/mono/bin:$PATH>>~/.bash_profile

8. source ~/.bash_profile

9. mono -V

to check mono is installed or not

10. cd ..
XSP installation

1. wget http://ftp.novell.com/pub/mono/sources/xsp/xsp-2.10.2.tar.bz2 2. tar jxvf xsp-2.10.2.tar.bz2 3. cd xsp-2.10.2 4. ./configure -prefix=/opt/mono; make; make install 5. cd ..

Apache installation

  1. yum -y install httpd-devel

mod_mono installation  

1. wget http://ftp.novell.com/pub/mono/sources/mod_mono/mod_mono-2.10.tar.bz2 2. tar jxvf mod_mono-2.10.tar.bz2 3.  cd  mod_mono-2.10 4. ./configure –prefix=/opt/mono; make; make install 5. cd ..

 Configuration

  1. mono_setup configuration: create mono_setup.conf under directory /etc/httpd/conf.d with below content.

Include /etc/httpd/conf/mod_mono.conf

MonoApplications “/:/var/www/html/”

MonoServerPath “/opt/mono/bin/mod-mono-server2”

 

Options FollowSymLinks

AllowOverride None

AddHandler mono .aspx .ascx .asax .ashx .config .cs .asmx .axd

 

  1. Apache configuration: 

1) Open /etc/httpd/conf/httpd.conf and uncomment below line(remove ‘#’).

NameVirtualHost *:80

Extra setting:

I like Ubuntu way to enable disable site so I added this in here too.

2) Add below line

Include /etc/httpd/sites-enabled/

3) Create two directory inside /etc/httpd using below command

mkdir /etc/httpd/sites-enabled
mkdir /etc/httpd/sites-available

4) Type following to create new file a2ensite
    1) cat > a2ensite
2) Type this line
ln -s /etc/httpd/sites-available/$1.conf   /etc/httpd/sites-enabled/$1.conf

3) Press enter to move cursor to a new line and press CTRL+D.
4) Type this  command to make a2ensite executable
chmod   +x   a2ensite
5) Copy into /usr/bin
cp a2ensite /usr/bin

Type following to create new file a2dissite
    1) cat > a2dissite
2) Type this line
rm       -f          /etc/httpd/sites-enabled/$1.conf

3) Press enter to move cursor to a new line and press CTRL+D.
4) Type this  command to make a2ensite executable
chmod   +x   a2dissite
5) Copy into /usr/bin
cp          a2dissite         /usr/bin

3.Config application

Each application will have own separate config in directory  /etc/httpd/sites-available
sample application config file: file path is /etc/httpd/sites-available/name.xyz.com

    ServerAdmin xxxx.yyy@name.xyz.com

DocumentRoot /var/www/name.xyz.com

DirectoryIndex Default.aspx

ServerName name.xyz.com

ServerPath /

MonoAutoApplication disabled

MonoApplications “/:/var/www/name.xyz.com”

MonoServerPath “/opt/mono/bin/mod-mono-server2”

AddHandler mono .aspx .ascx .asax .ashx .config .cs .asmx .axd

ErrorLog /var/log/httpd/ name.xyz.com-error_log

CustomLog /var/log/httpd/ name.xyz.com-access_log common

 

In sample config application root directory is var/www/name.xyz.com i.e all application files will be kept in folder name.xyz.com.

4. Deployment 
    1) create directory /var/www/name.xyz.com
2) Copy application folder content into directory /var/www/name.xyz.com
3) Type below command to enable web-application in apache

#a2ensite name.xyz.com

#service apache2 restart
4)  Type below command to disable web-application in apache.
#a2dissite name.xyz.com

#service apache2 restart
Note: file name should be same as what we create in /etc/apache2/sites-available.

5) Other useful commands
To start apache  #service httpd start
To restart apache #service httpd restart
To stop apache #service httpd stop
To check mono is running in background or not  #ps -ef |grep -i “mono”

I also came across few issues during setup and got good solution from other forums. Here are collectively all issues.

  1. After deployment & restart of apache when we try to access page ….it is shown as blank page.

Apache error log contains following errors:

[crit] (2)No such file or directory: Failed to create shared memory segment for backend ‘testing’ at ‘/var/run/mod_mono/mod_mono_dashboard_testing_2’.

It is actually thrown because it is failing to create shared memory. Mono run as user apache but only root can write to /var/run. So solution is to create a folder and make apache owner.

  1. After solving 1st error when we try to access page from browser it throws error “Service Temporarily Unavailable”. In apache error log below error is shown.

” [error] Failed to connect to mod-mono-server after several attempts to spawn the process.”

solution: This is because of SELinux. By disabling SELinux using following command this issue can be resolved.

# setenforce 0

Script for above two issue is as follows.

dir=”/var/run/mod_mono/”

echo “Disable SELinux”
setenforce 0
echo -n “SELinux status is ”
/usr/sbin/getenforce
echo “Stopping Apache”
apachectl stop
echo “Waiting 5 seconds”
sleep 5s
shopt -s dotglob
if [ -d “$dir” ]; then
echo “Removing $dir”
rm -rf “$dir”*
rmdir “$dir”
fi
echo “Creating $dir”
mkdir “$dir”
echo “Changing owernerhip of $dir to Apache”
chown apache “$dir”
chgrp apache “$dir”

chown             -R apache   /opt/mono/
mkdir   -p        ~apache/.mono
chown  -R        apache:apache    ~apache/.mono
chmod             u+rw    ~apache/.mono
echo “Starting Apache”
apachectl start
echo “Done!”

Above script will run only when system start or reboot.

  1. Error message “Make sure that your mod_mono module is loaded after the User/Group directives” is thrown when we start/restart apache. [1]

soln : Put below statement just above “Include conf.d/*.conf” in httpd.conf

User apache
Group apache

and then restart apache httpd service.

  1. Error message ” Starting httpd: httpd: Could not reliably determine the server’s fully qualified domain name, using localhost.localdomain for ServerName”

Do following
1) Edit /etc/hosts using your favourite editor The contents should look something like this: # Do not remove the following line, or various programs # that require network functionality will fail. 127.0.0.1 localhost.localdomain localhost ::1 localhost6.localdomain6 localhost6
2).
At the end of the file add: 192.168.20.100 Server1.example.com That’s a [tab] between the IP address and the host name.
3) Save the file
4) Restart the network services to apply your changes service network restart
5)
Restart Apache service httpd restart

  1. Error message “Auto generated encryption keys not saved: System.Security.SecurityException: No access to the given key”

Reason :ASP.NET is creating auto generated keys for the machine and failing to safe them. The only problem, other that seen that message, is that every time the application is started it will get new keys because they are not being saved.

The directory where this is trying to write is somewhere $SYSCONFDIR/mono/registry (usually /etc/mono/registry). If it allow the user running apache read, write, and execute permissions on that folder, Mono will be able to save the auto generated keys and will reuse them subsequently. Once the keys are written, the apache user will only need read and execute permission.

Solution:
chown             -R apache /opt/mono/
mkdir   -p        ~apache/.mono
chown  -R        apache:apache  ~apache/.mono
chmod             u+rw  ~apache/.mono

The auto-generated keys are now being stored at: “/var/www/.mono/registry/”

Setting up ASP.NET on CentOS 6.x

Original post/ curtsy: https://jefferytay.wordpress.com/2015/08/23/setting-up-asp-net-on-centos-6-x/

With vNext, we are now finally able to run asp.net on Linux!

 

This is done via the MONO framework, which has been around for ages. However as it is with bleeding edge technologies, the setup can be rather cumbersome.

 

Here is the script that you can use to install the latest version of everything and set it up nicely on a minimal setup CentOS box (which is what most servers typically use).

Full source can be found at http://pastebin.com/aTxQne2n

 

##################################################################
# For minimal setup of CentOS 6.5/6.6
##################################################################

##################################################################
# Basic Server essentials
##################################################################
yum -y update
yum -y install yum-utils epel-release
yum clean all && yum makecache
yum -y groupinstall ‘Development tools’

##################################################################
# Install Mono (Latest Version)
##################################################################
yum -y install git autoconf libtool automake build-essential gettext
git clone git://github.com/mono/mono.git
cd mono
./autogen.sh –prefix=/usr/local
make get-monolite-latest
make EXTERNAL_MCS=”${PWD}/mcs/class/lib/monolite/gmcs.exe
make
make install

##################################################################
# Install LibUV (Latest Version)
##################################################################
yum -y install gcc automake libtool
git clone https://github.com/libuv/libuv.git
cd libuv
sh autogen.sh
./configure
make
make install
ldconfig

##################################################################
# Install Node (Latest Version)
##################################################################
yum remove -y nodejs npm
curl -sL https://rpm.nodesource.com/setup | bash –
yum install -y nodejs

##################################################################
# Install Yeoman and Generator Support (Latest Version)
##################################################################
yum -y install npm

#update npm to latest
npm install -g npm@latest
npm install -g yo

#install Generator support
npm install -g generator-aspnet

##################################################################
# Install DNVM (Latest Version)
##################################################################
yum -y install unzip
curl -sSL https://raw.githubusercontent.com/aspnet/Home/dev/dnvminstall.sh | DNX_BRANCH=dev sh
source ~/.dnx/dnvm/dnvm.sh

##################################################################
# Update Mozilla Root Server Certificates
##################################################################
mozroots –import –sync

## for dnu restore to work more reliably, increase concurrent threads
echo -e ‘\r\n## Enable greater Mono concurrency (helps dnu restore)\r\nexport MONO_THREADS_PER_CPU=2000’ >> ~/.bashrc

## dnx needs this to load libuv on CentOS (why not on Ubuntu?)
echo -e ‘\r\n## dnx needs this to load libuv on CentOS (why not on Ubuntu?)\r\nexport LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH’ >> ~/.bashrc

## done – print final instructions
echo -e ‘\r\nDone installing ASP.NET 5 prerequisites and DNVM.\r\nOpen new shell environment\r\nor\r\n$ source .bashrc to keep using this one.\r\n’

How to read the response stream before the Http response completes

class Program
    {
        static void Main(string[] args)
        {
            HttpWebRequest req;
            HttpWebResponse res = null;

            try
            {
                req = (HttpWebRequest)WebRequest.Create(
                        "http://cdimage.debian.org/debian-cd/5.0.4/i386/iso-cd/debian-504-i386-CD-1.iso");
                res = (HttpWebResponse)req.GetResponse();
                Stream stream = res.GetResponseStream();

                byte[] data = new byte[4096];
                int read;
                while ((read = stream.Read(data, 0, data.Length)) > 0)
                {
                    Process(data, read);
                }
            }
            finally
            {
                if (res != null)
                    res.Close();
            }
            Console.In.Read();
        }

        private static void Process(byte[] data, int read)
        {
            Console.Out.Write(ASCIIEncoding.ASCII.GetString(data));
        }
    }
}

Download/Stream file from URL – Asp.net and C#

 //Create a stream for the file
    Stream stream = null;

    //This controls how many bytes to read at a time and send to the client
    int bytesToRead = 10000;

    // Buffer to read bytes in chunk size specified above
    byte[] buffer = new Byte[bytesToRead];

    // The number of bytes read
    try
    {
      //Create a WebRequest to get the file
      HttpWebRequest fileReq = (HttpWebRequest) HttpWebRequest.Create(url);

      //Create a response for this request
      HttpWebResponse fileResp = (HttpWebResponse) fileReq.GetResponse();

      if (fileReq.ContentLength > 0)
        fileResp.ContentLength = fileReq.ContentLength;

        //Get the Stream returned from the response
        stream = fileResp.GetResponseStream();

        // prepare the response to the client. resp is the client Response
        var resp = HttpContext.Current.Response;

        //Indicate the type of data being sent
        resp.ContentType = "application/octet-stream";

        //Name the file 
        resp.AddHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
        resp.AddHeader("Content-Length", fileResp.ContentLength.ToString());

        int length;
        do
        {
            // Verify that the client is connected.
            if (resp.IsClientConnected)
            {
                // Read data into the buffer.
                length = stream.Read(buffer, 0, bytesToRead);

                // and write it out to the response's output stream
                resp.OutputStream.Write(buffer, 0, length);

                // Flush the data
                resp.Flush();

                //Clear the buffer
                buffer = new Byte[bytesToRead];
            }
            else
            {
                // cancel the download if client has disconnected
                length = -1;
            }
        } while (length > 0); //Repeat until no data is read
    }
    finally
    {
        if (stream != null)
        {
            //Close the input stream
            stream.Close();
        }
    }

ASP.NET Apache setup on CentOS

Original Post: http://jharitesh.blogspot.com/2012/06/aspnet-apache-setup-on-centos.html

ASP.NET Apache setup on CentOS

To run ASP.NET application on Apache mod_mono module should be installed and configured on Apache.
All .NET pages are actually processed by mono which runs in background along with Apache. Apache identify .Net request page with help of mod_mono module. I used centOS as OS and performed following steps to run ASP.NET application on Apache.

  • Mono installation
  • XSP installation
  • Apache installation
  • mod_mono installation
  • Configuration

Mono installation

  1. yum install gcc bison pkgconfig glib2-devel gettext make 2. Please check latest mono version on site before installation and edit command with latest version.  wget http://ftp.novell.com/pub/mono/sources/mono/mono-2.10.2.tar.bz2 3. tar jxvf mono-2.10.2.tar.bz2 4. cd mono-2.10.2 5. ./configure -prefix=/opt/mono; make; make install 6. echo export PKG_CONFIG_PATH=/opt/mono/lib/pkgconfig:$PKG_CONFIG_PATH>>~/.bash_profile 7. echo export PATH=/opt/mono/bin:$PATH>>~/.bash_profile 8. source ~/.bash_profile 9. mono -V

to check mono is installed or not

  1. cd ..

XSP installation

  1. wget http://ftp.novell.com/pub/mono/sources/xsp/xsp-2.10.2.tar.bz2 2. tar jxvf xsp-2.10.2.tar.bz2 3. cd xsp-2.10.2 4. ./configure -prefix=/opt/mono; make; make install 5. cd ..

Apache installation

  1. yum -y install httpd-devel

mod_mono installation  

  1. wget http://ftp.novell.com/pub/mono/sources/mod_mono/mod_mono-2.10.tar.bz2 2. tar jxvf mod_mono-2.10.tar.bz2 3.  cd  mod_mono-2.10 4. ./configure –prefix=/opt/mono; make; make install 5. cd ..

 Configuration

  1. mono_setup configuration: create mono_setup.conf under directory /etc/httpd/conf.d with below content.

Include /etc/httpd/conf/mod_mono.conf

MonoApplications “/:/var/www/html/”

MonoServerPath “/opt/mono/bin/mod-mono-server2”

Options FollowSymLinks

AllowOverride None

AddHandler mono .aspx .ascx .asax .ashx .config .cs .asmx .axd

  1. Apache configuration: 

1) Open /etc/httpd/conf/httpd.conf and uncomment below line(remove ‘#’).

NameVirtualHost *:80

Extra setting:

I like Ubuntu way to enable disable site so I added this in here too.

2) Add below line

Include /etc/httpd/sites-enabled/

3) Create two directory inside /etc/httpd using below command

mkdir /etc/httpd/sites-enabled
mkdir /etc/httpd/sites-available

4) Type following to create new file a2ensite
    1) cat > a2ensite
2) Type this line
ln -s /etc/httpd/sites-available/$1.conf   /etc/httpd/sites-enabled/$1.conf

3) Press enter to move cursor to a new line and press CTRL+D.
4) Type this  command to make a2ensite executable
chmod   +x   a2ensite
5) Copy into /usr/bin
cp a2ensite /usr/bin

Type following to create new file a2dissite
    1) cat > a2dissite
2) Type this line
rm       -f          /etc/httpd/sites-enabled/$1.conf

3) Press enter to move cursor to a new line and press CTRL+D.
4) Type this  command to make a2ensite executable
chmod   +x   a2dissite
5) Copy into /usr/bin
cp          a2dissite         /usr/bin

3.Config application

Each application will have own separate config in directory  /etc/httpd/sites-available
sample application config file: file path is /etc/httpd/sites-available/name.xyz.com

    ServerAdmin xxxx.yyy@name.xyz.com

DocumentRoot /var/www/name.xyz.com

DirectoryIndex Default.aspx

ServerName name.xyz.com

ServerPath /

MonoAutoApplication disabled

MonoApplications “/:/var/www/name.xyz.com”

MonoServerPath “/opt/mono/bin/mod-mono-server2”

AddHandler mono .aspx .ascx .asax .ashx .config .cs .asmx .axd

ErrorLog /var/log/httpd/ name.xyz.com-error_log

CustomLog /var/log/httpd/ name.xyz.com-access_log common

 

In sample config application root directory is var/www/name.xyz.com i.e all application files will be kept in folder name.xyz.com.

4. Deployment 
    1) create directory /var/www/name.xyz.com
2) Copy application folder content into directory /var/www/name.xyz.com
3) Type below command to enable web-application in apache

#a2ensite name.xyz.com

#service apache2 restart
4)  Type below command to disable web-application in apache.
#a2dissite name.xyz.com

#service apache2 restart
Note: file name should be same as what we create in /etc/apache2/sites-available.

5) Other useful commands
To start apache  #service httpd start
To restart apache #service httpd restart
To stop apache #service httpd stop
To check mono is running in background or not  #ps -ef |grep -i “mono”

I also came across few issues during setup and got good solution from other forums. Here are collectively all issues.

  1. After deployment & restart of apache when we try to access page ….it is shown as blank page.

Apache error log contains following errors:

[crit] (2)No such file or directory: Failed to create shared memory segment for backend ‘testing’ at ‘/var/run/mod_mono/mod_mono_dashboard_testing_2’.

It is actually thrown because it is failing to create shared memory. Mono run as user apache but only root can write to /var/run. So solution is to create a folder and make apache owner.

  1. After solving 1st error when we try to access page from browser it throws error “Service Temporarily Unavailable”. In apache error log below error is shown.

” [error] Failed to connect to mod-mono-server after several attempts to spawn the process.”

solution: This is because of SELinux. By disabling SELinux using following command this issue can be resolved.

# setenforce 0

Script for above two issue is as follows.

dir=”/var/run/mod_mono/”

echo “Disable SELinux”
setenforce 0
echo -n “SELinux status is ”
/usr/sbin/getenforce
echo “Stopping Apache”
apachectl stop
echo “Waiting 5 seconds”
sleep 5s
shopt -s dotglob
if [ -d “$dir” ]; then
echo “Removing $dir”
rm -rf “$dir”*
rmdir “$dir”
fi
echo “Creating $dir”
mkdir “$dir”
echo “Changing owernerhip of $dir to Apache”
chown apache “$dir”
chgrp apache “$dir”

chown             -R apache   /opt/mono/
mkdir   -p        ~apache/.mono
chown  -R        apache:apache    ~apache/.mono
chmod             u+rw    ~apache/.mono
echo “Starting Apache”
apachectl start
echo “Done!”

Above script will run only when system start or reboot.

  1. Error message “Make sure that your mod_mono module is loaded after the User/Group directives” is thrown when we start/restart apache. [1]

soln : Put below statement just above “Include conf.d/*.conf” in httpd.conf

User apache
Group apache

and then restart apache httpd service.

  1. Error message ” Starting httpd: httpd: Could not reliably determine the server’s fully qualified domain name, using localhost.localdomain for ServerName”

Do following
1) Edit /etc/hosts using your favourite editor The contents should look something like this: # Do not remove the following line, or various programs # that require network functionality will fail. 127.0.0.1 localhost.localdomain localhost ::1 localhost6.localdomain6 localhost6
2).
At the end of the file add: 192.168.20.100 Server1.example.com That’s a [tab] between the IP address and the host name.
3) Save the file
4) Restart the network services to apply your changes service network restart
5)
Restart Apache service httpd restart

  1. Error message “Auto generated encryption keys not saved: System.Security.SecurityException: No access to the given key”

Reason :ASP.NET is creating auto generated keys for the machine and failing to safe them. The only problem, other that seen that message, is that every time the application is started it will get new keys because they are not being saved.

The directory where this is trying to write is somewhere $SYSCONFDIR/mono/registry (usually /etc/mono/registry). If it allow the user running apache read, write, and execute permissions on that folder, Mono will be able to save the auto generated keys and will reuse them subsequently. Once the keys are written, the apache user will only need read and execute permission.

Solution:
chown             -R apache /opt/mono/
mkdir   -p        ~apache/.mono
chown  -R        apache:apache  ~apache/.mono
chmod             u+rw  ~apache/.mono

The auto-generated keys are now being stored at: “/var/www/.mono/registry/”

Introduction to Mono – ASP.NET with XSP and Apache

Original Post: http://www.codeproject.com/Articles/9738/Introduction-to-Mono-ASP-NET-with-XSP-and-Apache

Introduction

This article is the second article in the series of introductory articles that I am writing about Mono, the “open source development platform based on the .NET framework”. In this article we’ll take a look at how to get going with ASP.NET on the Mono platform. Although ASP.NET is not part of the ECMA and ISO standards mentioned in the first article[^], it is still one of the major selling points of the .NET platform and provides an extremely flexible and powerful platform for developing web applications and Web Services upon. Although you can develop ASP.NET applications for Mono on a number of different operating systems this article will focus mainly on Linux, although, in saying that, I do look briefly at getting XSP running on Windows. The reason I will concentrate on ASP.NET on Linux is because those people interested in ASP.NET on Windows have an extremely powerful option at their fingertips in the form IIS and I would whole heartedly recommend using it for ASP.NET on Windows.

Where does ASP.NET stand with Mono??

The latest stable version of Mono, version 1.0.5, has a fully functional implementation of ASP.NET. This includes full support for ASP.NET Web Forms and Web Services. This essentially means that more or less any ASP.NET application that you have developed using with the .NET Framework will work with Mono. Obviously there might be changes needed, such as data access changes, removal of any reliance on .NET Framework BCL types that are not implemented in Mono yet, and also the removal of any code that makes use of platform invoke and so on. At this stage the ASP.NET support in Mono can be considered as excellent and a lot of publicly available web applications already make use of Mono’s ASP.NET support. According to the Mono site the Mono Web Services stack is being used in the source control application Vault[^] by SourceGear and aspects of Mono’s ASP.NET implementation are also used in the Virtuoso[^] product from OpenLink.

What are XSP and mod_mono?

There isn’t much point in developing web applications and Web Services if you have no way of serving them, is there? Traditionally you would use IIS to host ASP.NET applications on Windows, although there are some other free ASP.NET web servers available such as Cassini[^]. However, when using ASP.NET with Mono you have two main options as regards which web server to host your ASP.NET applications in:

  • XSP
  • Apache

In this article we look at using both XSP (on Windows and Linux) and Apache to host your ASP.NET web applications and Web Services.

XSP

XSP is a “light-weight web server” capable of hosting and serving ASP.NET applications. It is written using C#, making extensive use of the classes in the System.Web namespace, and the source for XSP is available for download from the Mono download page[^] . It is also interesting to note that the XSP web server runs on both the Mono runtime and also on the .NET runtime which means that if you are looking for a light-weight ASP.NET web server for Windows but you don’t want to use or you don’t care about Mono, you could still make use of XSP. The Let’s get XSP up and running section runs through how to install and use XSP on Windows and Linux.

Apache

Apache is probably the de facto standard web server used on Linux. Apache makes extensive use of modules to enable it to host and serve web applications developed in a multitude of different web programming languages and scripts. Those of you familiar with IIS but not so familiar with Apache can think of these modules as the equivalent of ISAPI extensions. So, by this stage I assume you can guess how Apache can host and serve ASP.NET application? Yep, using an Apache module. This module, called mod_mono, allows Apache to serve ASP.NET pages “by proxying the requests to a slightly modified version of XSP called mod-mono-server”. mod-mono-server is installed when you install XSP. At the time of writing the current version of the mod_mono module for Apache only works for Apache on Linux and not for Apache on Windows. For this reason, when we look at ASP.NET on Apache in the Apache with mod_mono section we only look at it in the context of Linux. If you do wish to serve ASP.NET content from Apache on Windows have a look at this article[^] on CP, which shows how to use configure Apache to use Cassini to serve ASP.NET content or have a look at the Apache HTTP CLI[^] project.

What you need to know

This article assumes that you have Mono installed and working on your desired platform. The first article in this series, Introduction to Mono – Your first Mono app[^], explains how to get Mono up and running on Windows and Linux so if you have not yet done so have a look over that article. I also assume that you are familiar with ASP.NET and the semantics of ASP.NET programming as this article primarily looks at getting ASP.NET working with XSP and Apache, not at ASP.NET programming.

Lets get XSP up and running

In this section we will look at getting XSP up and running on both Windows and on Linux. Installing and running on Windows is “tackled” first simply because I want to get it out of the way and because chances are that you already have XSP on Windows considering the fact that you should have Mono already installed. Then it’s on to Linux and getting XSP up and running there. To be honest, if you already have Mono running on Linux, which you should as it’s one of the prerequisites for this article, then it should be relatively easy.

XSP on Windows

The first article[^] in this series ran through installing Mono on Windows. It mentioned that during the install process the installer gives you the option to select which components to install. By default, XSP is installed along with Mono but if you decided to not install XSP during the install then the easiest way to get XSP installed is to uninstall Mono and then reinstall it again. Here are the steps explaining how to install Mono and XSP on a Windows 2000 or above machine:

  1. Download the Mono Windows Installer from the Mono download page. The file I am working off is http://www.go-mono.com/archive/1.0.5/windows/mono-1.0.5-gtksharp-1.0.5-win32-0.1.exe
  2. The installer is a standard Windows installer so you can simply run it once it is downloaded by doubling clicking on it.
  3. Once the installer begins, click on next to go to the License Agreement page.
  4. You’ll probably notice on the License Agreement page that there are a number of different Licenses that apply to the different components of the install. While I understand that most people simply agree and click on next, it would be irresponsible of me if I didn’t recommend that you read the License Agreement before agreeing to it.
  5. Once you pass the License Agreement section you are presented with some general information about the install.
  6. Click on Next and select the install location.
  7. Click on Next again to bring you to the Select Components page. You are free to customize your installation but make sure that you install XSP. As I will be using all the components of this install in other articles in this series I would suggest that you accept the default “Full installation”.
  8. Click on Next again and you can customize the name of Mono folder that appears in the Start Menu.
  9. The installer will now ask you which port you want the XSP web server to listen on. Generally this will be port 8088 but you can change it to any free port on your system. When you are ready click on Next.
  10. Finally, click on Install to install and configure Mono and XSP on your Windows machine.

Those of you who read the first article will notice that I more or less ripped the instructions from that one… I’m not lazy, just no point in rewriting what has already been said! The next step in using XSP on Windows is to run it. The Running XSP section explains how to run the XSP web server and looks at some of the command line options that you can use to alter it’s default behaviour.

XSP on Linux

As with installing Mono on Linux, which we examined in the previous article, things are slightly more complicated on Linux then on Windows. However, as I already mentioned, if you have Mono installed, then getting XSP installed is going to be a breeze for you. I’m installing XSP on the same systems that I used for installing Mono in the previous article. A quick recap of what exactly I used is in order so: I used Microsoft Virtual PC 2004 and created two identical virtual PCs. I then installed SuSE 9.2 Professional with ACPI disabled and I used the default package selection when installing. In the last article I went through how to install Mono using the pre-built packages from the Mono site and also I ran through how to install Mono from source. So for the purposes of this article I will be installing XSP on a SuSE 9.2 Professional with the default package selection with Mono 1.0.5 installed along with any additional packages or programs that were installed during the Mono install.

In this article we will again look at installing from both the pre-build packages and from the source.

XSP using pre-built packages

As I am sure you can remember from the first article (if you read it that is), installing using the pre-built packages from the Mono site is one of the easiest ways of getting Mono up and running. The same thing applies to getting XSP up and running. So, head on over to the Mono download page (http://www.mono-project.com/downloads/index.html[^]) and see if there are pre-built packages for your particular Linux distribution or if there are packages for a distribution that you know will be compatible with your distribution. If there isn’t a package that will work with your distribution just jump ahead to the XSP from source section where I will run through installing XSP from the source which is a simple process.

Lets get going:

  1. On the Mono download page, click on the “Packages” link beside the Linux distribution that you believe to be compatible with your distribution. I simply selected the link beside SuSE 9.2
  2. This should bring you to a page which lists a lot of different packages that have been compiled and packaged for that particular Linux distribution.
  3. Before we install XSP we need to install two other pre-built packages. These packages include files that XSP is dependent upon.
  4. The first package we need to install is the “Database Core” package that can be found in the database section. The exact name of the package will vary depending on the packages link that you selected initially but in general the name will start with “mono-data”. The package I downloaded for SuSE 9.2 was http://www.go-mono.com/archive/1.0.5/suse-92-i586/mono-data-1.0.5-1.ximian.10.4.i586.rpm.
  5. Once you have downloaded this file, open it and it will launch the package manager for your distribution. At this stage simply follow the instructions to install the package.
  6. After you have installed the “mono-data” package you now need to download the “mono-web” package which will be listed under the “Web Applications, Web Services” section. The package I downloaded for SuSE 9.2 was http://www.go-mono.com/archive/1.0.5/suse-92-i586/mono-web-1.0.5-1.ximian.10.4.i586.rpm.
  7. Next, as with the “mono-data” package, simply open the package and follow the instructions to install it using the package manager of your distribution.
  8. Finally we are ready to install XSP itself. So just head back to the “Web Applications, Web Services” section of the package download page and download the “xsp” package. The package I got was http://www.go-mono.com/archive/1.0.5/suse-92-i586/xsp-1.0.5-1.ximian.10.1.i586.rpm.
  9. As with the two previous packages, simply open it and follow the instructions of your package manager to install XSP.
  10. All going well, you should have XSP installed now!

At this stage XSP should be installed on your system so you can jump ahead to the Running XSP section which gives an overview of how to use XSP. If you are having difficulty installing XSP from the packages have a look at the next section, “XSP from source”, which might be able to help you out.

XSP from source

Installing XSP from the source should be breeze if you have installed Mono from source by following the instructions in the previous article[^]. Let’s jump straight in:

  1. The first thing you need to do is download the source. Once again, head on over to the Mono download page at http://www.mono-project.com/downloads/index.html[^].
  2. In the source code section click on and download the “XSP web server”. I downloaded http://www.go-mono.com/archive/1.0.5/xsp-1.0.5.tar.gz.
  3. Once the file has been downloaded, go to a console for the remainder of the process
  4. You can decompress the downloaded file using the command tar -xvzf xsp-1.0.5.tar.gz where xsp-1.0.5.tar.gz is the name of the file you downloaded.
  5. Next, switch to the directory where the source code has been decompressed to by typing cd xsp-1.0.5 or cd followed by the name of the directory that you decompressed the file to if it is different from mine.
  6. Now, before we can compile the source you need to configure the make files. To do this simply type ./configure --prefix=/usr. This will take a bit of time. I am assuming here that there were no errors and that the ./configure --prefix=/usr went ahead without issue.
  7. When the the ./configure --prefix=/usr completes you are free to go ahead and compile and install the source.
  8. To actually compile the XSP source you need to run the make command.
  9. Root privileges are required for the next step. If you have just installed Linux as a standalone machine you can usually give your account root privileges by typing the sudo bash command and entering the root password.
  10. Finally, to install, for lack of a better word, simply run the make install command. This will deploy the compiled binaries, libraries and other bits and pieces to the correct directories.

Congratulations. You should not have the XSP web server installed on your system. The next section looks at how to run and use the XSP web server.

Running XSP

XSP should be installed on your preferred platform at this stage so now we will look at how to start XSP and we will also look at the different command line options you can use to modify the default behaviour of the server.

Running XSP ASP.NET Examples

Starting XSP with the ASP.NET examples that come with it is a bit of a no-brainer on both Linux and Windows, well, it is with the version 1.0.5 of XSP that I am using:

  • Starting XSP examples site on Windows
    • Simply go to to the Mono folder in the Programs or All Programs section of your Start Menu. For example, on my machine the Mono folder in the All Programs folder on the Start Menu is called Mono 1.0.5 for Windows.
      In this folder, go to the XSP subfolder and then click on XSP Test Web Server.
  • Starting XSP examples site on Linux
    • Starting the XSP server with the default settings on Linux is easier again. Simply change directory to the directory where the ASP.NET examples are installed using the command cd /usr/share/doc/xsp/test/ (this assumes that you have installed XSP in the /usr diretory).
      Now, to start XSP with the ASP.NET examples simply use the command xsp which will start XSP using the default settings.

On both Windows and Linux you should get output similar to the following on your screen:

Listening on port: 8080
Listening on address: 0.0.0.0
Root directory: /usr/share/doc/xsp/test
Hit Return to stop the server.

The above output is from starting XSP on Linux using the steps above. The main difference you will notice in the output from Windows is that the port that the server is listening on will probably be port 8088, which is the default port used by the Mono installer on Windows. You will also notice that the Root Directory will probably be something like C:\PROGRA~1\MONO-1~1.5\share\doc\xsp\test.

Regardless of the output, if XSP has started successfully simply fire up your browser and point it to http://localhost:<port num> where <port num> is the port number that is listed at the end of the the Listening on port: line of the output. On Linux you would generally use http://localhost:8080 and on Windows you would use http://localhost:8088. When the browser loads you should be presented with a web page similar to the one shown in Figure 1 below.

XSP web server on Linux showing ASP.NET examples pages.
Figure 1: XSP web server on Linux showing ASP.NET examples pages.

The page shown in Figure 1 is the home page of the example ASP.NET site that comes with XSP. It has numerous examples of ASP.NET Web Forms and ASP.NET Web Services.

XSP, close up.

The information in the section above shows how to start the examples website that comes with the XSP web server. However, chances are high that you will want to use the web server for actually serving your own web applications and Web Services. To this end we will now look at the command line options that you can use to modify the default behaviour of XSP. However, before doing that I want to point out where the XSP web server itself is located on both Linux and Windows:

  • Windows
    • <Mono install dir>\lib\mono\1.0\xsp.exe
      On my installation, a default Mono install, this relates to: C:\Program Files\Mono-1.0.5\lib\mono\1.0\xsp.exe

      I would recommend adding the directory <Mono install dir>\lib\mono\1.0\ to your path in Windows as this will allow you to start the XSP web server by typing xsp.exe at the command prompt. You should note however that if you do start XSP in this manner you will actually be running it on the .NET runtime and not on the Mono runtime… a good example of Mono – .NET interoperability.

  • Linux
    • <Mono install dir>/lib/mono/1.0/xsp.exe
      On my installation, using the instructions in the XSP on Linux section above, this related to: /usr/lib/mono/1.0/xsp.exe

      Note that on Linux the installation also places a script <Mono install dir>/bin/xsp that can be used from the console to start XSP. This is the command I used in the section above to start the XSP examples web site. When you type and run xsp at the console you are actually running the command mono /usr/lib/mono/1.0/xsp.exe

Now, on to the command line options for XSP, or to be more exact, for xsp.exe:

Option
–port N N is the tcp port to listen on.

Default value: 8080
AppSettings key name: MonoServerPort

–address addr addr is the ip address to listen on.

Default value: 0.0.0.0
AppSettings key name: MonoServerAddress

–root rootdir The server changes to this directory before doing anything else.

Default value: current directory.
AppSettings key name: MonoServerRootDir

–appconfigfile FILENAME Adds application definitions from the XML configuration file.

See sample configuration file that comes with the server.
AppSettings key name: MonoApplicationsConfigFile

–appconfigdir DIR Adds application definitions from all XML files found in the specified directory DIR.

Files must have ‘.webapp’ extension
AppSettings key name: MonoApplicationsConfigDir

–applications APPS A comma separated list of virtual directory and real directory for all the applications we want to manage with this server.

The virtual and real directories are separated by a colon. Optionally you may specify virtual host name and a port.
[[hostname:]port:]VPath:realpath,…

Samples:
/:.
the virtual / is mapped to the current directory.

/blog:../myblog
the virtual /blog is mapped to ../myblog

myhost.someprovider.net:/blog:../myblog
the virtual /blog at myhost.someprovider.net is mapped to ../myblog

/:.,/blog:../myblog
Two applications like the above ones are handled.

Default value: /:.
AppSettings key name: MonoApplications

–nonstop Don’t stop the server by pressing enter.
Must be used when the server has no controlling terminal.
–version Displays version information and exits.
–verbose Prints extra messages.
Mainly useful for debugging.

The above command line options where taken from the XSP web server itself

So, what does the above boil down to? Well, the easiest way to explain is to show some example command lines so here goes:

  • Windows: xsp.exe --port 80 --root c:\inetpub\wwwroot\
  • Linux: mono /usr/lib/mono/1.0/xsp.exe --port 80 --root /home/bdelahunty/asp.net/ --applications /:.,/articles:../documents/articles
  • Windows: mono "C:\Program Files\Mono-1.0.5\lib\mono\1.0\xsp.exe" --port 80 --root r:\WebApplications\ --nonstop

Now that we have looked at XSP it’s time to move on to getting Apache serving ASP.NET pages. If you don’t care about ASP.NET on Apache then you could always skip ahead to the Some Examples section where I run through a simple ASP.NET example.

ASP.NET on Apache with mod_mono

Apache is the web server of choice on Linux and is, according to netcraft[^], the most widely used web server on the net. This means that there are lots of websites out there that could be serving ASP.NET web applications and hosting ASP.NET Web Services. Mono makes this possible in the form of the mod_mono module for Apache which makes use of XSP or, to be more specific, it makes use of mod-mono-server.exe which is a specialized version of XSP.

In this section we will look at installing mod_mono and how to get Apache serving ASP.NET content. However, before we do that, we need to get Apache installed. As this article is aimed more at how to get Apache serving and hosting ASP.NET content I will quickly run over getting Apache installed. If you run into any difficulty when trying to install Apache just search the web for instructions on how to install Apache on your particular Linux distribution.

Oh, and one more thing. As I mentioned in the What are XSP and mod_mono? section, mod_mono currently does not work with Apache on Windows so I only look at Apache on Linux from here on.

Installing Apache

As with most things on Linux there are a number of different ways to install applications. In the case of Apache I’m just going to install from source as it is probably one of the easiest ways to install Apache and it is also the most distribution independent way of installing it that I know of. If you already have Apache 1.3.x or greater installed then you can skip ahead to the Installing and configuring mod_mono section. So, lets get Apache installed:

  1. First you will need to download the Apache source so head over to the Apache download page at http://httpd.apache.org/download.cgi[^].
  2. Download the latest version of the Unix Source for Apache. At the time of writing this article the latest version is 2.0.53. The file I downloaded was http://apache.mirrors.esat.net/httpd/httpd-2.0.53.tar.gz.
  3. Once the file has been downloaded, go to a console for the remainder of the process
  4. Decompress the downloaded file. For example, if you have downloaded the same file that I downloaded you could use the command tar -xvzf httpd-2.0.53.tar.gz as long as the file is in your current working directory.
  5. Next, switch to the directory where the source code has been decompressed to by typing cd httpd-2.0.53 or cd followed by the name of the directory that you decompressed the file to if it is different from mine.
  6. Now, before we can compile the source you need to configure the make files. To do this simply type ./configure --enable-so --prefix=/usr.
  7. When the the <CODE>./configure --enable-so --prefix=/usr completes you are free to go ahead and compile and install the source.
  8. Run the make command to compile Apache.
  9. You need to have root privileges to perform the next step successfully. If you have just installed Linux as a standalone machine you can usually give your account root privileges by typing the sudo bash command and entering the root password.
  10. Finally, to install, for lack of a better word, simply run the make install command. This will deploy the compiled binaries, libraries and other bits and pieces to the correct directories.

At this stage you should have Apache installed. To start the Apache web server type httpd at the console. You can check if Apache has started by pointing your browser to http://localhost/ where you should see a web page similar to the one shown in Figure 2.

Apache Web Server running on Linux
Figure 2: Apache Web Server running on Linux

Now that we have Apache installed it’s time to install mod_mono and configure Apache to host and serve ASP.NET content.

Installing and configuring mod_mono

Ok. We’re nearly there. Apache should be up and running by now so we don’t have too much left to do. We need to install mod_mono and then configure Apache in order to make it send the requests that we want through the mod_mono module and on to the mod-mono-server.exe application that will handle the request. As with Apache, I’m gong to run through installing mod_mono from the source. Before we actually go ahead and install mod_mono I should mention that there are some prerequisites to installing. You must have Mono installed, you must have XSP installed, and you must have Apache installed. If you’ve been following this article, and the previous article, then you should already have your system configured as needed.

Now, lets see if we can get mod_mono installed:

  1. So, once again, we need to download some source code. Head on over to the Mono download page at http://www.mono-project.com/downloads/index.html[^].
  2. In the source code section click on and download the “Apache Mono module”. I downloaded http://www.go-mono.com/archive/1.0.5/mod_mono-1.0.5.tar.gz.
  3. Once the file has been downloaded, go to a console for the remainder of the process
  4. Decompress the downloaded file using the command tar -xvzf mod_mono-1.0.5.tar.gz where mod_mono-1.0.5.tar.gz is the name of the file you downloaded.
  5. Next, switch to the directory where the source code has been decompressed to by typing cd mod_mono-1.0.5 or cd followed by the name of the directory that you decompressed the file to if it is different from mine.
  6. Now, before we can compile the source you need to configure the make files. To do this simply type ./configure --prefix=/usr .
  7. Next, to actually compile the mod_mono source you need to run the make command.
  8. The next step has to be performed using an account with root privileges. If you have just installed Linux as a standalone machine you can usually give your account root privileges by typing the sudo bash command and entering the root password.
  9. Finally, to install mod_mono simply run the make install command. This will deploy the compiled binaries, libraries and other bits and pieces to the correct directories.

That should have been painless. You should have mod_mono installed on your system now. So what’s left to do? Well, we need to configure Apache to use mod_mono to handle requests. This means we need to edit the Apache config files. If you have installed Apache following the instructions in the Installing Apache section the Apache config file should be the following file: /usr/conf/httpd.conf
Obviously the location of this file may be different if you installed Apache in a different location or if you installed it using a different method.

Firstly, we need to configure Apache to load the mod_mono module. To do this we need to edit the Apache config file, /usr/conf/httpd.conf, and add the following line to the end:

LoadModule mono_module /usr/modules/mod_mono.so

The above line tells Apache to load the module located at /usr/modules/mod_mono.so as the mono module.

The next thing that we need to do it configure Apache to serve some ASP.NET content. We will use the same ASP.NET Examples that we used with the XSP server above. To do this we need to tell Apache to serve content from the /usr/share/doc/xsp/test/ directory for a given path, lets say, /AspNetOnApache. This means that when you browse to http://localhost/AspNetOnApache you will be served the ASP.NET content in the /usr/share/doc/xsp/test/ directory.

Here are the additions you need to make to the Apache config file:

Alias /AspNetOnApache "/usr/share/doc/xsp/test"
MonoApplications "/AspNetOnApache:/usr/share/doc/xsp/test"

Finally, we need to tell Apache which module to use as the handler for requests. For Apache 1.3.x you can use:

<Directory /usr/share/doc/xsp/test>
    SetHandler mono
    <IfModule mod_dir.c>
        DirectoryIndex index.aspx
    </IfModule>
</Directory>

and for Apache 2.0.x you can use the above text OR you can use the following

<Location /AspNetOnApache>
    SetHandler mono
</Location>

Finally! We are there. Simply go to the console and type the command httpd to start Apache. Point your browser to http://localhost/AspNetOnApache and you should see a page similar to the one shown in Figure 3.

Apache web server on Linux showing ASP.NET examples pages.
Figure 3: Apache web server on Linux showing ASP.NET examples pages.

Some Examples

So after all that you should be able to serve ASP.NET content from at least one server on Linux. Just to help round off the “experience” I’ve decided to add a basic web application example to this article.

A simple web application example

Our simple web application is very simple indeed. It consists of a single page that displays some basic information about the machine that the page is hosted on and also some basic information about the request for the page. There are two files, index.aspx and index.aspx.cs, the former being the web page and the latter being the codebehind file for that page. So open up your favourite text editor or IDE and create the two files (hint: copy and paste is probably the quickest way):

index.aspx

<%@ Page language="c#" src="introtomono2/index.aspx.cs" 
         Inherits="SimpleWebApp.SimplePage" AutoEventWireup="false"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
   <HEAD>
      <title>Simple Page</title>
   </HEAD>
   <body>
      <form method="post" runat="server">
         <table width="450px" border="1px">
            <tr>
               <td colspan="2"><strong>Server Details</strong></td>
            </tr>
            <tr>
               <td>Server Name:</td>
               <td>
                  <asp:Label id="serverName" runat="server"></asp:Label></td>
            </tr>
            <tr>
               <td>Operating System:</td>
               <td>
                  <asp:Label id="operatingSystem" runat="server"></asp:Label>
               </td>
            </tr>
            <tr>
               <td>Operating System Version:</td>
               <td>
                  <asp:Label id="operatingSystemVersion" runat="server"><BR>                  </asp:Label>
               </td>
            </tr>
         </table>
         <br>
         <table width="450px" border="1px">
            <tr>
               <td colspan="2"><strong>Request Details</strong></td>
            </tr>
            <tr>
               <td>Page Requested:</td>
               <td>
                  <asp:Label id="requestedPage" runat="server"></asp:Label>
               </td>
            </tr>
            <tr>
               <td>Request From:</td>
               <td>
                  <asp:Label id="requestIP" runat="server"></asp:Label>
               </td>
            </tr>
            <tr>
               <td>User Agent:</td>
               <td>
                  <asp:Label id="requestUA" runat="server"></asp:Label>
               </td>
            </tr>
         </table>
      </form>
   </body>
</HTML>

index.aspx.cs

using System;
using System.Web.UI.WebControls;

namespace SimpleWebApp
{
   public class SimplePage : System.Web.UI.Page
   {
      protected Label operatingSystem;
      protected Label operatingSystemVersion;
      protected Label requestedPage;
      protected Label requestIP;
      protected Label requestUA;
      protected Label serverName;
   
      protected override void OnLoad(EventArgs e)
      {
         DisplayServerDetails();
         DisplayRequestDetails();

         base.OnLoad (e);
      }

      private void DisplayServerDetails()
      {
        serverName.Text = Environment.MachineName;
        operatingSystem.Text = Environment.OSVersion.Platform.ToString();
        operatingSystemVersion.Text = Environment.OSVersion.Version.ToString();
      }

      private void DisplayRequestDetails()
      {
         requestedPage.Text = Request.Url.AbsolutePath;
         requestIP.Text = Request.UserHostAddress;
         requestUA.Text = Request.UserAgent;
      }
   }
}

Once you have saved these two files we now need to be able to serve it. I saved the files to /home/bdelahunty/mono/webappexample/ and we now need to configure both XSP and Apache to serve pages from this location. As this is a simple web application example lets call it SimpleWebApp.

To get XSP to serve the page use the following command at the console (replacing /home/bdelahunty/mono/webappexample/ with the directory where you saved the files):

xsp --applications /SimpleWebApp:/home/bdelahunty/mono/webappexample/

To get Apache to serve the page simply add the following to the end of the Apache config file, /usr/conf/httpd.conf in my case (replacing /home/bdelahunty/mono/webappexample/ with the directory where you saved the files):

Alias /SimpleWebApp "/home/bdelahunty/mono/webappexample"
MonoApplications "/SimpleWebApp:/home/bdelahunty/mono/webappexample"   

You also need to add the following if you are using Apache 1.3.x:

<Directory /home/bdelahunty/mono/webappexample>
    SetHandler mono
        <IfModule mod_dir.c>
            DirectoryIndex index.aspx
        </IfModule>
</Directory>

and for Apache 2.0.x you can use the above text OR you can use the following:

<Location /SimpleWebApp>
    SetHandler mono
</Location>

Now just fire up your browser and point it to http://localhost/SimpleWebApp for Apache or http://localhost:8080/SimpleWebApp for XSP and you should see a page similar to the one shown in Figure 4.

The SimplePage.aspx file displaying the server and request             details.
Figure 4: The SimplePage.aspx file displaying the server and request details.

Problems with the Visual Studio .NET and IIS way?

If you are used to developing ASP.NET web applications with Visual Studio .NET then there are a few things you need to watch out for. When creating aspx pages Visual Studio .NET used the CodeBehind attribute in the pages @Page directive. However, CodeBehind is not actually an official ASP.NET @Page directive attribute and is only used by the IDE at design time and when compiling the ASP.NET application to a dll. You would normally then drop this compiled dll into the bin directory of your web application and IIS would automatically pick up the changes. This doesn’t work with XSP or mod_mono; you can’t just drop in a new pre-compiled dll and have the web server start using it.

You may or may not have noticed that I used the Src attribute in the @Page directive in the simple example above and I did not give any details about pre-compiling the web application to a dll that has to be deployed. I just pointed XSP and Apache to the directory where the index.aspx and index.aspx.cs files where located and the web servers handled the compilation of the web application the first time the page was called. This is one way you can get around the pseudo problem of not being able to just drop in a new pre-compiled dll. You can just drop in a new .cs codebehind file and the web server will compile it on the fly. This method does however require that you have all your codebehind files on the server. If you setup your permissions correctly then this is not an issue, if you don’t, it’s a big issue as any visitor to your site could potentially view the code behind your site and potentially come up with ways to hack it.

If you are used to the Visual Studio .NET and IIS way, or if you simply use Visual Studio .NET to build your web applications and want to just drop the pre-compiled dll into the bin directory then you can quickly overcome the problem by deploying your new dll and then restarting the web server. Use httpd -k restart to restart Apache after you have put in a new dll and if you are using XSP just shut it down and then restart XSP it.

Original Post: http://www.mono-project.com/docs/web/aspnet/

Mono has an implementation of ASP.NET 2.0, ASP.NET MVC and ASP.NET AJAX.

Quick Resources:

Mono’s ASP.NET implementations supports two kinds of applications:

  • Web Forms (Web Applications infrastructure).
  • Web Services (the SOAP-based RPC system).

Status and tests for ASP.NET 2.0 are available in our ASPTests page.

Running ASP.NET applications

To run your ASP.NET applications with Mono, you have three classes of options:

  • Apache hosting: use mod_mono, a module that allows Apache to serve ASP.NET applications.
  • FastCGI hosting: use the FastCGI hosting if you have a web server that supports the FastCGI protocol (for example Nginx) for extending the server. You also may use a web server that only has support for CGI using cgi-fcgi.
  • XSP: this is a simple way to get started, a lightweight and simple webserver written in C#.

For deploying applications, we recommend the use of the mod_mono or FastCGI approaches, as that will give you all the configuration options and flexibility that come with using Apache or a FastCGI server.

For quickly getting started, get familiar with Mono and ASP.NET, XSP is the ideal solution. Keep in mind that XSP is a very limited server and is only useful to get acquainted with ASP.NET and Mono, it only support HTTP 1.0 and does not provide much extensibility or configuration.

More advaned users can use the HttpListener and the ASP.NET hosting to create their own hosts for ASP.NET applications.

ASP.NET hosting with Apache

The mod_mono Apache module is used to run ASP.NET applications within the Apache web server.

The mod_mono module runs within an Apache process and passes all the requests to ASP.NET applications to an external Mono process that actually hosts your ASP.NET applications. The external ASP.NET host is called “mod-mono-server” and is part of the XSP module.

To use this, you must download and install the mod_mono and xsp components of Mono. mod_mono contains the actual Apache module, and xsp contains the actual ASP.NET hosting engine, both are available from our download page.

See the mod_mono page for details on installation and configuration.

ASP.NET hosting with Nginx

Nginx is a high-performance HTTP server which support running ASP.NET and ASP.NET MVC web applications through FastCGI protocol. See the FastCGI Nginx page for details on installation and configuration.

ASP.NET hosting with XSP

XSP is a standalone web server written in C# that can be used to run your ASP.NET applications with minimal effort. XSP works under both the Mono and Microsoft runtimes. The code is available from our download page (look for XSP web server) or from the git repository (module name: xsp).

The easiest way to start XSP is to run it from within the root directory of your application. It will serve requests on port 8080. Place additional assemblies in the bin directory. Other XSP options can be set on the command line, such as the application directory and the port to listen on.

XSP comes with a set of pages, controls and web services that you can use to test the server and see what ASP.NET looks like.

For example, once you install XSP, you can try some samples like this:

 $ cd /usr/lib/xsp/test
 $ xsp
 Listening on port: 8080
 Listening on address: 0.0.0.0
 Root directory: /home/cvs/mcs/class/corlib/Microsoft.Win32
 Hit Return to stop the server.

You can now browse to http://localhost:8080 and see various sample programs

SSL support in XSP

XSP supports SSL and TLS Client Certificates. For further details about setting it up, see the UsingClientCertificatesWithXSP document.

Configuration

Applications can be configured through the web.config file, the full documentation is available from MSDN, and also a Mono-specific version is available on this site here.

Additionally, you can configure Mono-specific ASP.NET settings (to have applications that behave differently depending on the operating system they are deployed in) using the ASP.NET Settings Mapping engine.

Other extensions

Check out ASP.NET Modules for details on how to support deflate/gzip encodings and authentication.

Debugging

By default xsp and xsp2 run in Release mode, which means that debugging line-number information will not be available in stack traces when errors occur.

To obtain line numbers in stack traces you need to do two things:

  1. Enable Debug code generation in your page. 2. Run Mono with the –debug command line option.

You must enable debug code generation in your page using the Debug=”true” in the top of your page, or setting the compilation flag in Web.config (compilation option).

Use the –debug command line option to Mono, this is done by setting the MONO_OPTIONS environment variable, like this:

$ MONO_OPTIONS=--debug xsp2
Listening on port: 8080 (non-secure)
Listening on address: 0.0.0.0
Root directory: /tmp/us
Hit Return to stop the server.

To do the same with the Apache mod_mono module, use the MonoDebug true directive in your apache configuration file.

Supported Versions

Mono supports ASP.NET 2.0, ASP.NET AJAX and a handful of 3.5 controls.

Limitations

Mono’s ASP.NET does not implement the following features:

  • Precompiled updatable web sites.
  • WebParts APIs.

Work in Progress

git access

Users interested in the latest version of mod_mono and xsp can retrieve these from our public git repository. The module names are mod_mono and xsp respectively. You will also need to check out the mcs module as the System.Web classes are in mcs/class/System.Web.

Designer

There is work in progress on an ASP.NET Designer the designer will eventually be integrated into the MonoDevelop IDE.

install_mono.sh

Install mono on centOS 6.x

$ yum 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 wget
$ cd /usr/local/src
$ tar jxf mono-3.2.8.tar.bz2
$ cd mono-3.2.8
$ ./configure –prefix=/opt/mono
$ make && make install
$ export PATH=$PATH:/opt/mono/bin
$ export PKG_CONFIG_PATH=/opt/mono/lib/pkgconfig
$ # make sure to add the previous two lines to your ~/.bash_profile

My Adventures Installing mono 2.0 on CentOS 4 to work with apache via mod_mono

My Adventures Installing mono 2.0 on CentOS 4 to work with apache via mod_mono

Apparently the good folks over at the mono project decided to discontinue binary packages for the Red Hat line of linux distributions.  It’s a shame in a way, there are a lot of those installation out there, so it would be nice to keep things updated through yum or apt-install or rhupdate, etc..

On the up side, installing from source has never been easier.  In the past I have went through many hours of trying to get the right versions of different libraries that were needed.  With the official release of 2.0 it seems much better.  I thought I would share the steps that I went through.

Disclaimer: This worked on a fairly fresh install of CentOS 4.7.  I have not tried it on 5.x, nor on any other flavor of linux (SUSE, Ubuntu, etc..) so your mileage may vary.

At the time of the install (and this writing) the current mono stable version is 2.0.1 so all references will be to that version.  Here are the steps that I went through.

Preparation

Always be prepared – Boy Scouts motto…

In rooting around the web I did find a few helpful pointers.  First, make sure you have gcc installed.  Now this is one of those duh pieces of information, but in the fairness of completeness I thought I would mention it. (Note: If you do not have gcc or bison, install them! Credit The_Assimilator’s comment)

# yum install gcc-c++
# yum install bison
Next I installed the httpd-devel package.  I had read (will find the link later) that it helps some of the installation down the line.  In my case I just use yum to install it. (Note:  httpd-devel package is required by the mod_mono compile if apxs (Apache Extension Tool) is not on your machine. credit to The_Assimilator’s comment)
yum install httpd-devel

You may also require the glib-2.0 libraries (thanks to Michael Walsh for that bit).  If you receive the error “Package glib-2.0 was not found in the pkg-config search path” you can install it via yum as well.

yum install glib2-devel

The Main Dance

Next comes the meat of the installation.  First, I downloaded the necessary source packages.  I simply used wget to snag the core mono package, xsp (mono web server) and mod_mono (apache integration).

wget http://ftp.novell.com/pub/mono/sources/mono/mono-2.0.1.tar.bz2
wget http://ftp.novell.com/pub/mono/sources/xsp/xsp-2.0.tar.bz2
wget http://ftp.novell.com/pub/mono/sources/mod_mono/mod_mono-2.0.tar.bz2

Next we install the mono core

tar -vxjf mono-2.0.1.tar.bz2
cd mono-2.0.1
./configure
make
make install
cd ..

Next comes xsp

tar -vxjf xsp-2.0.tar.bz
cd xsp-2.0
./configure
make
make install
cd ..

At this point I recevied an error (I believe it was in the make process) that the compiler could not find the file dotnet.pc.  I found that it was indeed on my system so I simply had to export the path and then finsih the compile.

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
make
make install
cd..

Note: Make sure the file dotnet.pc is in that location.  If not, adjust the path above.

Finally we install mod_mono

tar -vxjf mod_mono-2.0.tar.bz2
cd mod_mono-2.0
./configure
make
make install

There, easy huh?

Configuration

You may want to verify a few thigns to make sure the configuration is ready to rock.  In my case, I am keeping the mono configuration in a separate file for sanity sake.  You can do that or put it all in your httpd.conf, it’s up to you.

<IfModule !mod_mono.c>
    LoadModule mono_module /usr/lib/httpd/modules/mod_mono.so
    AddType application/x-asp-net .aspx
    AddType application/x-asp-net .asmx
    AddType application/x-asp-net .ashx
    AddType application/x-asp-net .asax
    AddType application/x-asp-net .ascx
    AddType application/x-asp-net .soap
    AddType application/x-asp-net .rem
    AddType application/x-asp-net .axd
    AddType application/x-asp-net .cs
    AddType application/x-asp-net .config
    AddType application/x-asp-net .Config
    AddType application/x-asp-net .dll
    DirectoryIndex index.aspx
    DirectoryIndex Default.aspx
    DirectoryIndex default.aspx
</IfModule>
That was it.  I hope that helps!

Running ASP.NET and MVC Applications in Linux/ Unix OS X (CentOS/ Debian) using XSP and Mono

http://www.mono-project.com/docs/web/aspnet/

ASP.NET

Mono has an implementation of ASP.NET 2.0, ASP.NET MVC and ASP.NET AJAX.

Quick Resources:

Mono’s ASP.NET implementations supports two kinds of applications:

  • Web Forms (Web Applications infrastructure).
  • Web Services (the SOAP-based RPC system).

Status and tests for ASP.NET 2.0 are available in our ASPTests page.

Running ASP.NET applications

To run your ASP.NET applications with Mono, you have three classes of options:

  • Apache hosting: use mod_mono, a module that allows Apache to serve ASP.NET applications.
  • FastCGI hosting: use the FastCGI hosting if you have a web server that supports the FastCGI protocol (for example Nginx) for extending the server. You also may use a web server that only has support for CGI using cgi-fcgi.
  • XSP: this is a simple way to get started, a lightweight and simple webserver written in C#.

For deploying applications, we recommend the use of the mod_mono or FastCGI approaches, as that will give you all the configuration options and flexibility that come with using Apache or a FastCGI server.

For quickly getting started, get familiar with Mono and ASP.NET, XSP is the ideal solution. Keep in mind that XSP is a very limited server and is only useful to get acquainted with ASP.NET and Mono, it only support HTTP 1.0 and does not provide much extensibility or configuration.

More advaned users can use the HttpListener and the ASP.NET hosting to create their own hosts for ASP.NET applications.

ASP.NET hosting with Apache

The mod_mono Apache module is used to run ASP.NET applications within the Apache web server.

The mod_mono module runs within an Apache process and passes all the requests to ASP.NET applications to an external Mono process that actually hosts your ASP.NET applications. The external ASP.NET host is called “mod-mono-server” and is part of the XSP module.

To use this, you must download and install the mod_mono and xsp components of Mono. mod_mono contains the actual Apache module, and xsp contains the actual ASP.NET hosting engine, both are available from our download page.

See the mod_mono page for details on installation and configuration.

ASP.NET hosting with Nginx

Nginx is a high-performance HTTP server which support running ASP.NET and ASP.NET MVC web applications through FastCGI protocol. See the FastCGI Nginx page for details on installation and configuration.

ASP.NET hosting with XSP

XSP is a standalone web server written in C# that can be used to run your ASP.NET applications with minimal effort. XSP works under both the Mono and Microsoft runtimes. The code is available from our download page (look for XSP web server) or from the git repository (module name: xsp).

The easiest way to start XSP is to run it from within the root directory of your application. It will serve requests on port 8080. Place additional assemblies in the bin directory. Other XSP options can be set on the command line, such as the application directory and the port to listen on.

XSP comes with a set of pages, controls and web services that you can use to test the server and see what ASP.NET looks like.

For example, once you install XSP, you can try some samples like this:

 $ cd /usr/lib/xsp/test
 $ xsp
 Listening on port: 8080
 Listening on address: 0.0.0.0
 Root directory: /home/cvs/mcs/class/corlib/Microsoft.Win32
 Hit Return to stop the server.

You can now browse to http://localhost:8080 and see various sample programs

SSL support in XSP

XSP supports SSL and TLS Client Certificates. For further details about setting it up, see the UsingClientCertificatesWithXSP document.

Configuration

Applications can be configured through the web.config file, the full documentation is available from MSDN, and also a Mono-specific version is available on this site here.

Additionally, you can configure Mono-specific ASP.NET settings (to have applications that behave differently depending on the operating system they are deployed in) using the ASP.NET Settings Mapping engine.

Other extensions

Check out ASP.NET Modules for details on how to support deflate/gzip encodings and authentication.

Debugging

By default xsp and xsp2 run in Release mode, which means that debugging line-number information will not be available in stack traces when errors occur.

To obtain line numbers in stack traces you need to do two things:

  1. Enable Debug code generation in your page. 2. Run Mono with the –debug command line option.

You must enable debug code generation in your page using the Debug=”true” in the top of your page, or setting the compilation flag in Web.config (compilation option).

Use the –debug command line option to Mono, this is done by setting the MONO_OPTIONS environment variable, like this:

$ MONO_OPTIONS=--debug xsp2
Listening on port: 8080 (non-secure)
Listening on address: 0.0.0.0
Root directory: /tmp/us
Hit Return to stop the server.

To do the same with the Apache mod_mono module, use the MonoDebug true directive in your apache configuration file.

Supported Versions

Mono supports ASP.NET 2.0, ASP.NET AJAX and a handful of 3.5 controls.

Limitations

Mono’s ASP.NET does not implement the following features:

  • Precompiled updatable web sites.
  • WebParts APIs.

Work in Progress

git access

Users interested in the latest version of mod_mono and xsp can retrieve these from our public git repository. The module names are mod_mono and xsp respectively. You will also need to check out the mcs module as the System.Web classes are in mcs/class/System.Web.

Designer

There is work in progress on an ASP.NET Designer the designer will eventually be integrated into the MonoDevelop IDE.

http://www.mono-project.com/download/

http://www.codeproject.com/Articles/9738/Introduction-to-Mono-ASP-NET-with-XSP-and-Apache

Introduction to Mono – ASP.NET with XSP and Apache

Introduction

This article is the second article in the series of introductory articles that I am writing about Mono, the “open source development platform based on the .NET framework”. In this article we’ll take a look at how to get going with ASP.NET on the Mono platform. Although ASP.NET is not part of the ECMA and ISO standards mentioned in the first article[^], it is still one of the major selling points of the .NET platform and provides an extremely flexible and powerful platform for developing web applications and Web Services upon. Although you can develop ASP.NET applications for Mono on a number of different operating systems this article will focus mainly on Linux, although, in saying that, I do look briefly at getting XSP running on Windows. The reason I will concentrate on ASP.NET on Linux is because those people interested in ASP.NET on Windows have an extremely powerful option at their fingertips in the form IIS and I would whole heartedly recommend using it for ASP.NET on Windows.

Where does ASP.NET stand with Mono??

The latest stable version of Mono, version 1.0.5, has a fully functional implementation of ASP.NET. This includes full support for ASP.NET Web Forms and Web Services. This essentially means that more or less any ASP.NET application that you have developed using with the .NET Framework will work with Mono. Obviously there might be changes needed, such as data access changes, removal of any reliance on .NET Framework BCL types that are not implemented in Mono yet, and also the removal of any code that makes use of platform invoke and so on. At this stage the ASP.NET support in Mono can be considered as excellent and a lot of publicly available web applications already make use of Mono’s ASP.NET support. According to the Mono site the Mono Web Services stack is being used in the source control application Vault[^] by SourceGear and aspects of Mono’s ASP.NET implementation are also used in the Virtuoso[^] product from OpenLink.

What are XSP and mod_mono?

There isn’t much point in developing web applications and Web Services if you have no way of serving them, is there? Traditionally you would use IIS to host ASP.NET applications on Windows, although there are some other free ASP.NET web servers available such as Cassini[^]. However, when using ASP.NET with Mono you have two main options as regards which web server to host your ASP.NET applications in:

  • XSP
  • Apache

In this article we look at using both XSP (on Windows and Linux) and Apache to host your ASP.NET web applications and Web Services.

XSP

XSP is a “light-weight web server” capable of hosting and serving ASP.NET applications. It is written using C#, making extensive use of the classes in the System.Web namespace, and the source for XSP is available for download from the Mono download page[^] . It is also interesting to note that the XSP web server runs on both the Mono runtime and also on the .NET runtime which means that if you are looking for a light-weight ASP.NET web server for Windows but you don’t want to use or you don’t care about Mono, you could still make use of XSP. The Let’s get XSP up and running section runs through how to install and use XSP on Windows and Linux.

Apache

Apache is probably the de facto standard web server used on Linux. Apache makes extensive use of modules to enable it to host and serve web applications developed in a multitude of different web programming languages and scripts. Those of you familiar with IIS but not so familiar with Apache can think of these modules as the equivalent of ISAPI extensions. So, by this stage I assume you can guess how Apache can host and serve ASP.NET application? Yep, using an Apache module. This module, called mod_mono, allows Apache to serve ASP.NET pages “by proxying the requests to a slightly modified version of XSP called mod-mono-server”. mod-mono-server is installed when you install XSP. At the time of writing the current version of the mod_mono module for Apache only works for Apache on Linux and not for Apache on Windows. For this reason, when we look at ASP.NET on Apache in the Apache with mod_mono section we only look at it in the context of Linux. If you do wish to serve ASP.NET content from Apache on Windows have a look at this article[^] on CP, which shows how to use configure Apache to use Cassini to serve ASP.NET content or have a look at the Apache HTTP CLI[^] project.

What you need to know

This article assumes that you have Mono installed and working on your desired platform. The first article in this series, Introduction to Mono – Your first Mono app[^], explains how to get Mono up and running on Windows and Linux so if you have not yet done so have a look over that article. I also assume that you are familiar with ASP.NET and the semantics of ASP.NET programming as this article primarily looks at getting ASP.NET working with XSP and Apache, not at ASP.NET programming.

Lets get XSP up and running

In this section we will look at getting XSP up and running on both Windows and on Linux. Installing and running on Windows is “tackled” first simply because I want to get it out of the way and because chances are that you already have XSP on Windows considering the fact that you should have Mono already installed. Then it’s on to Linux and getting XSP up and running there. To be honest, if you already have Mono running on Linux, which you should as it’s one of the prerequisites for this article, then it should be relatively easy.

XSP on Windows

The first article[^] in this series ran through installing Mono on Windows. It mentioned that during the install process the installer gives you the option to select which components to install. By default, XSP is installed along with Mono but if you decided to not install XSP during the install then the easiest way to get XSP installed is to uninstall Mono and then reinstall it again. Here are the steps explaining how to install Mono and XSP on a Windows 2000 or above machine:

  1. Download the Mono Windows Installer from the Mono download page. The file I am working off is http://www.go-mono.com/archive/1.0.5/windows/mono-1.0.5-gtksharp-1.0.5-win32-0.1.exe
  2. The installer is a standard Windows installer so you can simply run it once it is downloaded by doubling clicking on it.
  3. Once the installer begins, click on next to go to the License Agreement page.
  4. You’ll probably notice on the License Agreement page that there are a number of different Licenses that apply to the different components of the install. While I understand that most people simply agree and click on next, it would be irresponsible of me if I didn’t recommend that you read the License Agreement before agreeing to it.
  5. Once you pass the License Agreement section you are presented with some general information about the install.
  6. Click on Next and select the install location.
  7. Click on Next again to bring you to the Select Components page. You are free to customize your installation but make sure that you install XSP. As I will be using all the components of this install in other articles in this series I would suggest that you accept the default “Full installation”.
  8. Click on Next again and you can customize the name of Mono folder that appears in the Start Menu.
  9. The installer will now ask you which port you want the XSP web server to listen on. Generally this will be port 8088 but you can change it to any free port on your system. When you are ready click on Next.
  10. Finally, click on Install to install and configure Mono and XSP on your Windows machine.

Those of you who read the first article will notice that I more or less ripped the instructions from that one… I’m not lazy, just no point in rewriting what has already been said! The next step in using XSP on Windows is to run it. The Running XSP section explains how to run the XSP web server and looks at some of the command line options that you can use to alter it’s default behaviour.

XSP on Linux

As with installing Mono on Linux, which we examined in the previous article, things are slightly more complicated on Linux then on Windows. However, as I already mentioned, if you have Mono installed, then getting XSP installed is going to be a breeze for you. I’m installing XSP on the same systems that I used for installing Mono in the previous article. A quick recap of what exactly I used is in order so: I used Microsoft Virtual PC 2004 and created two identical virtual PCs. I then installed SuSE 9.2 Professional with ACPI disabled and I used the default package selection when installing. In the last article I went through how to install Mono using the pre-built packages from the Mono site and also I ran through how to install Mono from source. So for the purposes of this article I will be installing XSP on a SuSE 9.2 Professional with the default package selection with Mono 1.0.5 installed along with any additional packages or programs that were installed during the Mono install.

In this article we will again look at installing from both the pre-build packages and from the source.

XSP using pre-built packages

As I am sure you can remember from the first article (if you read it that is), installing using the pre-built packages from the Mono site is one of the easiest ways of getting Mono up and running. The same thing applies to getting XSP up and running. So, head on over to the Mono download page (http://www.mono-project.com/downloads/index.html[^]) and see if there are pre-built packages for your particular Linux distribution or if there are packages for a distribution that you know will be compatible with your distribution. If there isn’t a package that will work with your distribution just jump ahead to the XSP from source section where I will run through installing XSP from the source which is a simple process.

Lets get going:

  1. On the Mono download page, click on the “Packages” link beside the Linux distribution that you believe to be compatible with your distribution. I simply selected the link beside SuSE 9.2
  2. This should bring you to a page which lists a lot of different packages that have been compiled and packaged for that particular Linux distribution.
  3. Before we install XSP we need to install two other pre-built packages. These packages include files that XSP is dependent upon.
  4. The first package we need to install is the “Database Core” package that can be found in the database section. The exact name of the package will vary depending on the packages link that you selected initially but in general the name will start with “mono-data”. The package I downloaded for SuSE 9.2 was http://www.go-mono.com/archive/1.0.5/suse-92-i586/mono-data-1.0.5-1.ximian.10.4.i586.rpm.
  5. Once you have downloaded this file, open it and it will launch the package manager for your distribution. At this stage simply follow the instructions to install the package.
  6. After you have installed the “mono-data” package you now need to download the “mono-web” package which will be listed under the “Web Applications, Web Services” section. The package I downloaded for SuSE 9.2 was http://www.go-mono.com/archive/1.0.5/suse-92-i586/mono-web-1.0.5-1.ximian.10.4.i586.rpm.
  7. Next, as with the “mono-data” package, simply open the package and follow the instructions to install it using the package manager of your distribution.
  8. Finally we are ready to install XSP itself. So just head back to the “Web Applications, Web Services” section of the package download page and download the “xsp” package. The package I got was http://www.go-mono.com/archive/1.0.5/suse-92-i586/xsp-1.0.5-1.ximian.10.1.i586.rpm.
  9. As with the two previous packages, simply open it and follow the instructions of your package manager to install XSP.
  10. All going well, you should have XSP installed now!

At this stage XSP should be installed on your system so you can jump ahead to the Running XSP section which gives an overview of how to use XSP. If you are having difficulty installing XSP from the packages have a look at the next section, “XSP from source”, which might be able to help you out.

XSP from source

Installing XSP from the source should be breeze if you have installed Mono from source by following the instructions in the previous article[^]. Let’s jump straight in:

  1. The first thing you need to do is download the source. Once again, head on over to the Mono download page at http://www.mono-project.com/downloads/index.html[^].
  2. In the source code section click on and download the “XSP web server”. I downloaded http://www.go-mono.com/archive/1.0.5/xsp-1.0.5.tar.gz.
  3. Once the file has been downloaded, go to a console for the remainder of the process
  4. You can decompress the downloaded file using the command tar -xvzf xsp-1.0.5.tar.gz where xsp-1.0.5.tar.gz is the name of the file you downloaded.
  5. Next, switch to the directory where the source code has been decompressed to by typing cd xsp-1.0.5 or cd followed by the name of the directory that you decompressed the file to if it is different from mine.
  6. Now, before we can compile the source you need to configure the make files. To do this simply type ./configure --prefix=/usr. This will take a bit of time. I am assuming here that there were no errors and that the ./configure --prefix=/usr went ahead without issue.
  7. When the the ./configure --prefix=/usr completes you are free to go ahead and compile and install the source.
  8. To actually compile the XSP source you need to run the make command.
  9. Root privileges are required for the next step. If you have just installed Linux as a standalone machine you can usually give your account root privileges by typing the sudo bash command and entering the root password.
  10. Finally, to install, for lack of a better word, simply run the make install command. This will deploy the compiled binaries, libraries and other bits and pieces to the correct directories.

Congratulations. You should not have the XSP web server installed on your system. The next section looks at how to run and use the XSP web server.

Running XSP

XSP should be installed on your preferred platform at this stage so now we will look at how to start XSP and we will also look at the different command line options you can use to modify the default behaviour of the server.

Running XSP ASP.NET Examples

Starting XSP with the ASP.NET examples that come with it is a bit of a no-brainer on both Linux and Windows, well, it is with the version 1.0.5 of XSP that I am using:

  • Starting XSP examples site on Windows
    • Simply go to to the Mono folder in the Programs or All Programs section of your Start Menu. For example, on my machine the Mono folder in the All Programs folder on the Start Menu is called Mono 1.0.5 for Windows.
      In this folder, go to the XSP subfolder and then click on XSP Test Web Server.
  • Starting XSP examples site on Linux
    • Starting the XSP server with the default settings on Linux is easier again. Simply change directory to the directory where the ASP.NET examples are installed using the command cd /usr/share/doc/xsp/test/ (this assumes that you have installed XSP in the /usr diretory).
      Now, to start XSP with the ASP.NET examples simply use the command xsp which will start XSP using the default settings.

On both Windows and Linux you should get output similar to the following on your screen:

Listening on port: 8080
Listening on address: 0.0.0.0
Root directory: /usr/share/doc/xsp/test
Hit Return to stop the server.

The above output is from starting XSP on Linux using the steps above. The main difference you will notice in the output from Windows is that the port that the server is listening on will probably be port 8088, which is the default port used by the Mono installer on Windows. You will also notice that the Root Directory will probably be something like C:\PROGRA~1\MONO-1~1.5\share\doc\xsp\test.

Regardless of the output, if XSP has started successfully simply fire up your browser and point it to http://localhost:<port num> where <port num> is the port number that is listed at the end of the the Listening on port: line of the output. On Linux you would generally use http://localhost:8080 and on Windows you would use http://localhost:8088. When the browser loads you should be presented with a web page similar to the one shown in Figure 1 below.

XSP web server on Linux showing ASP.NET examples pages.
Figure 1: XSP web server on Linux showing ASP.NET examples pages.

The page shown in Figure 1 is the home page of the example ASP.NET site that comes with XSP. It has numerous examples of ASP.NET Web Forms and ASP.NET Web Services.

XSP, close up.

The information in the section above shows how to start the examples website that comes with the XSP web server. However, chances are high that you will want to use the web server for actually serving your own web applications and Web Services. To this end we will now look at the command line options that you can use to modify the default behaviour of XSP. However, before doing that I want to point out where the XSP web server itself is located on both Linux and Windows:

  • Windows
    • <Mono install dir>\lib\mono\1.0\xsp.exe
      On my installation, a default Mono install, this relates to: C:\Program Files\Mono-1.0.5\lib\mono\1.0\xsp.exe

      I would recommend adding the directory <Mono install dir>\lib\mono\1.0\ to your path in Windows as this will allow you to start the XSP web server by typing xsp.exe at the command prompt. You should note however that if you do start XSP in this manner you will actually be running it on the .NET runtime and not on the Mono runtime… a good example of Mono – .NET interoperability.

  • Linux
    • <Mono install dir>/lib/mono/1.0/xsp.exe
      On my installation, using the instructions in the XSP on Linux section above, this related to: /usr/lib/mono/1.0/xsp.exe

      Note that on Linux the installation also places a script <Mono install dir>/bin/xsp that can be used from the console to start XSP. This is the command I used in the section above to start the XSP examples web site. When you type and run xsp at the console you are actually running the command mono /usr/lib/mono/1.0/xsp.exe

Now, on to the command line options for XSP, or to be more exact, for xsp.exe:

Option
–port N N is the tcp port to listen on.

Default value: 8080
AppSettings key name: MonoServerPort

–address addr addr is the ip address to listen on.

Default value: 0.0.0.0
AppSettings key name: MonoServerAddress

–root rootdir The server changes to this directory before doing anything else.

Default value: current directory.
AppSettings key name: MonoServerRootDir

–appconfigfile FILENAME Adds application definitions from the XML configuration file.

See sample configuration file that comes with the server.
AppSettings key name: MonoApplicationsConfigFile

–appconfigdir DIR Adds application definitions from all XML files found in the specified directory DIR.

Files must have ‘.webapp’ extension
AppSettings key name: MonoApplicationsConfigDir

–applications APPS A comma separated list of virtual directory and real directory for all the applications we want to manage with this server.

The virtual and real directories are separated by a colon. Optionally you may specify virtual host name and a port.
[[hostname:]port:]VPath:realpath,…

Samples:
/:.
the virtual / is mapped to the current directory.

/blog:../myblog
the virtual /blog is mapped to ../myblog

myhost.someprovider.net:/blog:../myblog
the virtual /blog at myhost.someprovider.net is mapped to ../myblog

/:.,/blog:../myblog
Two applications like the above ones are handled.

Default value: /:.
AppSettings key name: MonoApplications

–nonstop Don’t stop the server by pressing enter.
Must be used when the server has no controlling terminal.
–version Displays version information and exits.
–verbose Prints extra messages.
Mainly useful for debugging.

The above command line options where taken from the XSP web server itself

So, what does the above boil down to? Well, the easiest way to explain is to show some example command lines so here goes:

  • Windows: xsp.exe --port 80 --root c:\inetpub\wwwroot\
  • Linux: mono /usr/lib/mono/1.0/xsp.exe --port 80 --root /home/bdelahunty/asp.net/ --applications /:.,/articles:../documents/articles
  • Windows: mono "C:\Program Files\Mono-1.0.5\lib\mono\1.0\xsp.exe" --port 80 --root r:\WebApplications\ --nonstop

Now that we have looked at XSP it’s time to move on to getting Apache serving ASP.NET pages. If you don’t care about ASP.NET on Apache then you could always skip ahead to the Some Examples section where I run through a simple ASP.NET example.

ASP.NET on Apache with mod_mono

Apache is the web server of choice on Linux and is, according to netcraft[^], the most widely used web server on the net. This means that there are lots of websites out there that could be serving ASP.NET web applications and hosting ASP.NET Web Services. Mono makes this possible in the form of the mod_mono module for Apache which makes use of XSP or, to be more specific, it makes use of mod-mono-server.exe which is a specialized version of XSP.

In this section we will look at installing mod_mono and how to get Apache serving ASP.NET content. However, before we do that, we need to get Apache installed. As this article is aimed more at how to get Apache serving and hosting ASP.NET content I will quickly run over getting Apache installed. If you run into any difficulty when trying to install Apache just search the web for instructions on how to install Apache on your particular Linux distribution.

Oh, and one more thing. As I mentioned in the What are XSP and mod_mono? section, mod_mono currently does not work with Apache on Windows so I only look at Apache on Linux from here on.

Installing Apache

As with most things on Linux there are a number of different ways to install applications. In the case of Apache I’m just going to install from source as it is probably one of the easiest ways to install Apache and it is also the most distribution independent way of installing it that I know of. If you already have Apache 1.3.x or greater installed then you can skip ahead to the Installing and configuring mod_mono section. So, lets get Apache installed:

  1. First you will need to download the Apache source so head over to the Apache download page at http://httpd.apache.org/download.cgi[^].
  2. Download the latest version of the Unix Source for Apache. At the time of writing this article the latest version is 2.0.53. The file I downloaded was http://apache.mirrors.esat.net/httpd/httpd-2.0.53.tar.gz.
  3. Once the file has been downloaded, go to a console for the remainder of the process
  4. Decompress the downloaded file. For example, if you have downloaded the same file that I downloaded you could use the command tar -xvzf httpd-2.0.53.tar.gz as long as the file is in your current working directory.
  5. Next, switch to the directory where the source code has been decompressed to by typing cd httpd-2.0.53 or cd followed by the name of the directory that you decompressed the file to if it is different from mine.
  6. Now, before we can compile the source you need to configure the make files. To do this simply type ./configure --enable-so --prefix=/usr.
  7. When the the <CODE>./configure --enable-so --prefix=/usr completes you are free to go ahead and compile and install the source.
  8. Run the make command to compile Apache.
  9. You need to have root privileges to perform the next step successfully. If you have just installed Linux as a standalone machine you can usually give your account root privileges by typing the sudo bash command and entering the root password.
  10. Finally, to install, for lack of a better word, simply run the make install command. This will deploy the compiled binaries, libraries and other bits and pieces to the correct directories.

At this stage you should have Apache installed. To start the Apache web server type httpd at the console. You can check if Apache has started by pointing your browser to http://localhost/ where you should see a web page similar to the one shown in Figure 2.

Apache Web Server running on Linux
Figure 2: Apache Web Server running on Linux

Now that we have Apache installed it’s time to install mod_mono and configure Apache to host and serve ASP.NET content.

Installing and configuring mod_mono

Ok. We’re nearly there. Apache should be up and running by now so we don’t have too much left to do. We need to install mod_mono and then configure Apache in order to make it send the requests that we want through the mod_mono module and on to the mod-mono-server.exe application that will handle the request. As with Apache, I’m gong to run through installing mod_mono from the source. Before we actually go ahead and install mod_mono I should mention that there are some prerequisites to installing. You must have Mono installed, you must have XSP installed, and you must have Apache installed. If you’ve been following this article, and the previous article, then you should already have your system configured as needed.

Now, lets see if we can get mod_mono installed:

  1. So, once again, we need to download some source code. Head on over to the Mono download page at http://www.mono-project.com/downloads/index.html[^].
  2. In the source code section click on and download the “Apache Mono module”. I downloaded http://www.go-mono.com/archive/1.0.5/mod_mono-1.0.5.tar.gz.
  3. Once the file has been downloaded, go to a console for the remainder of the process
  4. Decompress the downloaded file using the command tar -xvzf mod_mono-1.0.5.tar.gz where mod_mono-1.0.5.tar.gz is the name of the file you downloaded.
  5. Next, switch to the directory where the source code has been decompressed to by typing cd mod_mono-1.0.5 or cd followed by the name of the directory that you decompressed the file to if it is different from mine.
  6. Now, before we can compile the source you need to configure the make files. To do this simply type ./configure --prefix=/usr .
  7. Next, to actually compile the mod_mono source you need to run the make command.
  8. The next step has to be performed using an account with root privileges. If you have just installed Linux as a standalone machine you can usually give your account root privileges by typing the sudo bash command and entering the root password.
  9. Finally, to install mod_mono simply run the make install command. This will deploy the compiled binaries, libraries and other bits and pieces to the correct directories.

That should have been painless. You should have mod_mono installed on your system now. So what’s left to do? Well, we need to configure Apache to use mod_mono to handle requests. This means we need to edit the Apache config files. If you have installed Apache following the instructions in the Installing Apache section the Apache config file should be the following file: /usr/conf/httpd.conf
Obviously the location of this file may be different if you installed Apache in a different location or if you installed it using a different method.

Firstly, we need to configure Apache to load the mod_mono module. To do this we need to edit the Apache config file, /usr/conf/httpd.conf, and add the following line to the end:

LoadModule mono_module /usr/modules/mod_mono.so

The above line tells Apache to load the module located at /usr/modules/mod_mono.so as the mono module.

The next thing that we need to do it configure Apache to serve some ASP.NET content. We will use the same ASP.NET Examples that we used with the XSP server above. To do this we need to tell Apache to serve content from the /usr/share/doc/xsp/test/ directory for a given path, lets say, /AspNetOnApache. This means that when you browse to http://localhost/AspNetOnApache you will be served the ASP.NET content in the /usr/share/doc/xsp/test/ directory.

Here are the additions you need to make to the Apache config file:

Alias /AspNetOnApache "/usr/share/doc/xsp/test"
MonoApplications "/AspNetOnApache:/usr/share/doc/xsp/test"

Finally, we need to tell Apache which module to use as the handler for requests. For Apache 1.3.x you can use:

<Directory /usr/share/doc/xsp/test>
    SetHandler mono
    <IfModule mod_dir.c>
        DirectoryIndex index.aspx
    </IfModule>
</Directory>

and for Apache 2.0.x you can use the above text OR you can use the following

<Location /AspNetOnApache>
    SetHandler mono
</Location>

Finally! We are there. Simply go to the console and type the command httpd to start Apache. Point your browser to http://localhost/AspNetOnApache and you should see a page similar to the one shown in Figure 3.

Apache web server on Linux showing ASP.NET examples pages.
Figure 3: Apache web server on Linux showing ASP.NET examples pages.

Some Examples

So after all that you should be able to serve ASP.NET content from at least one server on Linux. Just to help round off the “experience” I’ve decided to add a basic web application example to this article.

A simple web application example

Our simple web application is very simple indeed. It consists of a single page that displays some basic information about the machine that the page is hosted on and also some basic information about the request for the page. There are two files, index.aspx and index.aspx.cs, the former being the web page and the latter being the codebehind file for that page. So open up your favourite text editor or IDE and create the two files (hint: copy and paste is probably the quickest way):

index.aspx

<%@ Page language="c#" src="introtomono2/index.aspx.cs" 
         Inherits="SimpleWebApp.SimplePage" AutoEventWireup="false"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
   <HEAD>
      <title>Simple Page</title>
   </HEAD>
   <body>
      <form method="post" runat="server">
         <table width="450px" border="1px">
            <tr>
               <td colspan="2"><strong>Server Details</strong></td>
            </tr>
            <tr>
               <td>Server Name:</td>
               <td>
                  <asp:Label id="serverName" runat="server"></asp:Label></td>
            </tr>
            <tr>
               <td>Operating System:</td>
               <td>
                  <asp:Label id="operatingSystem" runat="server"></asp:Label>
               </td>
            </tr>
            <tr>
               <td>Operating System Version:</td>
               <td>
                  <asp:Label id="operatingSystemVersion" runat="server"><BR>                  </asp:Label>
               </td>
            </tr>
         </table>
         <br>
         <table width="450px" border="1px">
            <tr>
               <td colspan="2"><strong>Request Details</strong></td>
            </tr>
            <tr>
               <td>Page Requested:</td>
               <td>
                  <asp:Label id="requestedPage" runat="server"></asp:Label>
               </td>
            </tr>
            <tr>
               <td>Request From:</td>
               <td>
                  <asp:Label id="requestIP" runat="server"></asp:Label>
               </td>
            </tr>
            <tr>
               <td>User Agent:</td>
               <td>
                  <asp:Label id="requestUA" runat="server"></asp:Label>
               </td>
            </tr>
         </table>
      </form>
   </body>
</HTML>

index.aspx.cs

using System;
using System.Web.UI.WebControls;

namespace SimpleWebApp
{
   public class SimplePage : System.Web.UI.Page
   {
      protected Label operatingSystem;
      protected Label operatingSystemVersion;
      protected Label requestedPage;
      protected Label requestIP;
      protected Label requestUA;
      protected Label serverName;
   
      protected override void OnLoad(EventArgs e)
      {
         DisplayServerDetails();
         DisplayRequestDetails();

         base.OnLoad (e);
      }

      private void DisplayServerDetails()
      {
        serverName.Text = Environment.MachineName;
        operatingSystem.Text = Environment.OSVersion.Platform.ToString();
        operatingSystemVersion.Text = Environment.OSVersion.Version.ToString();
      }

      private void DisplayRequestDetails()
      {
         requestedPage.Text = Request.Url.AbsolutePath;
         requestIP.Text = Request.UserHostAddress;
         requestUA.Text = Request.UserAgent;
      }
   }
}

Once you have saved these two files we now need to be able to serve it. I saved the files to /home/bdelahunty/mono/webappexample/ and we now need to configure both XSP and Apache to serve pages from this location. As this is a simple web application example lets call it SimpleWebApp.

To get XSP to serve the page use the following command at the console (replacing /home/bdelahunty/mono/webappexample/ with the directory where you saved the files):

xsp --applications /SimpleWebApp:/home/bdelahunty/mono/webappexample/

To get Apache to serve the page simply add the following to the end of the Apache config file, /usr/conf/httpd.conf in my case (replacing /home/bdelahunty/mono/webappexample/ with the directory where you saved the files):

Alias /SimpleWebApp "/home/bdelahunty/mono/webappexample"
MonoApplications "/SimpleWebApp:/home/bdelahunty/mono/webappexample"   

You also need to add the following if you are using Apache 1.3.x:

<Directory /home/bdelahunty/mono/webappexample>
    SetHandler mono
        <IfModule mod_dir.c>
            DirectoryIndex index.aspx
        </IfModule>
</Directory>

and for Apache 2.0.x you can use the above text OR you can use the following:

<Location /SimpleWebApp>
    SetHandler mono
</Location>

Now just fire up your browser and point it to http://localhost/SimpleWebApp for Apache or http://localhost:8080/SimpleWebApp for XSP and you should see a page similar to the one shown in Figure 4.

The SimplePage.aspx file displaying the server and request             details.
Figure 4: The SimplePage.aspx file displaying the server and request details.

Problems with the Visual Studio .NET and IIS way?

If you are used to developing ASP.NET web applications with Visual Studio .NET then there are a few things you need to watch out for. When creating aspx pages Visual Studio .NET used the CodeBehind attribute in the pages @Page directive. However, CodeBehind is not actually an official ASP.NET @Page directive attribute and is only used by the IDE at design time and when compiling the ASP.NET application to a dll. You would normally then drop this compiled dll into the bin directory of your web application and IIS would automatically pick up the changes. This doesn’t work with XSP or mod_mono; you can’t just drop in a new pre-compiled dll and have the web server start using it.

You may or may not have noticed that I used the Src attribute in the @Page directive in the simple example above and I did not give any details about pre-compiling the web application to a dll that has to be deployed. I just pointed XSP and Apache to the directory where the index.aspx and index.aspx.cs files where located and the web servers handled the compilation of the web application the first time the page was called. This is one way you can get around the pseudo problem of not being able to just drop in a new pre-compiled dll. You can just drop in a new .cs codebehind file and the web server will compile it on the fly. This method does however require that you have all your codebehind files on the server. If you setup your permissions correctly then this is not an issue, if you don’t, it’s a big issue as any visitor to your site could potentially view the code behind your site and potentially come up with ways to hack it.

If you are used to the Visual Studio .NET and IIS way, or if you simply use Visual Studio .NET to build your web applications and want to just drop the pre-compiled dll into the bin directory then you can quickly overcome the problem by deploying your new dll and then restarting the web server. Use httpd -k restart to restart Apache after you have put in a new dll and if you are using XSP just shut it down and then restart XSP it.

What next?

Well hopefully this article has helped you to get some ASP.NET pages served from a Linux machine running XSP or Apache (and of course from XSP on Windows) and that it has given you a basic overview of using XSP and mod_mono. The next article in this “Introduction to Mono” series will either look at how you can get the latest version of Mono at any time using CVS or how to develop some GUI applications using Mono.

Note

When this article was written the latest stable version of Mono was 1.0.5. However, at the time of posting this article the latest stable version is 1.0.6.

Links

[For Debian]

Package mono-xsp

  • squeeze (oldstable) (cli-mono): simple web server to run ASP.NET applications – default version
    2.6.5-3: all
  • wheezy (stable) (cli-mono): simple web server to run ASP.NET applications – default version
    2.10-2.4: all
  • jessie (testing) (cli-mono): simple web server to run ASP.NET applications – default version
    3.8-2: all
  • sid (unstable) (cli-mono): simple web server to run ASP.NET applications – default version
    3.8-2: all

What is Mono ?

Mono provides the necessary software to develop and run .NET client and server applications on Linux, Solaris, Mac OS X, Windows, and Unix. Sponsored by Novell, the Mono open source project has an active and enthusiastic contributing community and is positioned to become the leading choice for development of Linux applications.

What is XSP ?

XSP is a “light-weight web server” capable of hosting and serving ASP.NET applications. It is written using C#, making extensive use of the classes in the System.Web namespace.XSP web server runs on both the Mono runtime and also on the .NET runtime.

Download Mono and XSP

http://go-mono.com/sources-stable/

Mono Screenshots

http://www.mono-project.com/Screenshots

Mono Documentation

http://www.go-mono.com/docs/

Mono FAQ

http://www.mono-project.com/FAQ:_General

Now we are going to take a look how to install XPS and mono for ASP.NET applications and testing this setup

Install mono in Debian

If you want to install mono in your debian system you need to run the following command

#apt-get install mono mono-gmcs mono-gac mono-utils monodevelop monodoc-browser monodevelop-nunit monodevelop-versioncontrol

Install XSP Web server in Debian

If you want to install ASP.NET2.0 version use the following command

#apt-get install mono-xsp2 mono-xsp2-base asp.net2-examples

If you want to install ASP.NET1.0 version use the following command

#apt-get install mono-xsp mono-xsp-base asp.net-examples

This will install all the required development environment and sample applications

Testing your ASP.NET Applications

We have already installed sample applications

If you want to run ASP.NET 2.0 application you need to go to /usr/share/asp.net2-demos/ run the following command

#xsp2

This will start the XSP with ASP.Net 2.0 application

xsp2
Listening on port: 8080 (non-secure)
Listening on address: 0.0.0.0
Root directory: /usr/share/asp.net2-demos
Hit Return to stop the server.
Application_Start

Now you need to point your web browser to http://localhost:8080/

You should seen the following screen

If you want to run ASP.NET 1.0 application you need to go to /usr/share/asp.net-demos/ run the following command

#xsp

This will start the XSP with ASP.Net 1.0 application

xsp
Listening on port: 8080 (non-secure)
Listening on address: 0.0.0.0
Root directory: /usr/share/asp.net-demos
Hit Return to stop the server.
Application_Start

Now you need to point your web browser to http://localhost:8080/

You should seen the following screen

Possible errors

If you get the following error when you point http://localhost:8080/

Error Message: HTTP 404. File not found /usr/share/asp.net2-demos/index2.aspx

Solution

You need to copy the index.aspx to index2.aspx

#cp /usr/share/asp.net2-demos/index.aspx /usr/share/asp.net2-demos/index2.aspx

[For CentOS]

CentOS 6 Mono and Apache

http://openhelp.net/?p=584

How to Install Mono on CentOS 5.2?

mod_mono is an Apache 2.0/2.2 module that integrates Mono’s ASP.NET support with the Apache HTTP server.
Here is the steps for installing mod_mono with Apache:-

[root@ htdocs]#yum install glib2-devel
[root@ htdocs]#yum install gcc-c++
[root@ htdocs]#yum install bison

Apache is installed from source. If it is not installed on your machine please refer the URL http://openhelp.net/?cat=45 for more details about compiling apache from source. Make sure that APXS is enabled while compiling Apache.Download all the required source packages which includes mono,mod_mono and xsp.

[root@ htdocs]#wget http://ftp.novell.com/pub/mono/sources/mono/mono-2.0.1.tar.bz2
[root@ htdocs]#wget http://ftp.novell.com/pub/mono/sources/xsp/xsp-2.0.tar.bz2
[root@ htdocs]#wget http://ftp.novell.com/pub/mono/sources/mod_mono/mod_mono-2.0.tar.bz2

Install the Mono core from the source:-

[root@ htdocs]#tar -vxjf mono-2.0.1.tar.bz2
[root@ htdocs]#cd mono-2.0.1
[root@ htdocs]#./configure
[root@ htdocs]#make
[root@ htdocs]#make install
[root@ htdocs]#cd ..

You need to install XSP next. Xsp is the web server for mono . it handles the compilation and delivery of .aspx files.

[root@ htdocs]#tar -vxjf xsp-2.0.tar.bz
[root@ htdocs]#cd xsp-2.0
[root@ htdocs]#./configure
[root@ htdocs]#make
[root@ htdocs]#make install
[root@ htdocs]#cd ..

You may get the error ” compiler could not find the file dotnet.pc” while compiling the XSP. For fixing this just execute the following command:-

[root@ htdocs]#export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig

Recompile XSP once again from our end. The error will not be there.

Check the xsp binary location.

[root@localhost xsp]# which xsp
/usr/local/bin/xsp

Finally we install mod_mono

[root@ htdocs]#tar -vxjf mod_mono-2.0.tar.bz2
[root@ htdocs]#cd mod_mono-2.0
[root@ htdocs]#./configure
[root@ htdocs]#make
[root@ htdocs]#make install

Once the compilation is over, you can find  the mod_mono.so file in the /usr/local/apache/modules directory.

You need to add the following entries to the httpd.conf:-

LoadModule mono_module modules/mod_mono.so

Include “/usr/local/apache/conf/mod_mono.conf”

DirectoryIndex index.htm index.html index.shtml index.xhtml index.wml index.perl index.pl index.plx index.ppl index.cgi index.jsp index.js index.jp index.php4 index.php3 index.php index.phtml default.htm default.html home.htm index.php5 Default.html Default.htm home.html index.aspx default.aspx

Here is the contents of the file /usr/local/apache/conf/mod_mono.conf.

[root@ htdocs]#cat  /usr/local/apache/conf/mod_mono.conf
MonoServerPath /usr/local/bin/mod-mono-server2 –verbose
MonoAutoApplication enabled
AddType application/x-asp-net .aspx
AddType application/x-asp-net .asmx
AddType application/x-asp-net .ashx
AddType application/x-asp-net .asax
AddType application/x-asp-net .ascx
AddType application/x-asp-net .soap
AddType application/x-asp-net .rem
AddType application/x-asp-net .axd
AddType application/x-asp-net .cs
AddType application/x-asp-net .vb
AddType application/x-asp-net .config
AddType application/x-asp-net .Config
AddType application/x-asp-net .dll
DirectoryIndex index.aspx
DirectoryIndex Default.aspx
DirectoryIndex default.aspx

Restart apache and make sure that mono is running on the server.

[root@localhost ~]# pstree | grep mono
|-mono—6*[{mono}]

So we have successsfully installed mono on the server,
Create a test page index.aspx under the default document root of the server.
Here is the code:-

[root@localhost ~]#cat  index.aspx

=========================
<html>
<body>
<% Response.Write(“Hello ..Samle Asp Page”); %>
</body>
</html>
==========================

Try to load the test  page now.
http://hostname/index.aspx should be loading fine now.

Configuring XSP to serve ASP pages:-

you need to start the XSP server with the default settings .
Simply change directory to the directory where the ASP.NET examples are installed.

[root@localhost htdocs]# cd /usr/local/lib/xsp/

Start the XSP with the ASP.NET examples. Just execute the following command from the console.

[root@localhost xsp]# xsp
xsp
Listening on address: 0.0.0.0
Root directory: /usr/local/lib/xsp
Listening on port: 8080 (non-secure)
Hit Return to stop the server.

———————————————————————-

[Above process may create Error]

I am very new in linux environment as well as mod_mono. I installed mod mono on a CentOS server and it seems that I am having problem. I follow the step by step instruction on this site http://openhelp.net/?p=584. The only change with the steps stated on the site I’ve given is that I have used the latest mod_mono which is Mono 2.4.2.3. Below are the error message I am receiving when restarting apache using the command /sbin/service httpd restart :

[Mon Oct 12 10:46:33 2009] [crit] (2)No such file or directory: Failed to create shared memory segment for backend ‘/usr/local/bin/mod-mono-server2’ at ‘/tmp/mod_mono_dashboard_/usr/local/bin/mod-mono-server2_2’.

Here is my mod_mono.conf:

LoadModule mono_module /usr/local/apache/modules/mod_mono.so

#MonoServerPath /usr/local/bin/mod-mono-server2 .verbose
MonoServerPath /usr/local/bin/mod-mono-server2 -verbose
MonoAutoApplication enabled

AddType application/x-asp-net .aspx
AddType application/x-asp-net .asmx
AddType application/x-asp-net .ashx
AddType application/x-asp-net .asax
AddType application/x-asp-net .ascx
AddType application/x-asp-net .soap
AddType application/x-asp-net .rem
AddType application/x-asp-net .axd
AddType application/x-asp-net .cs
AddType application/x-asp-net .vb
AddType application/x-asp-net .master
AddType application/x-asp-net .sitemap
AddType application/x-asp-net .resources
AddType application/x-asp-net .skin
AddType application/x-asp-net .browser
AddType application/x-asp-net .webinfo
AddType application/x-asp-net .resx
AddType application/x-asp-net .licx
AddType application/x-asp-net .csproj
AddType application/x-asp-net .vbproj
AddType application/x-asp-net .config
AddType application/x-asp-net .Config
AddType application/x-asp-net .dll
DirectoryIndex index.aspx
DirectoryIndex Default.aspx
DirectoryIndex default.aspx

And here is what I added on my httpd.conf:

User apache
Group apache

LoadModule mono_module modules/mod_mono.so
Include “/usr/local/apache/conf/mod_mono.conf”

DirectoryIndex index.html index.shtml index.xhtml index.wml index.perl index.pl index.plx index.ppl index.cgi index.jsp index.js index.jp index.php4 index.php3 index.php index.phtml default.htm default.html home.htm index.php5 Default.html Default.htm home.html index.aspx default.aspx

I can run $ xsp command on the directory where I have my aspx pages. The page runs smoothly with xsp command but it stop the service as soon as I close my shell. My only problem is that, how can I keep it alive without the use of that command? Or is there a way to automatically start the xsp command on that directory when I restart my apache? Hoping for your response guys.

———————————————————————-

https://centos.org/forums/viewtopic.php?f=20&t=20596

[On CentOS Just Do]

1. yum groupinstall Mono
2. yum install mod_mono
3. yum install xsp

 

How to Install XSP and Integrate XSP With Apache 2 Under CentOS 5

How to Install XSP and Integrate XSP With Apache 2 Under CentOS 5

In the last article, I have introduced How to Install Mono 2.4.2.3 on CentOS 5 , forgot telling you, why I want to install Mono on CentOS, I want to try to run ASP.NET under Linux.

Mono has an implementation of ASP.NET 2.0 and ASP.NET AJAX, and to run your ASP.NET applications with Mono, you have three classes of options:

  • Apache hosting: use mod_mono, a module that allows Apache to serve ASP.NET applications.
  • FastCGI hosting: use the FastCGI hosting if you have a web server that supports the FastCGI protocol for extending the server. You also may use a web server that only has support for CGI using cgi-fcgi.
  • XSP: this is a simple way to get started, a lightweight and simple webserver written in C#.

XSP is a standalone web server written in C# that can be used to run your ASP.NET applications with minimal effort.
The mod_mono Apache module is used to run ASP.NET applications within the Apache (http://httpd.apache.org) web server.

Let’s continue install XSP and mod_mono now.
1.Go to this page and download xsp-2.4.2.tar.bz2,
#wget http://ftp.novell.com/pub/mono/sources/xsp/xsp-2.4.2.tar.bz2

2. Extract the download file,
# tar jxvf xsp-2.4.2.tar.bz2

3. In to the xsp-2.4.2 folder,
#cd xsp-2.4.2

4.configure, build and install XSP,
# ./configure –prefix=/opt/mono; make; make install
because I installed mono in /opt/mono, so the same treat to XSP

5.go to the parent folder,
#cd ..

6.by default, Apache 2 has been installed, make sure you have installed httpd-devel, or you will get error message like”**** apxs was not found,” when you build mod_mono,
#yum -y install httpd-devel

7.Go to this page and download mod_mono-2.4.2.tar.bz2

8. Extract the download file,
# tar jxvf mod_mono-2.4.2.tar.bz2

9. In to the mod_mono-2.4.2 folder,
#cd mod_mono-2.4.2

10.configure, build and install mod_mono,
# ./configure –prefix=/opt/mono; make; make install

11.You may want to verify a few thigns to make sure the configuration is ready to rock. In my case, I am keeping the mono configuration in a separate file for sanity sake. You can do that or put it all in your httpd.conf, it’s up to you,

<IfModule !mod_mono.c>
LoadModule mono_module /usr/lib/httpd/modules/mod_mono.so
AddType application/x-asp-net .aspx
AddType application/x-asp-net .asmx

AddType application/x-asp-net .ashx
AddType application/x-asp-net .asax
AddType application/x-asp-net .ascx
AddType application/x-asp-net .soap
AddType application/x-asp-net .rem
AddType application/x-asp-net .axd
AddType application/x-asp-net .cs
AddType application/x-asp-net .config
AddType application/x-asp-net .Config
AddType application/x-asp-net .dll
DirectoryIndex index.aspx
DirectoryIndex Default.aspx
DirectoryIndex default.aspx
</IfModule>

<VirtualHost *:80>
DocumentRoot /home/httpd/aspx/html
ServerName aspx.yoursite.com
Alias /demo /opt/mono/lib/xsp/test
MonoApplications "/demo:/opt/mono/lib/xsp/test"
MonoServerPath /opt/mono/bin/mod-mono-server2
<Directory /opt/mono/lib/xps/test>
SetHandler mono
</Directory>
</VirtualHost>

12.restart httpd service,
#service httpd restart

13.Test it,
visit http://aspx.yoursite.com/demo in your browser, and if you get the page with the title “Welcome to Mono XSP!”, congratulation!

references,
ASP.NET and Mono
My Adventures Installing mono 2.0 on CentOS 4 to work with Apache via mod_mono

My Adventures Installing mono 2.0 on CentOS 4 to work with apache via mod_mono

Apparently the good folks over at the mono project decided to discontinue binary packages for the Red Hat line of linux distributions.  It’s a shame in a way, there are a lot of those installation out there, so it would be nice to keep things updated through yum or apt-install or rhupdate, etc..

On the up side, installing from source has never been easier.  In the past I have went through many hours of trying to get the right versions of different libraries that were needed.  With the official release of 2.0 it seems much better.  I thought I would share the steps that I went through.

Disclaimer: This worked on a fairly fresh install of CentOS 4.7.  I have not tried it on 5.x, nor on any other flavor of linux (SUSE, Ubuntu, etc..) so your mileage may vary.

At the time of the install (and this writing) the current mono stable version is 2.0.1 so all references will be to that version.  Here are the steps that I went through.

Preparation

Always be prepared – Boy Scouts motto…

In rooting around the web I did find a few helpful pointers.  First, make sure you have gcc installed.  Now this is one of those duh pieces of information, but in the fairness of completeness I thought I would mention it. (Note: If you do not have gcc or bison, install them! Credit The_Assimilator’s comment)

# yum install gcc-c++
# yum install bison
Next I installed the httpd-devel package.  I had read (will find the link later) that it helps some of the installation down the line.  In my case I just use yum to install it. (Note:  httpd-devel package is required by the mod_mono compile if apxs (Apache Extension Tool) is not on your machine. credit to The_Assimilator’s comment)
yum install httpd-devel

You may also require the glib-2.0 libraries (thanks to Michael Walsh for that bit).  If you receive the error “Package glib-2.0 was not found in the pkg-config search path” you can install it via yum as well.

yum install glib2-devel

The Main Dance

Next comes the meat of the installation.  First, I downloaded the necessary source packages.  I simply used wget to snag the core mono package, xsp (mono web server) and mod_mono (apache integration).

wget http://ftp.novell.com/pub/mono/sources/mono/mono-2.0.1.tar.bz2
wget http://ftp.novell.com/pub/mono/sources/xsp/xsp-2.0.tar.bz2
wget http://ftp.novell.com/pub/mono/sources/mod_mono/mod_mono-2.0.tar.bz2

Next we install the mono core

tar -vxjf mono-2.0.1.tar.bz2
cd mono-2.0.1
./configure
make
make install
cd ..

Next comes xsp

tar -vxjf xsp-2.0.tar.bz
cd xsp-2.0
./configure
make
make install
cd ..

At this point I recevied an error (I believe it was in the make process) that the compiler could not find the file dotnet.pc.  I found that it was indeed on my system so I simply had to export the path and then finsih the compile.

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
make
make install
cd..

Note: Make sure the file dotnet.pc is in that location.  If not, adjust the path above.

Finally we install mod_mono

tar -vxjf mod_mono-2.0.tar.bz2
cd mod_mono-2.0
./configure
make
make install

There, easy huh?

Configuration

You may want to verify a few thigns to make sure the configuration is ready to rock.  In my case, I am keeping the mono configuration in a separate file for sanity sake.  You can do that or put it all in your httpd.conf, it’s up to you.

<IfModule !mod_mono.c>
    LoadModule mono_module /usr/lib/httpd/modules/mod_mono.so
    AddType application/x-asp-net .aspx
    AddType application/x-asp-net .asmx
    AddType application/x-asp-net .ashx
    AddType application/x-asp-net .asax
    AddType application/x-asp-net .ascx
    AddType application/x-asp-net .soap
    AddType application/x-asp-net .rem
    AddType application/x-asp-net .axd
    AddType application/x-asp-net .cs
    AddType application/x-asp-net .config
    AddType application/x-asp-net .Config
    AddType application/x-asp-net .dll
    DirectoryIndex index.aspx
    DirectoryIndex Default.aspx
    DirectoryIndex default.aspx
</IfModule>
That was it.  I hope that helps!