Step 2: Set up the environments

Created at: November 7, 2014; Last update: January 23, 2015

(created at: November 7, 2014; last update: January 22, 2015)

Environments are the mediums in which you work. Usually, when working on a site, you have a local version and a production server – those are the environments. And, because they are different environments, they might have different configurations (database, config etc.). Those configurations can be set differently, depending on the environments that the application is in (read here: http://www.codeigniter.com/userguide3/general/environments.html). Taking this into consideration, Codeigniter will define a constant named ENVIRONMENT before initializing and give it the value you want. You can either set the value yourself by defining the $_SERVER[‘CI_ENV’] before the definition of ENVIRONMENT takes place, or you can let the server set it, by creating a .htaccess file and writing the following lines:

(ofcourse, you must change www.yoursite.tld, test.yoursite.tld and localhost with your own servers). If you don’t have an Apache server you can change the line in public_html/index.php:

with:

NOTE: Even if you don’t feel you need different environments, it is a good practice to make that separation. You can even let yoursite.tld as top level domains and work only on localhost. When you will transfer the site you just replace yoursite.tld with the domain you have.

Once you’ve set up the environments, go into your application/config folder and create three new folders: production, testing and development. In those folders we will keep all the configurations for those specific environments. For starters, we should move or copy config.php, database.php, and routes.php from the config folder and paste them to the development folder. Your site’s structure should look like this:

From now on, until we’ve finished our application we will only work with the development configuration. Once you’ve finished your development, you can simply copy/paste your files from the development folder to the testing or production directory, and make the changes to the files you’ve pasted according to the settings that are particular to those environments.

11 thoughts on “Step 2: Set up the environments

  1. Flemming

    I have been using CI for years and never knew that you could have different config files for different environments! Thanks for posting this!

    Keep up your good work – it’s great that there is renewed enthusiasm for CI at long last 🙂

    Reply
  2. Dijup

    I made the changes and I get “Message: include(/Users/dijup/Sites/ci/application/config/autoload.php): failed to open stream: No such file or directory” I added the htaccess file.

    Reply
    1. avenirer Post author

      Well… that means that the file is not there. When I said “ci/application”, I was actually referring to the application folder of your site, wherever that application folder is in your case.

      Reply
  3. Federico

    Just wondering…how CI knows that has to read config/autoload/router from the correct folder instead the main config/autoload/router file into application folder? I just copied the config/autoload/router file form application folder into the development/testing/production directories.
    Thanks

    Reply
    1. avenirer Post author

      The framework will first look for the environment variable (if it’s “development”, “testing” or “production”). Depending on that, it will first look for a directory named either “development”, “testing” or “production” (or whatever you named your environment) inside the config directory. If it finds the directory it will look inside that directory for the configuration files. If it doesn’t it will look for those file inside the config directory.

      Reply
  4. Jacky

    Just for those that is using XAMP and WAMP (or similar). The built-in Apache have rewrite_module turned off by default.

    The above suggestions will leads to a HTTP 500. If that’s the case, just enable rewrite_module by in {wamp_dir}/apache/conf/httpd.conf

    Hope it helps.

    Reply
  5. Pingback: Setting up different environments in CodeIgniter | Cactus's Web

  6. solidcodes

    Very nice tutorial dude, It makes the CodeIgniter Community alive again.
    Please continue making tutorial dude…
    Thank you for this.

    Reply
  7. Ambroz

    Well I ttink that you should mension why you did the rewrite engine with the production, testing and developement.
    If i’m thinking correct, that the production with the normal site is the one that we will use to get to our website when we bring her to live in the web. The test is the wabsite for testers, and the developement is for us when we create the site.
    Am i right?

    P.S sorry if my english isn’t to good.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

No spam? * Time limit is exhausted. Please reload CAPTCHA.