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 –
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
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
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*)
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.
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
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!
In: PHP Tutorials · Tagged with: Apache, codeigniter, website