Jump to content

Multi-environment Config?


AJ Troxell
 Share

Recommended Posts

I have searched the forums and PW documentation and can't find anything on this topic. Is there a config someone has implemented for using PW in multiple environments? I'm using Git with DeployHQ to deploy changes and would like a config that will detect which environment the site is on thusly loading the appropriate DB details.

Link to comment
Share on other sites

This is sort-of built in to ProcessWire already on a basic level.

If you make a copy of your config.php file called config-dev.php, then that one will take priority over config.php. Just ignore the -dev version in DeployHQ settings, or add it to .gitignore so it doesn't even get committed - up to you.

As long as config-dev.php on your local system has your local settings in, and your config.php is treated as your live version, it should be fine :)

  • Like 4
Link to comment
Share on other sites

Not at the moment, that I know of.

One way you could do this, is to reduce your config.php down to some detection code, make different copies of your configs for different environments, and then just include the relevant one.

if (strpos($_SERVER['HTTP_HOST'], '.local') !== FALSE)
	require('config.local.php');

if (strpos($_SERVER['HTTP_HOST'], 'staging.') !== FALSE)
	require('config.staging.php');

if ( ! defined('ENVIRONMENT'))
	require('config.production.php');

I haven't tested that, but I don't think ProcessWire does weird things with config.php so it should work. Give it a go :)

Link to comment
Share on other sites

When the need is there for separate DB configurations and something more than config-dev.php, I've done it like this in my /site/config.php file: 

switch($_SERVER['SERVER_NAME']) {
  case 'localhost':
    // set database settings for localhost
    break;
  case 'dev.domain.com':
    // set database settings for staging server
    break;
  default: 
    // set database settings for production server
}

You should give preference to SERVER_NAME in this case (over HTTP_HOST) just because HTTP_HOST is coming from the client rather than the server (open to tampering). Though it doesn't really matter as long as your last "default" condition assumes the production server. 

  • Like 7
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...