Jump to content

Having multiple databases (hacking PW2 to work on two different servers)


Adam Kiss
 Share

Recommended Posts

Hi Ryan,

is it possible to hack something in PW, so it will work on two different servers? (staging AND live server)

something like (pseudocode):

  $LIVE = (set_by_url) ? true : false;
  if($LIVE){
   $db = 'hujul'; $db_user = 'user'; $db_pw = 'pw'; $db_server = 'this-one.com';
  }else{
   $db = 'staging-db'; $db_user = 'root'; $db_pw = 'root'; $db_server = 'staging.that-one.com';
  }
  • Like 2
Link to comment
Share on other sites

What you suggest should work just fine. Using your example, here's how I'd code it in your /site/config.php:

Solution 1:

<?php
// If your dev/staging isn't 'localhost' then replace it with 
// whatever $_SERVER['HTTP_HOST'] is on your dev server: 

if($_SERVER['HTTP_HOST'] == 'localhost') {
   // staging or dev server
   $config->dbHost = 'localhost';
   $config->dbName = 'pw2_blank';
   $config->dbUser = 'root';
   $config->dbPass = 'root';
   $config->dbPort = '3306';
} else {
   // live server
   $config->dbHost = 'something';
   $config->dbName = 'db_name';
   $config->dbUser = 'db_user';
   $config->dbPass = 'db_pass';
   $config->dbPort = '3306';
}

Solution 2:

Another solution, and the one I use is to maintain a separate config file called /site/config-dev.php. ProcessWire looks for this file, and if it sees it, it'll use that rather than /site/config.php. If you just keep this file on your staging/dev server, and not on your live server, then that solves it for you. I also added that file to my .gitignore file so that it doesn't ever get deployed, while /site/config.php does.

To get started with this method, just copy your /site/config.php to /site/config-dev.php (on your staging/dev server) and change the database settings in the config-dev.php version.

  • Like 2
Link to comment
Share on other sites

  • 2 years later...

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...