I am trying to create multisites and the first one is up fine and running. I am trying to use drush to install a 2nd site but I need the settings.php file to point to a different db. Can I modify my drush command to do that or am I going to have to modify the settings.php by hand each time?

sudo drush -y site-install standard --db-url=mysql://$username:$pass@$dbname --site-name="$sitename" --sites-subdir="$sitename"
  • The command above should create a settings.php file for each site -- presuming that $dbname is in fact different on each run. Are you observing any different behavior than the expected when you run it? Aug 22 '13 at 19:50
  • The db i'm creating is on my local box but the settings file its copying points to a corp server. So when it copies the settings.php file its confused because settings points one place and I'm telling it another. I did just find this drupal.org/node/986740 and they talk about using aliases in the comments. Wonder if that would work?
    – roadzy
    Aug 22 '13 at 19:54
  • I still cannot tell what you are trying to do; however, related to that issue you reference, no, Drush absolutely will not let you change the way your settings.php file works by putting information (e.g. db-url) into a site alias. If you need to dynamically adjust the $databases array (e.g. to show something different to Drush than Drupal sees), then you should add php code to settings.php to allow for that. That is something that you'll have to hand-code; you couldn't get site-install to edit settings.php like that. Aug 23 '13 at 0:35

What you really need here is different settings.php files for your production and development environments.

There are multiple ways of achieving this, some examples:

I usually include the following lines at the end of the settings.php file (in all instances):

// Override settings in this file.
if (file_exists('./' . conf_path() . '/local_settings.php')) {
  include_once './' . conf_path() . '/local_settings.php';

And then change the $databases array there, and any other specific environment settings.

You may also find useful the following links:

  • He could not commit settings.php and use a custom drush function to his project or system to implement the rewrite_settings() as needed. Thus being DVCS pure and getting the functionality he wants. The approach you outline, works, but adds alot of noise into the build process.
    – tenken
    Jul 9 '15 at 23:09
  • Instead of calling drush, couldn´t the settings be appended to the settings.php or settings.local.php file with a shell command? Like this: echo "\$databases = array();" >> docroot/sites/default/settings.php"
    – Pere
    Jul 10 '15 at 9:41
  • Sure, but then you basically loose out on use drush and the drupal api to do work for you. Bash skills are great, but there is alot to learn from understanding how drupal is installed on a blank system. which is where drupal_rewrite_settings() is used, when an installation profile is run (for D7). just an fyi, i've literally used all 3 solutions here: your bash solution(s), Environment modules, and drupal_rewrite_settings() ... they all have their use-cases.
    – tenken
    Jul 10 '15 at 14:51
  • I still think the best approach is to have a separate local settings file. It's quite common to have settings specific to environments (like different debug levels, css/js compression...) and I like to keep these settings in code. I've been building and uploading a local settings file in my automated builds, and I would say the required few lines of code are totally worth it.
    – Pere
    Jul 10 '15 at 17:35
  • Knowing how to use drupal_rewrite_settings() may be useful but does not look like an abstraction layer to settings.php. Drupal developers are expected to understand how settings.php work and are loaded, and at the same time knowing the level of bash/ruby/etc required to do these steps is basic and IMHO a must when doing backend web development.
    – Pere
    Jul 10 '15 at 17:41

You can try to use drupal_rewrite_settings() function (in install.inc) which Drupal uses to write the $databases variable to your settings.php, in example:

drush eval '
include DRUPAL_ROOT."/includes/install.inc";
include DRUPAL_ROOT."/includes/update.inc";
global $db_prefix;
$db["databases"]["value"] = update_parse_db_url("mysql://root:root@localhost/drupal7", $db_prefix);
drupal_rewrite_settings($db, $db_prefix);

This will override your settings.php file so use it with caution. Change db url (mysql://root:root@localhost/drupal7) based on your correct db credentials.


Alternatively you can check similar approach from update.inc:

function update_fix_d7_requirements() {
  // Rewrite the settings.php file if necessary, see
  // update_prepare_d7_bootstrap().
  global $update_rewrite_settings, $db_url, $db_prefix;
  if (!empty($update_rewrite_settings)) {
    $databases = update_parse_db_url($db_url, $db_prefix);
    $salt = drupal_hash_base64(drupal_random_bytes(55));
    file_put_contents(conf_path() . '/settings.php', "\n" . '$databases = ' . var_export($databases, TRUE) . ";\n\$drupal_hash_salt = '$salt';", FILE_APPEND);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.