<p>BOOM! Got it.</p>

<p>The following makes it work. I can leave the ip address as is, and the insert a test for which port I’m on. I never would have figured that without y’all’s help and another pass through the <a href="http://buildwithcraft.com/docs/multi-environment-configs" rel="nofollow">Multi-Environment Configs</a> doc.</p>

<p>I did this for <code>general.php</code>:</p>

<pre><code>'10.0.1.2' =&gt; array(
    'userSessionDuration' =&gt; false,

    'environmentVariables' =&gt; array(
        'siteUrl' =&gt; ($_SERVER['SERVER_PORT'] == '8081' ? 'http://10.0.1.2:8081/' : 'http://10.0.1.2:8082/'),
        'fileSystemPath' =&gt; ($_SERVER['SERVER_PORT'] == '8081' ? '/path/to/trunk/files/' : '/path/to/branch/files/'),
    ),
),
</code></pre>

<p>And this in <code>db.php</code>:</p>

<pre><code>'10.0.1.2' =&gt; array(
    'server' =&gt; 'localhost',
    'user' =&gt; 'root',
    'password' =&gt; 'root',
    'database' =&gt; ($_SERVER['SERVER_PORT'] == '8081' ? 'trunk.database' : ‘branch.database'),
),
</code></pre>

<p>THANK YOU, everyone, for your help!</p>

CC BY-SA 3.0
2014-07-24T18:41:07.677Z
Nice catch on the edit ;)
Willhaus
CC BY-SA 3.0
2014-07-24T17:16:30.947Z
2014-07-24T18:35:45.930Z
2014-07-24T18:35:45.930Z
Willhaus
<p>You can define <code>CRAFT_ENVIRONMENT</code> in your <em>index.php</em> like so:</p>

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

<p>And then in <em>general.php</em> and <em>db.php</em> you can:</p>

<pre><code>return array(
  '*' =&gt; array(
    ...
  ),
  'TRUNK' =&gt; array(
    ...
  ),
  'BRANCH' =&gt; array(
    ...
  )
);
</code></pre>

