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: , ,

22 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

  13. Written by KPR on August 17, 2010 at 5:02 am
    Permalink

    Nick, who would do that?! As this tutorial suggests, you save different version in different folders. Then you would have to upgrade each site individually.

    If it breaks a development site, you don’t update to the new version, unless there are features that are wanted/needed or there is a security issue.

    Ideally, if you’re running 100+ sites (10+ or even 5+), you’re not developing 100 different individual sites, but are extending the framework’s classes (MY_) as needed and developing reusable modules for the same framework (and version of that framework) that can work across all sites, and can be instantiated on an as-needed basis. Also, if you’re running 100+ sites, this is a best practices method for maintaining your server.

    You might also consider setting up a subdomain if you are using the same CSS and Javascript frameworks on multiple sights. Something like resources.myserver.com allows you to separate frameworks (Blueprint, jQuery, etc.) and helps your site load faster because browsers limit the calls to a specific server, but can syncronously load from multiple servers (just don’t overdo it). Also, for libraries like Moo Tools or jQuery, you can use Google’s API server, which is fast and has request methods to allows serve up the latest version (2.x) by default or a specific version (2.3.1) of a library.

    You can also try doing this for API libraries like Vimeo, Twitter, cloudfusion, etc.

    Also, yes, you can do this with Kohana (I do), and Cake; you just need to reference the correct version of the framework you are using for each project.

  14. Written by Vikki on September 30, 2010 at 7:39 pm
    Permalink

    Just what I needed. Thanks a bunch!

  15. Written by Steve on October 22, 2010 at 11:55 pm
    Permalink

    Hi there,

    I recently wrote a blog post with a full tutorial on how to set this up properly in a windows environment. Help it helps!

    http://www.kintek.com.au/web-design-blog/configuring-multiple-domains-within-wamp-for-local-development/

    - Steve

  16. Written by Vikki on October 25, 2010 at 3:56 am
    Permalink

    Hi,

    I have setup my CI folders in my local machine the same way, but only the base url works(like http://example1.localhost for the default controller welcome.php gets called). But when I access urls with any controller segment,it doesn’t work, throws a 404 error. Even http://example1.localhost/welcome doesn’t work. I have entered the host names in the hosts file too.
    (127.0.0.1 mybits.localhost
    127.0.0.1 linestolinger.localhost)

    My setup is as follows;

    F:\dev\CodeIgniter_1.7.2
    \.metadata
    \app1
    \app2
    \htdocs_app1
    \htdocs_app2
    \system
    \system_ci_1.7.2
    \user_guide
    index.php
    license.txt

    Have specified the system and application dir paths in the index.php files in htdocs too;
    For the index.php in \htdocs_app1 ;
    $system_folder = “../system_ci_1.7.2″;
    $application_folder = “../app1″;

    For the index.php in \htdocs_app2 ;
    $system_folder = “../system_ci_1.7.2″;
    $application_folder = “../app2″;

    The virtual hosts setup is as follows;

    ServerAdmin webmaster@localhost
    DocumentRoot “F:/dev/CodeIgniter_1.7.2/htdocs_app1″
    ServerName mybits.localhost
    DirectoryIndex index.php index.html index.htm index.shtml

    Options Indexes
    AllowOverride All
    Order allow,deny
    Allow from all

    ServerAdmin webmaster@localhost
    DocumentRoot “F:/dev/CodeIgniter_1.7.2/htdocs_app2″
    ServerName linestolinger.localhost
    DirectoryIndex index.php index.html index.htm index.shtml

    Options Indexes
    AllowOverride All
    Order allow,deny
    Allow from all

    Any idea what could be wrong?.

  17. Written by Vikki on October 25, 2010 at 4:00 am
    Permalink

    I do have the virtualhost, directory tags in the virtualhosts setup. Its just that the tag signs doesn’t show in the post here.

  18. Written by Peter on December 21, 2010 at 4:53 am
    Permalink

    Great tutorial. Works really well.

  19. Written by Abolade Baba on March 20, 2011 at 12:55 am
    Permalink

    Hi there,

    Great work I presumed but it isn’t working for me yet!

    I am trying to configure CI for a site for now then I can add other sites later if it works for one. I followed your instructions here carefully and they were simple enough for me until I got to where I need to edit the my system’s host file to recognise dev.ci .

    I tried editing the “host” file, it seems to accept two parameters – IP address and the ServerName. I cld understand dev.ci:80 as my ServerName but I didn’t is what IP address will I have to put in the host file before writing the server name?

    On saving and restarting the apache server, when I type local host into my address bar, I takes me straight up to the codeigniter welcome page. I couldn’t debug this on my own. Could you please help? Thanks

  20. [...] Series andrewroland zend lucene librarycodeigniter 2.0 workshoplearncodeigniter basics Sponsors Top Rated TutorialsUsing Elliot Haughin's Twitter API with Codeigniter Part 2 (2 votes)CodeIgniter Active Record class and INSERT IGNORE (2 votes)Codeigniter Project Development from scratch to app Part 6 (2 votes)Tags2.0 active record ajax amplio.ch andrewroland.com api askaboutphp beginner cart class codesamplez controller country darrenonthe.net db dragffy.com drop down error flashdata form from scratch helper html install jquery login lucene multiple MVC net.tutsplus profiler reactor registration search session simplycodeigniter string twitter upload URL validation view views workshop zend Sponsors LinksCodeIgniter: Extending the native ‘Model’ and make it your own. [...]

  21. Written by junkiest on November 16, 2011 at 7:57 pm
    Permalink

    Nice tutorial…. thanks !!!

  22. Written by Awesome tutorials to master CodeIgniter | CatsWhoCode.com on September 30, 2013 at 11:21 pm
    Permalink

    [...] Let’s finish this post with a very handy tutorial, which will show you how to set up multiple sites using a single CI install. A must read! » Read tutorial [...]

Subscribe to comments via RSS

Leave a Reply