Multi-environment configs and running sites on different ports In my studio, we have successfully used multi-environment config files to allow several machines on our local network to use local MAMP installs as well as designate dev and production environments at our web host.

Locally we run sites on separate ports, so for instance to look at my site I point my browser to http://10.0.1.2:8081/. To look at what’s going on on each other’s machines in the studio, we just change the ip address. 

But now we are looking to run both a stable “trunk” version and a “branch” version for developing new features of our site on each local machine. And I can’t see how to make that work in the general.php and db.php files.

The following is what I’ve had in general.php for running a single instance of the site on my server:

'10.0.1.2' =&gt; array(
    'userSessionDuration' =&gt; false,

    'environmentVariables' =&gt; array(
        'siteUrl'        =&gt; 'http://10.0.1.2:8081/',
        'fileSystemPath' =&gt; ‘/path/to/trunk/files/‘,
    ),
),


I feel that what I’d like to do is something like the following to differentiate between the two sites running on 10.0.1.2:

'10.0.1.2:8081’ =&gt; array(
    'userSessionDuration' =&gt; false,

    'environmentVariables' =&gt; array(
        'siteUrl'        =&gt; 'http://10.0.1.2:8081/',
        'fileSystemPath' =&gt; ‘/path/to/trunk/files/‘,
    ),
),

'10.0.1.2:8082’ =&gt; array(
    'userSessionDuration' =&gt; false,

    'environmentVariables' =&gt; array(
        'siteUrl'        =&gt; 'http://10.0.1.2:8082/',
        'fileSystemPath' =&gt; ‘/path/to/branch/files/‘,
    ),
),


Similarly, in the db.php file, currently I’ve got:

'10.0.1.2' =&gt; array(
    'server' =&gt; 'localhost',
    'user' =&gt; 'root',
    'password' =&gt; 'root',
    'database' =&gt; 'trunk.database’
),


Which I thought I’d change to:

'10.0.1.2:8081’ =&gt; array(
    'server' =&gt; 'localhost',
    'user' =&gt; 'root',
    'password' =&gt; 'root',
    'database' =&gt; 'trunk.database’
),

'10.0.1.2:8082’ =&gt; array(
    'server' =&gt; 'localhost',
    'user' =&gt; 'root',
    'password' =&gt; 'root',
    'database' =&gt; ‘branch.database’
),


But alas these kick off this error:

An exception has been thrown during the rendering of a template ("Craft canâ€™t connect to the database with the credentials in craft/config/db.php.") in "_layout" at line 15.


Am I trying to do something impossible, or can anyone point me in the right direction?

Thank you much,
Willhaus
You can define CRAFT_ENVIRONMENT in your index.php like so:

switch ($_SERVER['SERVER_PORT']) {
  case "8081":
    define('CRAFT_ENVIRONMENT', 'TRUNK');
    break;
  case "8082":
    define('CRAFT_ENVIRONMENT', 'BRANCH');
    break;
}


And then in general.php and db.php you can:

return array(
  '*' =&gt; array(
    ...
  ),
  'TRUNK' =&gt; array(
    ...
  ),
  'BRANCH' =&gt; array(
    ...
  )
);