CC BY-SA 3.0
2014-07-24T08:06:15.060Z
Great workaround, Marion! Do you also have an idea on how to serve different templates to those environments? Willhaus wants to use "branch" for developing new features, what I think is a great concept for site development. Serving "branch" a different database should work with this config, but I have no clue on how to deal with the templates (see my comments to Brads answer)?
carlcs
CC BY-SA 3.0
2014-07-24T14:25:12.920Z
@marion-newlevant Whoooooaaaaaaaa, thanks! I think that just might do it. It's kind of a drag to edit `index.php` since it's so nice to keep all your configs together in, duh, `craft/config`, but I'll take it if it works. I'll report back after I get a chance to test.
Willhaus
CC BY-SA 3.0
2014-07-24T14:30:44.677Z
@christian-seelbach Using Marion's method, I should be able to use `fileSystemPath` to define my whole separate branch install, allowing for not just separate templates, but plugins, etc also. Thx, and I'll report back if/when I get it working.
Willhaus
CC BY-SA 3.0
2014-07-24T15:11:34.593Z
I think you are wrong, @Willhaus. The *custom* vars you define in [`environmentVariables`](http://buildwithcraft.com/docs/multi-environment-configs#environment-specific-variables) are nowhere as deeply integrated into Craft as you hope. These vars only work in "asset source settings" and "general settings" in the CP.
carlcs
CC BY-SA 3.0
2014-07-24T15:14:44.173Z
You could as well rename `fileSystemPath` to `myWebhostWantsMeToPutMyFilesHerePath`, the var name is up to you!
carlcs
CC BY-SA 3.0
2014-07-24T15:52:21.943Z
Have you ever used a multi-environment config? What I'm describing is exactly why that feature exists. The only difference is that I want 2 of my environments to be on the same machine running on different ports.
Willhaus
CC BY-SA 3.0
2014-07-24T16:20:14.830Z
Alas, @marion-newlevant, I can't get that to work. I get the same `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.` error as before. You were referring to the `index.php` in the rot of the content folder, right?
Willhaus
CC BY-SA 3.0
2014-07-24T16:56:50.460Z
Oooof, sorry if that soured a little harsh, @christian-seelbach. What I meant to impart was that if `config/general.php` would pay attention to the port, `'fileSystemPath' => ‘/path/to/trunk/files/‘` and `'fileSystemPath' => ‘/path/to/branch/files/‘` do a fine job of pointing to different sets of craft install files.
Willhaus
CC BY-SA 3.0
2014-07-24T19:04:11.467Z
It wasn't clear to me that you sync those versions and end up having two totally separate Craft installs for "trunk" and "branch" on your machine, @Willhaus! The way I use a "multi-environment" config for *local development* is to have *ONE INSTALL* and configure the files to allow access from different clients (`craft.dev` same machine,  `10.0.1.2` network, `craft.dyndns.org` external).
carlcs
CC BY-SA 3.0
2014-07-24T19:04:47.077Z
Additionally I can eg. enable/disable robots or [Craft caching dependent on the environment](http://craftcms.stackexchange.com/a/106/125) and I thought that maybe there is a way to serve different *templates* from that one install to my environments as well? Then all one has to do is to add another environment (eg. `branch.willhaus.dev`) and test new layout/features with these files.
carlcs
CC BY-SA 3.0
2014-07-24T06:57:46.457Z
2014-07-24T06:57:46.457Z
Marion Newlevant
<p>When matching the array keys in a multi-environment config, Craft checks the $_SERVER['SERVER_NAME'], which does not include the port, which is in <a href="http://php.net/manual/en/reserved.variables.server.php" rel="nofollow">$_SERVER['SERVER_PORT']</a>, so it's currently not going to work the way you expect.</p>

<p>But it will do a partial match (since it just uses <a href="http://php.net/manual/en/function.strpos.php" rel="nofollow">strpos</a>) and successfully match the first key if finds.  In your case, '10.0.1.2:8081'.</p>

<p>If your MySQL install is running on a non-default (3306) port as well, there is a separate db.php config item for 'port' that you'd need to specify.</p>

CC BY-SA 3.0
2014-07-24T05:51:24.527Z
The "fileSystemPath" variable defined in "environmentVariables" is a custom variable. You can probably use it to configure sections' template path in the CP. But how can you have different home pages (or other pages where URI to template path matching is required) bound to the environments, Brad?
carlcs
CC BY-SA 3.0
2014-07-24T06:02:21.567Z
Not sure I'm following the question (it *is* pretty late here)... currently there is only 3 settings in Craft that support environment variables: http://buildwithcraft.com/docs/multi-environment-configs#setting-support
Brad Bell
CC BY-SA 3.0
2014-07-24T06:13:07.900Z
As Willhaus wants to serve different templates dependent on the environment, I thought about how to realize that. Using the "fileSystemPath" in the section settings came to mind, but if I interpret you right (it's pretty early here), this is not an option as environment vars won't work there (→ feature request?).
carlcs
CC BY-SA 3.0
2014-07-24T14:37:17.930Z
Maybe a feature request is a good idea. I would suspect there are others who would want the same set up. After all, what I am looking for is already possible easily using "name-based virtual hosting" (is that the right term?). I could use `'trunk.dev’ => array(…` and `'branch.dev’ => array(…` and it would work perfectly. But that would only work locally – we'd loose the ability to share links through the studio to show our work. I the meantime, I'm going to try Marion's suggestion to edit index.php to capture the ports.
Willhaus
CC BY-SA 3.0
2014-07-24T15:25:42.320Z
Why not add the vhosts to `hosts` file in `/private/etc/` on each machine, @Willhaus?
carlcs
CC BY-SA 3.0
2014-07-24T16:11:47.553Z
Oh, man, do mean map each ip + port to each vhost on each machine? I guess that would work if I could figure it out, but it sure does seem more cumbersome than editing 2 config files synced via version control.
Willhaus
CC BY-SA 3.0
2014-07-24T19:05:06.053Z
@Willhaus, I didn't mean this to be a replacement for your environmental configs in Craft. In my opinion the convenience is definitely worth it! You'd have `branch.willhaus.dev` instead of `10.0.1.2:8082` (and before you ask, yes I've ever used bookmarks!).
carlcs
CC BY-SA 3.0
2014-07-24T05:14:08.360Z
2014-07-24T05:14:08.360Z
Brad Bell
<p>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.</p>

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

<p>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.</p>

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

<pre><code>'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/‘,
    ),
),
</code></pre>

<p>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:</p>

<pre><code>'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/‘,
    ),
),
</code></pre>

