Configure CodeIgniter to run on two servers
Using a dynamicly defined constant in the index.php file, CodeIgniter can be set up to run on two servers.
I have found this really speeds up my development time, especially if used along with Bazaar Upload for Web. With this feature, the same CodeIgniter installation can run on two servers without the need to change configuration variables.
Step 1
To implement this feature into CodeIgniter, edit the index.php file, and include the following code at the top.
if ($_SERVER['SERVER_NAME'] == 'localhost')
{
//Development enviroment.
define('SITE_LIVE', FALSE);
}
else
{
//Live enviroment.
define('SITE_LIVE', TRUE);
}
The code determines if CodeIgniter is running on a localhost, and then sets the SITE_LIVE constant to FALSE, or to TRUE if it’s not.
Step 2
Alter the "config.php" file ( ../application/config/config.php ), by replacing the original "base_url" variable, with the code below.
if (SITE_LIVE)
{
//Live base URL
$config['base_url'] = "http://neillyons.info/";
}
else
{
//Development base URL
$config['base_url'] = "http://localhost/";
}
The if statement sets the "base_url" variable depending on whether the site is Live or in Development.
Step 3
Alter the "database.php" file ( ../application/config/database.php ), by replacing the original variables with the code below.
if (SITE_LIVE)
{
//Live site database settings.
$active_group = "default";
$active_record = TRUE;
$db['default']['hostname'] = "localhost";
$db['default']['username'] = "";
$db['default']['password'] = "";
$db['default']['database'] = "";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";
}
else
{
//Development site database settings.
$active_group = "default";
$active_record = TRUE;
$db['default']['hostname'] = "localhost";
$db['default']['username'] = "";
$db['default']['password'] = "";
$db['default']['database'] = "";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";
}
Let me know what you think of this feature in the comments.
Comments
-
@Ding-Wen thanks for your feedback.
I know what you mean. Deploying to production servers can be a bit of a pain, especially when it comes to configuration settings. If you use version control software I would recommend using Bazaar Upload for Web or Git-ftp aswell as this method. It makes things so much easier.
Got a few articles written in draft, so I'll try and polish them up and post them this week :)
This is setting is amazing. I can't say how many troubles I have ran into because of "localhost" settings on production server. I also read your database driven routing which opened my eyes to another world. Thank you for the great articles. I just start learning how to use Codeigniter. To know how to do more advanced PHP programming is very very exciting. Hopefully, we can see you post more.