Handling Multiple Environments

Developers often desire different system behavior depending on whetheran application is running in a development or production environment.For example, verbose error output is something that would be usefulwhile developing an application, but it may also pose a security issuewhen “live”. In development environments, you might want additionaltools loaded that you don’t in production environments, etc.

The ENVIRONMENT Constant

By default, CodeIgniter comes with the environment constant set to usethe value provided in $_SERVER['CI_ENVIRONMENT'], otherwise defaulting to‘production’. This can be set in several ways depending on your server setup.

.env

The simplest method to set the variable is in your .env file.

  1. CI_ENVIRONMENT = development

Apache

This server variable can be set in your .htaccess file or Apacheconfig using SetEnv.

  1. SetEnv CI_ENVIRONMENT development

nginx

Under nginx, you must pass the environment variable through the fastcgiparamsin order for it to show up under the $SERVER variable. This allows it to work on thevirtual-host level, instead of using env to set it for the entire server, though thatwould work fine on a dedicated server. You would then modify your server config to somethinglike:

  1. server {
  2. server_name localhost;
  3. include conf/defaults.conf;
  4. root /var/www;
  5.  
  6. location ~* \.php$ {
  7. fastcgi_param CI_ENVIRONMENT "production";
  8. include conf/fastcgi-php.conf;
  9. }
  10. }

Alternative methods are available for nginx and other servers, or you canremove this logic entirely and set the constant based on the server’s IP address(for instance).

In addition to affecting some basic framework behavior (see the nextsection), you may use this constant in your own development todifferentiate between which environment you are running in.

Boot Files

CodeIgniter requires that a PHP script matching the environment’s name is locatedunder APPPATH/Config/Boot. These files can contain any customizations thatyou would like to make for your environment, whether it’s updating the error displaysettings, loading additional developer tools, or anything else. These areautomatically loaded by the system. The following files are already created ina fresh install:

  • development.php
  • production.php
  • testing.php

Effects On Default Framework Behavior

There are some places in the CodeIgniter system where the ENVIRONMENTconstant is used. This section describes how default framework behavioris affected.

Error Reporting

Setting the ENVIRONMENT constant to a value of ‘development’ will causeall PHP errors to be rendered to the browser when they occur.Conversely, setting the constant to ‘production’ will disable all erroroutput. Disabling error reporting in production is agood security practice.

Configuration Files

Optionally, you can have CodeIgniter load environment-specificconfiguration files. This may be useful for managing things likediffering API keys across multiple environments. This is described inmore detail in the Handling Different Environments section of theWorking with Configuration Files documentation.