Posted March 31, 2009 12:00AM by Robert Smith
Tagged: MONO, Linux
Configuring Mono on Apache2The basics: - You can get a single simple Mono web app running with one line of configuration code, but to have more control and multiple sites you should get into the VirtualHosts files. Virtual Host files aren't hard and the sooner you start the more ready you'll be when you *need* their power.
- Apache "Modules" are like ISAPI Filters. Install them, tell Apache that you want to use them and on what sites, enabled them, they start intercepting requests. Mono itself is a module, your previous one-line installation made it available and enabled Apache-wide so you're just one nano away from ASP.Linux.
- Sites in Apache, just like sites in IIS, can be set up and selectively enabled/disabled.
It's really the same stuff as doing ASP.Net on Windows, just that you have to mix file editing and commandlines rather than just clicking your way to release. One line to ASP.NetMono features an instant-on option, allowing a machine to have a single ASP.Net site active using just the default Apache file location and settings. To turn this on, navigate to the Apache service folder and edit the main Apache configuration file httpd.conf. sudo nano /etc/apache2/httpd.conf
The httpd.conf file is empty to start, the line to add is - case sensitive - "MonoAutoApplication enabled" 
After saving the file, force Apache to reload its configuration information... sudo /etc/init.d/apache2 reload
Or use the human-brain style: sudo service apache2 reload
... and call up the test ASP.Net page again (the codebehind quickie from the Installing Apache and Mono page). 
The webform now appears. And pressing the button shows the server time and the account under which Apache2 runs on the server machine. 
Ubuntu 9.04 tip:There's a bug in the mono 2 installer that neglects to create a required symbolic link for the newer c# compiler. If you install monodevelop then all is fine but if you just want to run Mono based apps without the IDE on a box you hit the issue. If instead of getting the .Net page you get a .Net error dump with the header "Standard output has not been redirected or process has not been started", try these added steps: sudo find /usr/bin | grep gmcs
If the result of that only shows the c#2 compiler ("/usr/bin/gmcs2"), make a link to trick Mono to use that under the name of the older compiler with this command: sudo ln -s /usr/bin/gmcs2 /usr/bin/gmcs
Restart apache ("sudo service apache2 restart") and try the page again. That trickfix came from "Kitt's" comment here. Limitations of staying here... and where are we anyway?Some tipsites for folks who just want to get a single domain running on a machine will often stop right here and let you add and add and add asp.net files to /var/www/. However, once you put a web app on a machine, it's not long before you want or need to add more. Perhaps you're running a unit that's tasked with putting up a number of different web apps for BI and you want to assign different siteletts to different developers to avoid them stepping on each others files, or you have great bandwidth and you want to offer a slice of your machine to friends for their own web sites. In any case, extending from one to many only takes a few more sessions in nano. For a "V8 Moment", wanna see why you had to put your monotest webform files in /var/www/? Run this line. cat /etc/apache2/sites-enabled/000-default
Right near the top of that xml-ish ini file is the setting for DocumentRoot. And the value is pointing to the place we told you to copy your test files. 
Technically speaking, you can just use nano on that file (referred to as a "Virtual Hosts file") and change the DocumentRoot and related <Directory /var/www/> node to point to some other physical location. It will work. But the correct way to do such changes are to create/edit file(s) in /etc/apache2/sites-available/ and use a command to copy the settings over to /sites-enabled/. This is what we meant at the top when we said that sites can be selectively enabled and disabled. It does seem redundant and even silly at first, because the files are right there, but it's a file-based version of the IIS practice of creating websites and right clicking the MMC snap-in to turn them on and off. Setting the location of your web pages isn't the only option that a Virtual Host file provides. This is the place where you specify log file locations, permissions for sub folders, custom Apache file extention mapping and more. Let's create a new site configuration from scratch to get the gist. The simplest Virtual Hosts file for Mono ASP.NetThe Apache installation created that default location /var/www/ for its "It Works" confirmation html file, and building off of /var/www/ by adding subfolders is an acceptable way to go in many cases. For the point of learning, let's pick a different physical location for our web site. We'll make the site right in our user's home folder. This is a typical configuration, as is the related technique of creating new "users" that are not people at all, but just placeholders for web applications being developed by multiple actual humans. BTW: for Windows users, making the site location in a user folder adds the benefit of making cross-box file management easier by eliminating the extra mv calls in PuTTY after dragdropping with WinSCP. Navigate to your home folder and create a set of subfolders for the site data. We'll make a hierarchy with a top level non-webapp folder called "sites" and subfolders in that for each site, with "asdf" being our first site. Note the use of the "-p" switch, allowing us to create both the parent and child folders in one call. 
Now, use nano to put a simple html file in there to test non-managed Apache handling. sudo nano /home/kilgore/sites/asdf/default.htm
Type this or copypaste it into nano (copy this code to your clipboard and right click in the PuTTY shell) and CTL-X to save. <html>
<body>
<p>i am the simple HTML file</p>
</body>
</html>
And put a simple quickie asp.net form in the /sites/asdf folder. sudo nano /home/kilgore/sites/asdf/inline.aspx
Copy this into nano (copy this code to your clipboard and right click in the PuTTY shell) and CTL-X to save. <%@ Page Language="C#" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
lblTest.Text = String.Format("I am the asp.net test page<br />{0}<br /> on {1}",
System.Environment.CurrentDirectory,System.Environment.MachineName);
}
</script>
<body>
<form id="form1" runat="server">
<asp:Label ID="lblTest" runat="server"></asp:Label>
</form>
</body>
</html>
Now the fun part, tell Apache about the site. Use nano to create a site configuration file in /etc/apache2/sites-available/ folder. sudo nano /etc/apache2/sites-available/asdf
Add an "xml-ish node element" stating the Virtual Host will use the standard web port 80, and within that "node" add a line for the primary hostname (your www.* value) with the setting of "ServerName" and a line for the the physical file location setting "DocumentRoot". Remember that all this is case-sensitive and don't forget to close the "node". <VirtualHost *:80>
ServerName www.asdf.com
DocumentRoot /home/kilgore/sites/asdf
</VirtualHost>
Saving the file and restarting/reloading Apache isn't enough to get your new site working. You've made the file in sites-available but Apache reads from sites-enabled. To get the config copied over so that Apache will see it you have to use the command "a2ensite" (Apache2ENableSITE), such as: The a2ensite command can be called from anywhere and doesn't require the full path to the configuration file, Apache knows where the default folders are. The reversal of a2ensite is "a2dissite [file name]" (note the two s's). NOW, you can reload/restart the Apache2 service and give it a whirl. To test in a browser, remember to use the full domain url that you put in the configuration file AND that the same full domain value is in the client machine's hosts file, in our case the domain value is www.asdf.com. 
:). How 'bout that? Nuthin' new under the sun. Just as in IIS, if you don't want generic requests on a folder to show a directory listing you have to set a default "index" file or otherwise force a redirect. We'll do that in a second, but you can test that both the static html AND the asp.net files work by clicking on them in the list (andor typing the full paths in the browser). 

Yay! Revel in the moment! Adding more webapps/sites to the machine is now just a matter of creating and enabling more virtual hosts files andor adding more <Virtual Host /> xml sections to an existing virtual host file. Separate files lets you switch sites on an off more selectively and helps protect existing sites from typing accidents, but if you want just one file to hold settings for a group of or all of a machine's sites it's allowed. Play with your working webapp and you will come up with some nasties beyond the lack of default page redirection. Like, "http://www.asdf.com/inline.aspx" works but "http://WwW.Asdf.COM/INline.asPx" does not (Linux is case-sensitive, remember?), and what to do about page and resource 404's (and 403's and 500's and and and), and just as in IIS you have to handle managed and static files, and what about shutting down directory listing of non-managed subfolders such as CSS and Image folders, and how about requests for subdomains like "http://tryhacking.asdf.com", and logging for non-managed errors and events? Worry not. You set up ASP.Net in IIS all the time and now the only difference - except for the case-sensitivity -is that what you do by rote you have to think about again like the olde days and test a bit more.
Up next: Start getting good errors and take care of the default page.
jump to: |