ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Configure CodeIgniter to run on two servers

June 03, 2009 9:58am

Subscribe [8]
  • #1 / Jun 03, 2009 9:58am

    neillyons.co

    10 posts

    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 ( http://bazaar-vcs.org/BazaarUploadForWebDev ).

    With this feature, the same CodeIgniter installation can run on two servers without the need to change 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 variable 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.

  • #2 / Jun 04, 2009 5:32pm

    bmchild

    2 posts

    Looks like a good idea to switch between dev and production versions. I like it.

    I’ve also done something similar to choose between application folders depending on which host is being called, although I don’t think I would do that for a site that is very complicated.

  • #3 / Jun 05, 2009 2:31am

    Fatih's avatar

    Fatih

    59 posts

    Good feature. Maybe you can use below codes in your database.php file for clear coding.

    if (SITE_LIVE)
    {
        //Live site database settings.
        $active_group = "default";
    }
    else 
    {
        $active_group = "other_server";
    }
    
    
    
        $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";
    
        $db['other_server']['hostname'] = "localhost";
        $db['other_server']['username'] = "";
        $db['other_server']['password'] = "";
        $db['other_server']['database'] = "";
        $db['other_server']['dbdriver'] = "mysql";
        $db['other_server']['dbprefix'] = "";
        $db['other_server']['pconnect'] = TRUE;
        $db['other_server']['db_debug'] = TRUE;
        $db['other_server']['cache_on'] = FALSE;
        $db['other_server']['cachedir'] = "";
        $db['other_server']['char_set'] = "utf8";
        $db['other_server']['dbcollat'] = "utf8_general_ci";

    Also, if you can use cookies to get user information, your visitors shall be login in every changed servers.

  • #4 / Jun 05, 2009 4:16am

    eoinmcg's avatar

    eoinmcg

    311 posts

    I do something very similar, switching between my local machine and production server.

    In config/config.php

    $config['base_url']    = "http://".$_SERVER['HTTP_HOST']."/";

    Then in config/database.php

    if ($_SERVER["SERVER_ADDR"] != '127.0.0.1')
    {
        // local db config here
    }
    else
    {
        // live db config here
    }
  • #5 / Jun 05, 2009 4:39am

    neillyons.co

    10 posts

    @fatigue. I was looking through DB.php ( ../system/database/DB.php ) and noticed that CodeIgniter does already allow multiple database settings.

    $params = $db[$active_group];

    To set which settings just define $active_group like you said.

    Kudos fatigue, I didn’t pay any attention to the $active_group variable before. I think using CodeIgniter’s ( and your ) implementation is best.

  • #6 / Jun 05, 2009 4:51am

    eoinmcg's avatar

    eoinmcg

    311 posts

    Must admit that, I too, hadn’t paid attention to using

    $active_group

    for switching databases.

    Nice tip, cheers!  😊

  • #7 / Jun 05, 2009 8:51am

    Phil Sturgeon's avatar

    Phil Sturgeon

    2889 posts

    Here’s my views on this one “How to Support multiple production environments in CodeIgniter”.

    Using that approach cleans up your config files from storing domains in multiple locations and allows you to have a database file such as:

    // Local
    $db['local']['hostname'] = "localhost";
    $db['local']['username'] = "root";
    $db['local']['password'] = "";
    $db['local']['database'] = "";
    $db['local']['dbdriver'] = "mysql";
    $db['local']['dbprefix'] = "";
    $db['local']['active_r'] = TRUE;
    $db['local']['pconnect'] = TRUE;
    $db['local']['db_debug'] = TRUE;
    $db['local']['cache_on'] = FALSE;
    $db['local']['cachedir'] = "";
    $db['local']['char_set'] = "utf8";
    $db['local']['dbcollat'] = "utf8_unicode_ci";
     
    // Dev
    //$db['dev']['hostname'] = "localhost";
    // ...etc
     
    // QA
    //$db['qa']['hostname'] = "localhost";
    // ...etc
     
    // Live
    $db['live']['hostname'] = "localhost";
    $db['live']['username'] = "root";
    $db['live']['password'] = "";
    $db['live']['database'] = "";
    $db['live']['dbdriver'] = "mysql";
    $db['live']['dbprefix'] = "";
    $db['live']['active_r'] = TRUE;
    $db['live']['pconnect'] = TRUE;
    $db['live']['db_debug'] = TRUE;
    $db['live']['cache_on'] = FALSE;
    $db['live']['cachedir'] = "";
    $db['live']['char_set'] = "utf8";
    $db['live']['dbcollat'] = "utf8_unicode_ci";
     
    // Check the configuration group in use exists
    if(!array_key_exists(ENV, $db))
    {
      show_error(sprintf(lang('error_invalid_db_group'), ENV));
    }
     
    // Assign the group to be used
    $active_group = ENV;
     
    ?>
  • #8 / Jul 01, 2009 3:05pm

    neillyons.co

    10 posts

    After looking through the CodeIgniter.php file ( ../system/codeigniter/CodeIgniter.php ) I noticed that constants are loaded on line 52.

    require(APPPATH.'config/constants'.EXT);

    I recommend removing the constant SITE_LIVE I defined in the index.php to the constants.php file ( ../system/application/config/constants.php ). To make upgrading the core easier.

  • #9 / Jul 01, 2009 3:46pm

    TWP Marketing

    596 posts

    Between my dev and live servers the only change I need to make is in (under WIN XP) the hosts file.  I uncomment (dev) or comment (live) the declaration of my site url in hosts. For example:

    in hosts file
    ...
    #127.0.0.1 <a href="http://www.mysite.com">http://www.mysite.com</a>

    The # is the comment delimiter.  I realize this is windows centric but it is far simpler than the code mods discussed above.  I regret that I don’t know if something similar is available under other OS’s, but if you run under win, it works well and is quick. No guarantee that this works if you are using Vista…

    Caveate: it requires that your local httpd.conf file contain virtual host definitions matching the live site installation. Use the same ServerName as in the hosts file, but this is simple to arrange. For example:

    in httpd.conf file
    ...
    <VirtualHost *>
    ServerName <a href="http://www.mysite.com">http://www.mysite.com</a>
    DocumentRoot /www/docs/www.mysite.com
    </VirtualHosts

    The DocumentRoot param points to where your code resides on both the dev and live sites.
    IT DOES NOT NEED TO BE THE SAME AS SERVERNAME.

    If you can’t control where the live site is installed, change your dev configuration to match the live site.  This is a one-time-only change at the time of project setup.

  • #10 / Jul 01, 2009 3:58pm

    neillyons.co

    10 posts

    I guess my solution is most useful if you use http://bazaar-vcs.org/BazaarUploadForWebDev which only uploads the files that have changed on your development site to the live site. This solution allows no changes to be made to the code after it’s been initially configured.

  • #11 / Jul 01, 2009 4:27pm

    TWP Marketing

    596 posts

    re: “This solution allows no changes to be made to the code after it’s been initially configured.”

    I’m not sure what this means.  Which solution does not allow code changes after initial install?  Please clarify what you mean here.

  • #12 / Jul 01, 2009 5:19pm

    Phil Sturgeon's avatar

    Phil Sturgeon

    2889 posts

  • #13 / Jul 25, 2009 10:03pm

    Ch!ps

    8 posts

    Interesting…no one seems to worry about error_reporting…we don’t want to display errors on live site…and to make application portable, we should address it as well…

    i think the environment constant should be set-up in the index.php file and error reporting adjusted accordingly…eg.

    define('ENV', strpos($_SERVER['SERVER_NAME'], 'local') !== FALSE ? 'local' : 'live');
    
    error_reporting( (ENV != 'live') ? E_ALL : 0 );

    The ENV constant can be used elsewhere in the system as it will be available throughout…though… putting this clause in index.php is not exactly ideal but i don’t think there is a better alternative…

    what do you think…would love to hear your thoughts…

  • #14 / Jul 26, 2009 9:44am

    neillyons.co

    10 posts

    Good idea Ch!ps,

    I have found that CodeIgniter has the perfect place to define constants. Have a look at this comment.

    http://ellislab.com/forums/viewthread/119072/#603470

  • #15 / Jul 26, 2009 2:43pm

    Ch!ps

    8 posts

    @ neil

    i did read your write-up on the constants.php file and i normally define constants in that file only…the problem is error_reporting() is called in the index.php and re-calling error_reporting() function later (either in constants file or in config file) doesn’t really work, at least not when i tried…it still retains the settings from the index.php file…eg….i would set-up a config item ‘environment’ in config.php and try to re-call error_reporting() again based on ‘environment’ config-item (E_ALL or 0) but the default settings from index.php would persist…

    one can keep separate index.php files for dev and live sites but i just don’t like the idea of maintaining two separate files…more often than not, i would forget to make the change or overwrite the file…not good 😉

    also, i think making changes/additions to the index.php file is fair game as it resides outside the ‘system’ folder…if one wants to take out the ‘application’ folder from the ‘system’ folder or rename application and/or system folder, you have to make changes to the index.php anyway…guess i am fine as long as the system folder is not hacked

    btw…nice write-up about db-driven routes on your blog 😊

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases