Step 2: Set up the environments

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

RewriteEngine On
SetEnvIf Host www.yoursite.tld$ CI_ENV=production
SetEnvIf Host test.yoursite.tld$ CI_ENV=testing
SetEnvIf Host localhost$ CI_ENV=development

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

define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');


if(! defined('ENVIRONMENT') )
  $domain = strtolower($_SERVER['HTTP_HOST']);
  switch($domain) {
    case 'www.yoursite.tld' :
      define('ENVIRONMENT', 'production');
    case 'test.yoursite.tld' :
      define('ENVIRONMENT', 'testing');
    default :
      define('ENVIRONMENT', 'development');

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:

- -development
- - -config.php
- - -database.php
- - -routes.php
- -production
- -testing

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.


  1. 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 🙂

  2. 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.

    1. 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.

  3. 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.

    1. 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.

  4. 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.

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

  6. 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.

  7. Thanks Avenir. This is really helpful (Actually better than CI3 manual in my opinion).
    Can I post translated articles of yours on my blog? I will write the source(your blog).

  8. You have helped me alot over the years Adrian, many thanks! May I suggest that the folder development is .gitignored when working in team.

Leave a Reply

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

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