Multisite CodeIgniter with sessions preserved across subdomains using virtual hosts


As I’m creating the mobile version of deskhot as well as some new accompanying apps I came across the need to have a single multisite codeigniter base where I can do all the development instead of multiple installations.

What I needed and what this post covers:

  • Multiple web applications running from a single codeigniter instance.
  • Each application must have it’s own subdomain. For example deskhot.com, m.deskhot.com, pilot.deskhot.com and so on.
  • When a user logs in, in one application, he must be logged in, in all applications. So all applications must share the same session data.
  • Only one codeigniter system folder so upgrades can done once for all applications/subdomains.
    But also having the flexibility to have a certain application using an older system folder if I see fit in the future.

ipad iphone mac devices

So let’s get into action

1. Add the subdomains in your hosts file

All operating systems have a hosts file which helps map ip addresses to domains. Of course you should add all the subdomains in your hosts file. Here is the location of the hosts file for each operating system.

This is how my hosts file looks like

127.0.0.1        deskhot.d
127.0.0.1        m.deskhot.d
127.0.0.1        pilot.deskhot.d

 

2. Add a server alias in the apache config

The next step is configuring your virtual hosts so you can have apache know about your subdomains. In my system the vhosts file is located in /apache/conf/extra/httpd-vhosts.conf. This will be different in your system so just do a search and you’ll find it. There will be some default configuration for localhost already there.

Add the following in the end of the file:

<VirtualHost *:80>
    DocumentRoot "/path-to-your-document-root/your-domain.tld"
    ServerName domain.tld
    ServerAlias *.domain.tld
</VirtualHost>

For example my vhosts file looks like this (in a windows system)

<VirtualHost *:80>
    DocumentRoot "/Users/Username/www/DemianLabs/Deskhot.com"
    ServerName deskhot.d
    ServerAlias *.deskhot.d
</VirtualHost>

As you have guesses I access my development version through http://deskhot.d. You need to change the path and domain in the above lines to suit your own configuration.

 

3. Create your applications

Grab a fresh CodeIgniter and drop it in the folder where you would normaly do your single site development. Then make a copy of application folder for each of your subdomains. A tidy way to name all those application folders is to follow the patern application_subdomain. So for example my folders look like this:

/application                  // for the main site: deskhot.d
/application_m                // for m.deskhot.d
/application_pilot            // for pilot.deskhot.d
/system                       // the system folder
/user_guide                   // the user guide
/index.php
/license.txt

Once again change deskhot to your desired domain.

Next we need to tell CodeIgniter which application folder will be active depending on the url. To do that, open the index.php file and find the line containing $application_folder = ‘application'; and replace it with the following code:

switch ($_SERVER['HTTP_HOST']){
        case 'm.deskhot.d':
            $application_folder = 'application_m';
        break;

        case 'pilot.deskhot.d':
            $application_folder = 'application_pilot';
        break;

        default :
            $application_folder = 'application';
}

Once again change deskhot to your desired domain.

 

4. Configure all applications to share the same cookie domain

In each application folder find the file config/config.php and find the line $config[‘cookie_domain’]    = “”;. It’s around line 260.

Replace it with something like this depending on your domain:

$config['cookie_domain']    = ".deskhot.d";

Notice that this must be the same line in all config/config.php files in all application folders. This will ensure that all applications will read the same cookie and therefore use the same session data.

 

5. Specify a different system folder for each application (optional)

We can do the same trick we did in step 3 for the system folder as well. So for example we can have the following folders:

/system             // The latest CodeIgniter
/system_201         // Version 2.0.1
/system_202         // Version 2.0.2
.... and so on

To do that open index.php again and delete the line (around 65) $system_path = ‘system';

Then change the code we used in step 3 to look like this:

switch ($_SERVER['HTTP_HOST']){
        case 'm.deskhot.d':
            $application_folder = 'application_m';
            $system_path = 'system';
        break;

        case 'pilot.deskhot.d':
            $application_folder = 'application_pilot';
            $system_path = 'system_201';
        break;

        default :
            $application_folder = 'application';
            $system_path = 'system';
 }

In this example all applications use the system folder except pilot, which is using system_201. This way you can upgrade CodeIgniter to a new version only for a specific application and leave the other ones using an older version. This can be handy when you want to do your post-upgrade testing in stages.

 

If you have any comments or questions I’d be glad to hear them on twitter or google+.
In a few days I’ll be writting a new post about how you can make all the applications share some common resources like helpers.