First and foremost: I’m not a Linux developer, but I love using Linux to run my asp.net web applications through mono. And I use it more and more every month. So even though I might not be a Linux Wizard, I can probably help other Windows developers understand how to work on Linux. Anyway, here goes.
Installing Mono on Linux
I’m using the SUSE Linux distribution; specifically I’m using the 64 bit Amazon edition of Suse Linux. I’ve found it easier to deploy mono to Suse than other distributions. It might not be true anymore, and it might have been easier on other distributions if I knew more about Linux in general 🙂
However, installing mono under Suse is really easy.
First, you just have to add the relevant repository. Later we can install mono from this repository:
zypper addrepo http://download.mono-project.com/download-stable/openSUSE_11.4 mono-stable zypper refresh --repo mono-stable
Important: You might experience that you are prompted to trust the repository key. Hit ‘a’ to always trust the key. Now upgrade the repository:
zypper dist-upgrade --repo mono-stable
Finally, install mono from the repository:
zypper install -y mono-complete
On my AWS Micro instance this takes a couple of minutes. This should do the trick. Type “mono –version” to ensure that mono has been installed.
Installing Apache with Mono on Linux
Now that Mono is installed, it is time to install the Apache Web Server. Apache works quite different from IIS, but you can actually come to appreciate it 🙂
We’ll start with the basic installation of Apache:
zypper install -y httpd touch /etc/apache2/sysconfig.d/include.conf rcapache2 start zypper install -y xsp zypper install -y mod_mono
In the first line httpd is installed. On Linux, things that ends with a “d” are usually “daemons” – somewhat equivalent to Windows services. So httpd is the Apache HTTP Daemon.
rcapache2 is a command for performing basic commands on Apache, for example the “start” command. It takes other parameters such as “stop” and “restart”.
Xsp is the mono web server. It can be nice to install for various testing purposes, but it shouldn’t be necessary. I usually always install it.
Finally the “mod_mono” component is the Apache module that runs mono. Apache plugins are often prefixed by “mod_”, for example mod_ssl.
Now that Apache is installed, we need to configure a VirtualHost on Apache. A VirtualHost is somewhat equivalent to a Virtual Directory in IIS. Actually the name VirtualHost probably makes more sense than Virtual Directory, I think.
Basically, the VirtualHost is given a PORT number, and you point it to some location on the file system. Anything ingoing on the specified port will then be executed by the web application running at the specified location on the file system. To glue these two together (to establish an actual runtime for executing your mono project), you need help from mod_mono. More on this below.
The VirtualHost is configured in a domain specific language which is almost an XML structure. In the beginning I found it hard to accept that this looks so much like XML, but it is far from being XML compliant. My advice is just to accept this. Yes, it’s ugly, but that’s how it is:-)
The actual file structure of the configuration files depends between the Linux distributions, but on Suse Linux the VirtualHosts are placed in the /etc/apache2/conf.d folder. Suse Linux is setup to automatically load all configuration files ending in .conf in the conf.d folder. So, go to this folder by typing:
Create a file with a custom name ending in .conf – for example myWebServer.conf.
Open the file and paste this code (replace ServerName and ServerAdmin with your own values):
Now, apache is configured with mod_mono. This VirtualHost will listen to all incoming traffic on port 80. You can specify the server name, the server admin in accordance with your own data.
The DocumentRoot directive is important and points to the folder on your file system that contains your web site. We haven’t created the web site yet, but we’ll do so in a minute. The MonoServerPath specifies which executable will run mod_mono. The number 4 signals, that we’re using the .NET 4 runtime. Mono has runtimes for other .NET versions as well.
The Location directive takes the parameter “/” which maps to the root location of the web site folder (specified in DocumentRoot above). Here some apache specific stuff can be set. I’ll cover the details of this in a later blog post.
Create your site
Allright, the DocumentRoot now points to a location on your server. In my code above it points to /srv/www/mysite. Create this folder and go to it by typing:
mkdir /srv/www/mysite cd /srv/www/mysite
Create a file named index.html by typing:
echo "<h1>Hello world</h1>" >> index.html
Finally, you need to allow the webserver to access the file and restart apache:
chmod 777 /srv/www/mysite -R rcapache2 restart
Now you should be able to access your service in a normal browser, and it should return Hello World.
You can deploy your ASP.NET application to this folder, and it should work.
If you experience that changes you make are not taking effect, try restarting apache.
Making Apache start automatically on reboot
The Apache Daemon does not automatically start itself on boot. Often this is what you want for a web server. To enable this, type:
chkconfig --level 2345 apache2 on