Currently Reading

December 7, 2011

Multiple Applications, One Domain

I am working on a project with a friend of mine that will be, in the beginning, hosted in the cloud. Since our CPU/memory footprint will be very valuable to us we decided to go with Lighttpd as our web server. Apache is a great and reliable web server, just look at Netcraft’s November 2011 Web Server Statistics to see how many sites are using Apache as their web server. The one downside of Apache, is that it can get a little greedy when it comes to CPU and memory. So in an effort to save precious bytes we went with Lighttpd which has a lower CPU/memory usage footprint.

Part of the site will be static content, the other part will be user generated content. The user generated content is being powered by a custom MVC framework and the static content is powered by WordPress. This gives us the ability to update static content, blog posts, and general information in WordPress’s interface without having to jump into the MVC code to make content updates. We wanted the user experience to be seamless. So if a user visited a static page or a generated page the site would not look different, thus not ‘breaking’ their experience.

We also wanted to keep the code together as much as possible. So we structured the code base into separate directories under one project name: coolnewapp. The WordPress code lives in the site directory while the MVC code lives in the app directory. The static resources, like CSS, JavaScript, and images, live in the assets directory. Here is what the directory structure looks like:

1
2
3
4
coolnewapp/
    app/
    assets/
    site/

The challenge is how do we get Lighttpd to serve up pages from the WordPress code when a static page is visited and how do I get it to serve up pages from the MVC code when generated content pages are visited. So I set out on my quest to find a solution.

I searched the highest mountains and lowest valleys of Google but could only find a couple sites that even remotely talked about what I was trying to accomplish. I found a blog post entitled, “How to setup multiple apps on Ruby on Rails and Lighttpd“. This post gave me the insight I needed to accomplish multiple applications under one domain.

I was able to get Lighttpd to serve different document roots depending on the URL. Here is the solution I developed, with the help of the article:

1
2
3
4
5
6
7
8
$HTTP["host"] == "coolnewapp.com" {
    server.document-root = "/path/to/wordpress/dir/"
 
    $HTTP["url"] =~ "^/mvc-url" {
        server.document-root = "/path/to/mvc/app/dir/"
        alias.url = ("/mvc-url" => "/path/to/mvc/app/dir/" )
    }
}

The key to this succeeding was alias.url. This basically tells Lighttpd that if the current request is for any URLs that start with /mvc-url use the provided path as the document root instead of the default path.

So now any URL that starts with /mvc-url will use the MVC application code instead of the WordPress code. If a user requests a URL that does not start with not /mvc-url the WordPress code will be used instead.

One issue with this solution, although not a show stopper, is that it tightly couples the server configuration to the whole site. This means that the site is less portable when it comes to switching to a new server or if the site needs to be hosted on multiple application servers. A solution for this issue is that the config file will always be stored in version control and a deploy script will be used that will include the config file as part of the deployment process. As time goes on with this project, the whole process will be refined.