Setting Up Apache Virtual Hosts With mod_mono

Joe Audette

Updated

In this article, I will walk through an installation of mojoPortal and show how to configure Apache with Virtual Hosts so that we can host multiple websites on a single IP address.

In the previous article, we setup a Samba share on your Suse 10 machine so you can move files between your Windows machine and Suse 10.

This should be a good starting point to enable you to develop your own ASP.NET applications on Windows and deploy and configure them to run on Linux/mono/apache. Another resource you should also review is the mod_mono page on the mono project site.

If you followed the previous articles you should have a Suse 10 machine all set up with Apache installed and Mono including xsp and mod_mono built from source code.

The first thing you should do is open your Firefox web browser and go to http://localhost and you should see the default Apache page.

Setting Up Apache Virtual Hosts

If you see this then Apache is running. The next thing to do is to download mojoPortal. Get the release version for now, I will cover working with the source code from Subversion in a future article.

I would doubt that you want to use the MS SQL version since you are setting up the website on Linux but there is nothing preventing you from doing that as long as the MS SQL database is accessible from the Linux machine. More likely you are interested in using PostgreSQL or MySQL for the database.

Download the release for PostgreSQL and if you wish to use the MySQL database also download the data layer for MySQL from the download page at mojoPortal.com.

To illustrate hosting multiple sites with the same IP address, we will install 2 instances of mojoportal with 2 databases. We will actually set up 3 sites, the 3rd site will demonstrate the ability to host multiple sites with one instance of mojoportal.

So in the final result, we will have 3 sites with different virtual hosts running on a single IP address.

Create 2 folders in your web share folder (see the previous article on setting up a samba share if you don’t yet have your webshare folder setup), in my case they are:

  • /home/frodo/webshare/mojodev
  • and
  • /home/frodo/webshare/mojoprod

Unzip the release files and copy the contents of the mojoportal folder into each of the new folders we created.

Now we need to make the Data folder beneath each writable. Right-click each Data folder under the 2 folders we created and choose Properties, then go to the Permissions tab as shown below and set it to allow writing from all. Be sure and check the box to apply the same permissions to all child folders and files.

Next set up your database following either the instructions for PostgreSQL or MySQL. If using the MySQL version you must also unzip the data layer for MySQL and then replace the mojoPortal.Data.dll in the bin folder beneath the root of the website folder. Also, place the MySQL.Data.dll into the bin folder.

If you followed my first article on Installing Suse 10, you should at least have MySQL installed. Make sure you have started the service. Open Control Center (YaST) and go to System > System Services (Runlevel) to start the MySQL service.

For the purposes of this article, I am using a Virtual Machine for the websites and pointing each site to another machine on my local network for the DB. I am setting up the mojodev folder to use MySQL and I am setting up the mojoprod folder to use PostgreSQL.

To follow all the steps in this article you will want 2 different mojoportal databases though they can both be MySQL or PostgreSQL, you don’t have to have 1 of each, that’s just what I’m doing.

To create a second database just edit the CreateDatabase.sql script and put a different name for the second database and then run the rest of the scripts against it to create tables and data (and stored procedures if using PostgreSQL)

Now edit the connection string in the Web.config file at the root of each site folder according to your database setup.

I’m not going into a lot of detail here on the database setup since the focus of this article is on Apache configuration. If you would like to see an article with more step-by-step details on setting up MySQL or PostgreSQL email me and I may consider writing another article if there is interest.

Now the first thing we need to tackle is name resolution. We are going to set up 3 sites:

  • dev.mojoportal.home
  • prod.mojoportal.home
  • site2.mojoportal.home

These names are arbitrary, you can use whatever you want, but the zip file for this article includes the Apache virtual host files for these 3 names. You can of course edit to your needs but it might be best to stay close to what I’m doing the first time through.

We want to be able to open the browser on the Suse 10 machine and enter these URLs and see our sites that are going to run on this Suse machine. If you have a DNS server running on your local network you could set up these names as aliases pointing to the IP address of your Suse machine.

Since I don’t have a DNS server on my local network, I will show you how to edit the hosts file to make the machine resolve these names to its own IP address. In resolving a hostname, a machine will always check its hosts file first, if there are no entries in the hosts file to resolve the hostname it will try any DNS servers available to it.

If neither can resolve the name then it will not work. Using File Manager – Super User Mode, browse to /etc and right-click the hosts file, and open with a text editor. I have added entries to my hosts file as shown below.

172.16.0.21 corresponds to my suse 10 machine, your address may be different. A very common local address pattern is 192.168.0.x