<p>Similarly, in the db.php file, currently I’ve got:</p>

<pre><code>'10.0.1.2' =&gt; array(
    'server' =&gt; 'localhost',
    'user' =&gt; 'root',
    'password' =&gt; 'root',
    'database' =&gt; 'trunk.database’
),
</code></pre>

<p>Which I thought I’d change to:</p>

<pre><code>'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’
),
</code></pre>

<p>But alas these kick off this error:</p>

<pre><code>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.
</code></pre>

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

<p>Thank you much,
Willhaus</p>

<config><multi-environment>
Multi-environment configs and running sites on different ports
# Multi-environment configs and running sites on different ports

(Asked by: username_0 on 2014-07-24)

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' => array(
    'userSessionDuration' => false,

    'environmentVariables' => array(
        'siteUrl'        => 'http://10.0.1.2:8081/',
        'fileSystemPath' => ‘/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’ => array(
    'userSessionDuration' => false,

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

'10.0.1.2:8082’ => array(
    'userSessionDuration' => false,

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

Similarly, in the db.php file, currently I’ve got:
'10.0.1.2' => array(
    'server' => 'localhost',
    'user' => 'root',
    'password' => 'root',
    'database' => 'trunk.database’
),

Which I thought I’d change to:
'10.0.1.2:8081’ => array(
    'server' => 'localhost',
    'user' => 'root',
    'password' => 'root',
    'database' => 'trunk.database’
),

'10.0.1.2:8082’ => array(
    'server' => 'localhost',
    'user' => 'root',
    'password' => 'root',
    'database' => ‘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

**Tags:** config, multi-environment

---

## 3 answers

### Answer by username_0 on 2014-07-24. Score: 3 | Accepted answer

BOOM! Got it.
The following makes it work. I can leave the ip address as is, and the insert a test for which port I’m on. I never would have figured that without y’all’s help and another pass through the Multi-Environment Configs doc.
I did this for general.php:
'10.0.1.2' => array(
    'userSessionDuration' => false,

    'environmentVariables' => array(
        'siteUrl' => ($_SERVER['SERVER_PORT'] == '8081' ? 'http://10.0.1.2:8081/' : 'http://10.0.1.2:8082/'),
        'fileSystemPath' => ($_SERVER['SERVER_PORT'] == '8081' ? '/path/to/trunk/files/' : '/path/to/branch/files/'),
    ),
),

And this in db.php:
'10.0.1.2' => array(
    'server' => 'localhost',
    'user' => 'root',
    'password' => 'root',
    'database' => ($_SERVER['SERVER_PORT'] == '8081' ? 'trunk.database' : ‘branch.database'),
),

THANK YOU, everyone, for your help!

> **Comments:**
> **username_0**: Nice catch on the edit ;)  

---

### Answer by username_1 on 2014-07-24. Score: 4

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(
  '*' => array(
    ...
  ),
  'TRUNK' => array(
    ...
  ),
  'BRANCH' => array(
    ...
  )
);

> **Comments:**
> **username_2**: Great workaround, Marion! Do you also have an idea on how to serve different templates to those environments? Willhaus wants to use "branch" for developing new features, what I think is a great concept for site development. Serving "branch" a different database should work with this config, but I have no clue on how to deal with the templates (see my comments to Brads answer)?  
> **username_0**: @marion-newlevant Whoooooaaaaaaaa, thanks! I think that just might do it. It's kind of a drag to edit `index.php` since it's so nice to keep all your configs together in, duh, `craft/config`, but I'll take it if it works. I'll report back after I get a chance to test.  
> **username_0**: @christian-seelbach Using Marion's method, I should be able to use `fileSystemPath` to define my whole separate branch install, allowing for not just separate templates, but plugins, etc also. Thx, and I'll report back if/when I get it working.  
> **username_2**: I think you are wrong, @Willhaus. The *custom* vars you define in [`environmentVariables`](http://buildwithcraft.com/docs/multi-environment-configs#environment-specific-variables) are nowhere as deeply integrated into Craft as you hope. These vars only work in "asset source settings" and "general settings" in the CP.  
> **username_2**: You could as well rename `fileSystemPath` to `myWebhostWantsMeToPutMyFilesHerePath`, the var name is up to you!  
> **username_0**: Have you ever used a multi-environment config? What I'm describing is exactly why that feature exists. The only difference is that I want 2 of my environments to be on the same machine running on different ports.  
> **username_0**: Alas, @marion-newlevant, I can't get that to work. I get the same `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.` error as before. You were referring to the `index.php` in the rot of the content folder, right?  
> **username_0**: Oooof, sorry if that soured a little harsh, @christian-seelbach. What I meant to impart was that if `config/general.php` would pay attention to the port, `'fileSystemPath' => ‘/path/to/trunk/files/‘` and `'fileSystemPath' => ‘/path/to/branch/files/‘` do a fine job of pointing to different sets of craft install files.  
> **username_2**: It wasn't clear to me that you sync those versions and end up having two totally separate Craft installs for "trunk" and "branch" on your machine, @Willhaus! The way I use a "multi-environment" config for *local development* is to have *ONE INSTALL* and configure the files to allow access from different clients (`craft.dev` same machine,  `10.0.1.2` network, `craft.dyndns.org` external).  
> **username_2**: Additionally I can eg. enable/disable robots or [Craft caching dependent on the environment](http://craftcms.stackexchange.com/a/106/125) and I thought that maybe there is a way to serve different *templates* from that one install to my environments as well? Then all one has to do is to add another environment (eg. `branch.willhaus.dev`) and test new layout/features with these files.  

---

### Answer by username_3 on 2014-07-24. Score: 3

When matching the array keys in a multi-environment config, Craft checks the $\_SERVER['SERVER\_NAME'], which does not include the port, which is in [$\_SERVER['SERVER\_PORT']](http://php.net/manual/en/reserved.variables.server.php), so it's currently not going to work the way you expect.

But it will do a partial match (since it just uses [strpos](http://php.net/manual/en/function.strpos.php)) and successfully match the first key if finds. In your case, '10.0.1.2:8081'.

If your MySQL install is running on a non-default (3306) port as well, there is a separate db.php config item for 'port' that you'd need to specify.

> **Comments:**
> **username_2**: The "fileSystemPath" variable defined in "environmentVariables" is a custom variable. You can probably use it to configure sections' template path in the CP. But how can you have different home pages (or other pages where URI to template path matching is required) bound to the environments, Brad?  
> **username_3**: Not sure I'm following the question (it *is* pretty late here)... currently there is only 3 settings in Craft that support environment variables: http://buildwithcraft.com/docs/multi-environment-configs#setting-support  
> **username_2**: As Willhaus wants to serve different templates dependent on the environment, I thought about how to realize that. Using the "fileSystemPath" in the section settings came to mind, but if I interpret you right (it's pretty early here), this is not an option as environment vars won't work there (→ feature request?).  
> **username_0**: Maybe a feature request is a good idea. I would suspect there are others who would want the same set up. After all, what I am looking for is already possible easily using "name-based virtual hosting" (is that the right term?). I could use `'trunk.dev’ => array(…` and `'branch.dev’ => array(…` and it would work perfectly. But that would only work locally – we'd loose the ability to share links through the studio to show our work. I the meantime, I'm going to try Marion's suggestion to edit index.php to capture the ports.  
> **username_2**: Why not add the vhosts to `hosts` file in `/private/etc/` on each machine, @Willhaus?  
> **username_0**: Oh, man, do mean map each ip + port to each vhost on each machine? I guess that would work if I could figure it out, but it sure does seem more cumbersome than editing 2 config files synced via version control.  
> **username_2**: @Willhaus, I didn't mean this to be a replacement for your environmental configs in Craft. In my opinion the convenience is definitely worth it! You'd have `branch.willhaus.dev` instead of `10.0.1.2:8082` (and before you ask, yes I've ever used bookmarks!).  

---

