Y'herd thisun? 

“Philip also watched and learned from the democratic assembly at Thebes. He saw the grave weakness of a system in which every man could voice his opinion and vote. Debates in the assembly were endless, while political parties worked to destroy the power of their rivals. Philip began to see how an old-fashioned monarchy like Macedonia could act much more decisively than a Greek city and be unstoppable”
-Philip Freeman

from Alexander The Great

MONO/Ubuntu part 9 - Configure MONO on Apache2

TaggedMONO, Linux

Configuring Mono on Apache2

The 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.Net

Mono 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 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.

What we're showing in this tutorial is a middle ground setup, mixing the easy MonoAutoApplication option with virtual hosts files to let a machine have multiple sites quickly. This style is akin to the Windows IIS5 ASP.Net model, which is simple and works fine for many needs but has the point against it that all sites share a single instance of Mono; so if one site goes bad it can take out the others. After you are comfortable with a MonoAutoApplication-based system, you should look into a full manual deployment which can be configured more similar to an IIS6/7 installation using separate w3wp.exe workers (ApplicationPools) for hosting appdomains.

The full manual setup isn't very hard, it just takes more typing of settings in virtual host files, lines that are very similar to those found in the mod_mono.conf and mod_mono_auto.conf.

My suggestion for folks who are getting their feet wet with Mono on Linux/Apache is to first use the MonoAutoApplication method for a while until you are more comfortable surfing the system configuration files, and then move up to manual as needed.

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.Net

The 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.

The disk partition that /var/ lives in can have limited storage space but adding more or just pointing to a larger drive area using a symbolic link isn't a great pain when the need arises. And neither is just putting your files elsewhere whenever you want and quickly editing the Virtual Hosts file to do a repoint.

Some forum posters rag on using /var/www/, but personally I've not seen any single recommended one-size-best-for-all alternative.

I *have* seen the recommendation of creating sites in children of the /usr/ folder because these experts say that "usr" logically stands for "users"... but others say that "usr" stands for "shared user files" and like the "Program Files" folder in Windows it's not for direct-human use... and other authorities are adamant about not going near /usr/ because they say that "usr" does not stand for "USER" at all and instead it stands for "Unix System Resources" which should be off-limits to any human mucking.

Linux. Warlords and Kings telling the rubes how dumb we are... and never giving a straight answer :(.

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.

sudo mkdir -p sites/asdf

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.

<p>i am the simple HTML file</p>

And put a simple quickie 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#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
lblTest.Text = String.Format("I am the test page<br />{0}<br /> on {1}",
<form id="form1" runat="server">
<asp:Label ID="lblTest" runat="server"></asp:Label>

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>
DocumentRoot /home/kilgore/sites/asdf

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:

sudo a2ensite asdf

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

:). 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 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, "" 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 "", 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:

  • 1: Why?
  • 2: Installation
  • 3: Update the OS with APT
  • 4: Remoting to the box with SSH
  • 5: NANO quickies for 95% of the jobs
  • 6: Firewalling Ubuntu 8.10 Server
  • 7: Installing Apache2 and MONO
  • 8: Test client host files
  • 9: Configure MONO on Apache2
  • 10: Apache default pages
  • 11: Handling Apache and Mono Errors
  • 12: Subdomains and Christian porn
  • 13: Virtual Host Tweak: unmanaged cAse sensitiviTy
  • 14: Managed cAse sensitiviTy
  • 15: Managing files and folders
  • 16: Logging Apache Accesses and Errors
  • 17: Ubuntu Task Managers
  • 18: Ubuntu Services and Service Managers
  • 19: Installing Oracle 10g XE Server
  • 20: Connecting Mono to Oracle

  • home     who is smith    contact smith     rss feed π
    Since 1997 a place for my stuff, and it if helps you too then all the better