Note that I have also added an entry to 172.16.0.25 bagend, this is another machine on my local network that I am using for the MySQL and PostgreSQL databases. This allows me to put bagend for the server name in my connection string in my Web.config files.

Note that if you want to be able to browse to these sites from a Windows machine on your local network, you can edit the hosts file the same way on your Windows machine. Typically it is located at c:\Windows\system32\drivers\etc\hosts

Now it’s time to move on to the Apache Virtual Host configuration. Using File Manager – Super User mode, browse to /etc/apache2, and right-click the httpd.conf file and open it in a text editor.
You need to make 2 entries in this file, 1 to load mod_mono and the other to tell it to use hosts names in mapping Virtual Hosts. Here are the 2 entries, see the example httpd.conf file in the zip for this article for where to put these entries. If you scan this file you will see these entries I have added:

# ************** Joe Audette added this *********************
# include mod_mono
Include /etc/apache2/mod_mono.conf
# ***********************************************************
and
# *************** Joe Audette added this ********************************
# very important this is telling apache to use host names to map requests
NameVirtualHost *:80
# ***********************************************************************

These are the only changes needed to the httpd.conf file

The httpd.conf file is setup by default on suse to include Virtual Host configuration files that you place in the /ect/apache2/vhosts.d folder. You can copy the mojodev.conf, mojoprod.conf, and mojoprod2.conf files from the zip for this article into your /etc/apache2/vhosts.d folder and then edit them as needed. The following 3 screenshots show these files.

  • mojodev.conf:
  • mojoprod.conf:
  • mojoprod2.conf:

Note that you will need to edit these in 3 places to point to the correct folders on your system.
DocumentRoot “/home/frodo/webshare/mojoprod”

Obviously won’t be right unless you are also using the user name Frodo and you set things up exactly as I did.

Note that the MonoExecutablePath and MonoServerPath do not have to be included, they have default values, as you can see, I commented them out in mojoprod2.conf and the defaults work just fine. If you installed from a release version of Mono you probably should just comment these entries out.

The use of MonoServerPath is mainly for if you want to force the use of the 2.0 runtime. mojoportal is not currently designed to run in the 2.0 runtime so don’t configure it that way, just realize you can try this in your own experiments with 2.0 ASP.NET.

In my case, everything is beneath /usr because I used that prefix when compiling and installing mono from SVN as in my previous article on building mono from source code.

Now to make our changes take effect we need to restart Apache. Open Control Center (YaST) and go to System > System Services (Runlevel) and disable then enable the apache2 service.

Now open Firefox and navigate to http://dev.mojoportal.home
Assuming you got everything right in your edits of the Virtual Host Files and your connection string is pointing to a reachable database, you should see something like this

The first request after restarting the application is usually very slow, after that it should perform well. This is a feature of the ASP.NET compilation model and is normal on both Windows and Mono.

Next, I opened 2 more tabs in my Firefox browser and navigated to prod.mojoportal.home and site2.mojoportal.home. Right now they are both using the same site and if I were to log in to either one of them and edit content, the content would also change in the other site.

So we have both prod.mojoportal.home and site2.mojoportal.home pointing to the mojoprod folder and therefore using the same database and I want to show you how you can use the features of mojoportal to make them completely independent sites even though they reside in the same folder and use the same DB.

Now one of the sites has to be the master site when configuring mojoportal to host multiple sites in the same instance. Whichever site you use to create the second site becomes the master. I will log in to prod.mojoportal.home and use it as the master.

After I log in I go to Admin > Site Settings as shown below.

Then in the upper right corner of the dropdown list where you see mojoportal, choose New Site, then enter site2.mojoportal.home for the site name, set the skin to GotMono, and click the Save button at the bottom.

Now some new settings appear at the bottom of the screen allowing you to choose which features are included in the new site. Click a feature name then use the arrow button to add it to the new site as shown below.

Most importantly, you have to add the host name for the site to work, enter site2.mojoportal.home, and click Add Host.

Now if we refresh the broswer in the tab that is viewing site2.mojoportal.home we should see that the skin has changed and it now has separate content from prod.mojoportal.home

Finally, log in as admin to this site and you can see that you can add and edit the content of this site and it has no impact on the prod.mojoportal.home site

I hope you were able to follow along with this article and set up 3 sites of mojoportal all running on the same ip address using Apache Virtual Hosts. If you had any trouble please email me and I will try to update this article to make it clearer.

If all went well you should have learned enough along the way to set up your own apps for testing on mono.