Codeigniter: Setting up multiple sites on one install

It’s been awhile since I last posted about Codeigniter. In this post, I’m going to walkthrough how I setup a CI install that can support multiple sites. This is by no means the only way to do it, but it’s what works for me.

CI’s default folder structure is really geared towards one site domain, and having both the application folder and the system folder visible to the browser is not exactly a secure approach.

By configuring your install to support multiple sites, not only do you make the updating of the CI core easier, but also make the CI install more secure as a whole. So without delay, let’s get started…

In this walkthrough, I’m setting up my multiple CI sites on my local XAMPP install, which is on my M:\ drive. Your Apache setup might be different, so do take note of my settings and adjust according to your own web server environment.

I’m planning to setup two sites just for illustration – http://dev.ci/ and http://live.ci/.

Codeigniter default folder structure
When you first unzip the downloaded CI file, you would get something like this:

You would have set your Apache’s configuration to point to your CI folder (which is M:\xampplite\CodeIgniter_1.7.1 for me) as the document root. Once you hit the site in your browser, you would get the standard welcome page.

If you have setup the default CI before, you may know that within the system folder are a bunch of core folders which you don’t normally need to touch and the application folder which will house all your codes for your site. You may also have noticed the index.php file in the root of the CI directory, which you didn’t really need to touch before but will become crucial in setting up multiple sites.

Adjusting the application folders
First thing I do is to move the application folder out of the system folder to the same level as the system folder.

Since I’m setting up to run two distinct sites sharing the same CI core, I will make another copy of the application folder and rename both folders to reflect the different sites.

Create the web root folders
Next, we would need two folders which will be the website’s root folders. This is how I setup mine.

Make copies of the index.php file located in the CI’s root folder (ie M:\xampplite\CodeIgniter_1.7.1) into each of the htdocs folder. In this case, I would have one index.php in each of my htdocs_ci_dev and htdocs_ci_live folders.

Open index.php file for each site and edit the following lines.

At Line 26:
	$system_folder = "system";
Change to 
	$system_folder = "../system";
 
At Line 43:
	$application_folder = "application";
Change to 
	$application_folder = "../app_ci_dev"; (for the htdocs_ci_dev)
or 
	$application_folder = "../app_ci_live"; (for the htdocs_ci_live)

Configure Apache virtual hosts
Once that’s done, the last thing to do is to setup the Apache virtual host configuration of my XAMPP install.

My Apache virtual host configuration looks like this:

Don’t forget to edit your local hosts file so that your system recognizes the dev.ci and live.ci domains.

Once you fire up your Apache web server, you should be able to load either site accordingly (*fingers crossed*)

Renaming the system folder
If you want to go further you can rename the system folder to reflect the different CI versions. This way, when a new version of the CI core is released, you can configure your sites easily by just changing $system_folder parameter in the index.php file (Line 26) for each site.

Conclusion
I think you will agree that with this setup, you can easily expand the CI install to more sites. There is also no doubt that this is more secure than the default CI setup, as the only folders which are exposed to the browser are the htdocs folders – all the application and system folders are no longer accessible through the web server.

Of course, this is not the only way you can configure CI for multiple sites/applications, check out some of these alternatives:
- CodeIgniter Wiki – Multiple Applications
- Michael Wales – CodeIgniter Advent: Day 1
- Multiple codeigniter sites, using symlinks

Please feel free to give your thoughts and feedback. Thanks!

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • DZone
  • Propeller
  • Reddit
  • StumbleUpon
  • Technorati
  • Yahoo! Buzz
Posted on August 19, 2009 at 4:19 pm by Eldee · Permalink
In: PHP Tutorials · Tagged with: , ,

12 Responses

