Software Development

Nginx and PHP environment directive

With the Apache web server, I set a server wide directive of which environment the server is in, eg. production, test or dev, using the SetEnv directive. In a PHP script I can pick up this value, and set on all debugging in dev, or suppression in production. Having it set 1 place in Apache means I don’t need to hard code the environment in any PHP code, making movement of code between environments cleaner.
Recently I have begun to use Nginx as the web server with PHP fastcgi for a project, and was looking for a similar set up. The answer I have come up with is an additional entry in my fastcgi_params file. The exact name & location of this file may be different for you. Mine is located at /etc/nginx/fastcgi_params.

fastcgi_param  SYS_ENV             dev;

This way in my bootstrap script I can have the following code:

if ($_SERVER['SYS_ENV'] === 'production') {
    date_default_timezone_set('America/Los_Angeles');    
} else { // Test and dev
    error_reporting(E_ALL|E_STRICT);
    date_default_timezone_set('Australia/Sydney');
    ini_set('display_errors','On');
}

There is probably another way to do this with Nginx, and I would be happy to hear it, as I try and improve my Nginx knowledge.

Standard