settings.php

Clean Up settings.php

Typically, when I build sites, I delete all of the comments, and unused options in settings.php. If I ever need to know available option, or what an option does, I can look in the default.settings.php file.

So, for this site, if I do that, I'm left with the following in my settings.php file:

<?php

/**
 * Salt for one-time login links, cancel links, form tokens, etc.
 */
$settings['hash_salt'] = '';

/**
 * Access control for update.php script.
 */
$settings['update_free_access'] = FALSE;

/**
 * Load services definition file.
 */
$settings['container_yamls'][] = __DIR__ . '/services.yml';

/**
 * The default list of directories that will be ignored by Drupal's file API.
 *
 * @see file_scan_directory()
 * @see \Drupal\Core\Extension\ExtensionDiscovery::scanDirectory()
 */
$settings['file_scan_ignore_directories'] = [
  'node_modules',
  'bower_components',
];

/**
 * Load local development override configuration, if available.
 * Keep this code block at the end of this file to take full effect.
 */

# if (file_exists(__DIR__ . '/settings.local.php')) {
# include __DIR__ . '/settings.local.php';
# }

$databases['default']['default'] = array (
  'database' => 'drupal8site',
  'username' => 'root',
  'password' => 'root',
  'prefix' => '',
  'host' => '127.0.0.1',
  'port' => '3306',
  'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
  'driver' => 'mysql',
);

$settings['install_profile'] = 'standard';

$config_directories['sync'] = 'sites/default/files/config_/sync';

A mere 50 lines, as opposed to the original 766! Much easier to read, and modify when needed. (Not to mention that we're about to move 10 of them to a settings.local.php file!)

Prepare settings.php to Include the settings.local.php File

You'll notice that there's still a commented-out section that's used to "Load local development override configuration, if available." that references a settings.local.php file. That file is were you might store the database credentials for each instance of your site, as well as configuration overrides that are specific to one instance of the site. Let's set that up.

First, I'll uncomment the three lines:

if (file_exists(__DIR__ . '/settings.local.php')) {
  include __DIR__ . '/settings.local.php';
}

This basically means, "if there's a settings.local.php file next to this one, read and include everything in that file now."

Then, you'll notice that it says to "Keep this code block at the end of this file to take full effect." This is important, because if we were, for example, to leave the database credentials where they are, and then try to override them in the settings.local.php file, the local credentials would load when the local file is included, but then the ones here would override them because they come later in the file.

So, I'll just copy the whole block and move it to the very end of the settings.php file.

Also, since I'm using MAMP, I'm going to add the following line to the databases array that will ensure I can use Drush and other command line tools with this site:

'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',

settings.local.php

Create settings.local.php

Because the "sites/default" directory has write permissions protected, we need to open up the permissions of the "default" directory, create the file, and then set the "default" directory permissions back to 555.

  • cd sites/ Move into the "sites" directory
  • ls -al This shows the current permissions of the "default" directory. (dr-xr-xr-x is equivalent to 555)
  • chmod 777 default/ This sets the permissions of the "default" directory to 777 so that you can do anything in it. This is NOT secure though, so we don't want to keep it this way.
  • touch default/settings.local.php Create an empty file named "settings.local.php" in the default directory.
  • chmod 555 default/ Set the permissions back to a more secure 555.
  • ls -al to verify the permissions are back to the way they were when we started.

Move Database Settings into settings.local.php

The first thing we'll put in our settings.local.php file is the database information for this machine. It's as simple as copying and pasting the credentials from settings.php to settings.local.php.

The reason we're doing this is so that we can share some configuration across all instances of our site (like the $config_directories setting), while keeping settings that vary per instance separate (like the database credentials, which are often different on each machine, and might contain information like the "unix_socket" that I added for MAMP that only applies to one instance.)

You will need to type "<?php" at the beginning of the file so that it is registered as php code, and usable by Drupal. I also like to include the setting description. So my settings.local.php file now looks like this:

<?php

/**
* Database settings:
*/
$databases['default']['default'] = array (
  'database' => 'drupal8site',
  'username' => 'root',
  'password' => 'root',
  'prefix' => '',
  'host' => '127.0.0.1',
  'port' => '3306',
  'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
  'driver' => 'mysql',
  'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
);