Subscribe to comments via RSS

  1. Written by derschreckliche on August 21, 2009 at 4:08 am
    Permalink

    nice tutorial, thx for that!

    do you know if this way will work in kohana, too?

  2. Written by webmaster on August 21, 2009 at 8:02 am
    Permalink

    Sorry, I’ve not tried Kohana yet as I’m having too much fun with CI :)

    Perhap someone else can provide a comment about Kohana?

  3. Written by Tochie on August 26, 2009 at 4:30 am
    Permalink

    I have been looking for a solution to this problem as most sites I develop have admin backend which I would love to run on the same system folder.

    I think I prefer your method better but how do i set it up using WAMP server? and how is it applied to a live hosting?

    Thanks

  4. Written by webmaster on August 27, 2009 at 8:09 am
    Permalink

    Hi Tochie
    Regarding WAMP server, although I’m not familiar with it, I would imaging you should be able to do something similar. The main point behind this setup is to be able to modify your apache conf DocumentRoot setting and setting up different virtual hosts. I’m sure WAMP is able to do that. The rest is just folder re-organisation.

    Regardig a Live Hosting environment, I think you would not be able to setup CI like this on most basic hosting environment. The best method for basic hosting setup would be the Method #2 detailed in the Codeigniter Wiki…

    http://codeigniter.com/wiki/Multiple_Applications/

    and use a .htaccess to do some URL rewriting.

  5. Written by Moefelt on September 9, 2009 at 4:52 pm
    Permalink

    Great tut! Works like a charm.

  6. Written by bootcat on September 27, 2009 at 2:44 pm
    Permalink

    hello again friend .
    sorry if this comment is not related .
    but i wanted to ask if you are on facebook .
    i want to add u as ma friend .

    on facebook , pls add my mail as bootcat [at] gmail [dot] com . Or you can give me ur mail address and i will add them ya.

    Else just your email .
    btw your site is really cool and great . keep the good work going .

    Thanks
    Banny BooTCaT

  7. [...] 25. Codeigniter: Setting up multiple sites on one install [...]

  8. Written by Edhi Prabawa on October 22, 2009 at 7:17 pm
    Permalink

    It’s so good. But, can it run in Linux Mint 7 Gloria?

  9. [...] 25. Codeigniter: Setting up multiple sites on one install [...]

  10. Written by Nick Yeoman on November 21, 2009 at 6:53 am
    Permalink

    I don’t get why you would want this.

    Okay you use CI for more than one site. Great!

    Lets say you use it for 100 sites. A new CI is released. okay lets run an update. Oops it broke 50 of our sites. Now you have to revert.

    okay but now 5 of our sites need a feature that only the new CI will do. Can’t fix this until the other 50 are fixed.

    See the problem? (What if the 5 sites bring you 30% of your business)

    My solution:
    Write a bash script that will loop through your domains and do an svn up on your CI directory.

    Certainly no where near the time it would take to have to fix 50 sites. And once you have the bash script done it will take less than a minute to add all your joomla, wordpress, kohana, drupal, magento, insert_other_openSource_app_here.

    Thanks for your time. I’m a freelance web developer, you can read more of my daily experiences at NickYeoman.com

  11. Written by ben on February 19, 2010 at 8:10 pm
    Permalink

    hi, i have similar ci setup as yours,
    mine is
    /htdocs/
    /ci_projects/
    /ci_1.7.1/ * system folder
    /ci_1.7.2/ * system folder
    /website1/ * website 1
    /app/ * application folder, private
    /pubic/ * public folder, css, js …
    /css/
    /js/
    /images/
    index.php
    /website2/
    /app/
    /pubic/
    /css/
    /js/
    /images/
    index.php

    and set virtual host website1 documentroot to
    c:/xampp/htdocs/ci_projects/website1/public

    my problem is how i can setup cache path in the config file? can you give me a hint please :)

    @Nick Yeoman
    i dont think there would be problems like you mentioned.
    as we seperate and rename the system folder to different versions. we can set config in the index.php file.
    in this way we can have different sites using different ci versions system. no problems at all :)

  12. Written by ben on February 20, 2010 at 1:03 pm
    Permalink

    sorry about the format
    see if this make it clear

    /htdocs/
        /ci_projects/
            /ci_1.7.1/ * system folder
            /ci_1.7.2/ * system folder
                /website1/ * website 1
                    /app/ * application folder, private
                    /pubic/ * public folder, css, js …
                        /css/
                        /js/
                        /images/
                        index.php
            /website2/
                /app/
                    /pubic/
                        /css/
                        /js/
                        /images/
                        index.php

Subscribe to comments via RSS

Leave a Reply