.Net on Linux (Fedora Core 2)
Introduction
The .Net platform is Microsoft’s answer to Java. Java was/is a favorite for enterprise application developers who require cross-platform operability, object oriented design and scalability. However, .Net has been adopted very quickly given the dominance of its creator in IT circles. This platform offers some improvements over J2EE and also allows you to program in one of many languages which all compile to the same intermediate language. The .Net runtime does not care in which language the program was written.
One of the big advantages that .Net provides is the coding of ASPX pages. The concept of code-behind allows clean separation of UI and logic (well, some people still make a pudding out of it). Java offers this functionality too now. Competition is good indeed.
Until now, the major drawback of ASP.Net was that it only ran on Windows platform. Windows isn’t as secure, reliable or scalable as Unix. Now we have the open source and cross-platform project called Mono. It implements the .Net runtime which apparently is an open standard. It is a new project, but seems to be fairly usable already. One can program applications in Windows and run on Linux – an ideal combination for many. The only thing to watch for is the changing nature of this ‘standard’. Microsoft isn’t known for sticking to standards.
This is a log of my efforts to install Mono on my Linux server. It should also serve as a guide to anyone attempting to do the same. Mono is relatively new and is progressing fast. But the installation process is still intimidating and the setup requires manual configuration.
My System
- Fedora Core 2
- Apache version 2.0.51
Initial attempts
A few months ago, I had attempted to install individual RPM packages. But there were some circular dependencies which I did not know how to resolve. Then I tried yum, and that seemed to work to a certain extent. But recently, the yum repository for Fedora Core 2 seems to have disappeared. The repositories don’t seem to be maintained well.
Given this, I download the source code and tried compiling. Mono was easy to compile and install. Simply do the standard ./configure, make and make install. XSP, the standalone server also compiled and installed in similar fashion. mod_mono, the module which allows you to run ASP.Net applications in conjunction with Apache would not compile. It looks for a script from Apache called apxs. This is not installed on Fedora. I found the RPM package for it called apache-devel, but then the script complained about not finding some other files. So I gave up on this. After all any production-type scenario will require integration with Apache. XSP isn’t designed for heavy-duty work.
Something that works
This is my most current installation using RPMs. It seems to work even though some of the packages aren’t specifically for Fedora Core 2. Here is the list of packages that were needed (my main goal being getting ASP.Net to work) –
- mono-core-1.1.9-0.novell.i586.rpm (download)
- mono-data-1.1.9-0.novell.i586.rpm (download)
- mono-web-1.1.9-0.novell.i586.rpm (download)
- xsp-1.1.9-0.novell.noarch.rpm (download)
- mod_mono-1.1.9-0.fedora3.novell.i386.rpm (download)
These packages must be installed in the order above. These are the bare-minumum required. If you need additional function, you may download them from the following website –
Notice that the mod_mono package is for Fedora Core 3, but it also works for Core 2. Download each of the packages and type ‘rpm –install package_name’
Testing the installation
The first order of business is to familiarize yourself with the files that were installed.
C# compiler executable | /usr/bin/mcs |
Mono runtime executable | /usr/bin/mono |
Class Libraries | /usr/lib/mono |
XSP binaries | /usr/lib/xsp |
XSP startup scripts |
/usr/bin/xsp /usr/bin/xsp2 |
Sample ASP.Net pages | /usr/lib/xsp/test |
mod_mono.so (for Apache) | /etc/httpd/modules/ |
While browsing these directories, you will notice two different versions of XSP and libraries – 1.0 and 2.0. These correspond to the old and the new .Net versions.
Compiling Hello World!
Listed below is the most basic, command-line C# program. Create a file called hello.cs and type in the code below.
hello.cs
public class Hello1 { public static void Main() { System.Console.WriteLine("Hello, World!"); } }
You are now ready to compile your very first program.
[sumit@europa utils]$
This creates a file called ‘hello.exe’ which you can execute using mono runtime.
Hello, World!
Testing XSP
If everything worked as expected, you can start testing standalone web server written in C# called XSP. By default, it will process aspx pages present in the current directory. It listens on port 8080. To check this, do the following –
[sumit@europa utils]$ cd /usr/lib/xsp/test [sumit@europa test]$ xsp xsp Listening on port: 8080 Listening on address: 0.0.0.0 Root directory: /usr/lib/xsp/test Hit Return to stop the server.
Using any browser, point to the server at port 8080, you should see the sample applications installed in /usr/lib/xsp/test.
Testing with Apache
The final step is to install mod_mono so that we can use Apache to serve .aspx pages. To accomplish this, open up /etc/httpd/conf/httpd.conf using a text editor and insert the lines highlighed in blue below –
LoadModule file_cache_module modules/mod_file_cache.so LoadModule mem_cache_module modules/mod_mem_cache.so LoadModule cgi_module modules/mod_cgi.so LoadModule mono_module modules/mod_mono.so
Alias /icons/ "/var/www/icons/" <Directory "/var/www/icons"> Options Indexes MultiViews AllowOverride None Order allow,deny Allow from all </Directory> # # Mono ASP.Net applications # MonoServerPath /usr/lib/xsp/2.0/mod-mono-server2.exe Alias /samples "/usr/lib/xsp/test" MonoApplications "/samples:/usr/lib/xsp/test" <Directory /usr/lib/xsp/test> SetHandler mono </Directory>
Note that I chose to use version 2.0 in the MonoServerPath. It is upto you which one you want to use. Now restart the web server.
[root@europa test]# /etc/init.d/httpd restart Stopping httpd: [ OK ] Starting httpd: [ OK ]
Open your browser and point to http://servername/samples/. You should see the same page which you saw with XSP on port 8080. The only difference is that Apache handles the .aspx requests now by forwarding them to a slightly modified version of XSP called mod-mono-server2.exe. You can verify that the web server did indeed start this up by looking at the process list owned by apache. One of the lines should contain mod-mono-server2.exe
By now you should have a fairly good understanding of the Mono installation. The next step is to start coding some interesting applications.