How to setup a Local Settings.php file for Drupal

Submitted by Daniel Henry on 05/22/2013 - 11:04:am

As the number of sites I maintain were growing, I realized it was way too difficult, and a bit annoying to keep track of all of their server locations, API’s, and cache settings. Every time I wanted to update the customers database I have to go through a series of steps to disable secure pages, change the mobile/desktop URLs, disable caching, and switch all the APIs to their test gateways.

Thankfully the solution was quick to introduce itself: set the variables in the settings.php.

First it’s important to state that we do not store settings.php files in our git site repos. The file is very specific to each server and sometimes contains security info like database user name and passwords. Instead, we add the following overrides to 3 separate files in the repo:

  1. settings.dev.php
  2. settings.staging.php
  3. settings.prod.php

Then we can just include the appropriate one of those into the given servers settings.php file using the following code at the end of the setting.php file:

  1. // Additional site configuration settings.
  2. if (file_exists('/path/to/site/sites/default/settings.dev.php')) {
  3.   include_once('/path/to/site/sites/default/settings.dev.php');
  4. }

You can then forget about what APIs and module settings need to change between platforms and just set them in your environment settings.php includes.

Here are some common overrides for the various platforms.

settings.dev.php:

  1. // Local
  2. global $conf;
  3.  
  4. // Environment Indicator Devlopment. Environment Indicator Module.
  5. $conf['environment_indicator_color'] = 'blue';
  6. $conf['environment_indicator_enabled'] = TRUE;
  7. $conf['environment_indicator_text'] = 'Development Server';
  8.  
  9. // Secure Pages. Secure Pages Module.
  10. $conf['securepages_enable'] = FALSE;
  11. $conf['https'] = FALSE;
  12.  
  13. // Robots disable Staging, development. ROBOTS.TXT module.
  14. $conf['robotstxt'] = 'User-agent: *
  15. Disallow: /';
  16.  
  17. // Turn off Caching and such. Core Modules.
  18. $conf['cache'] = FALSE;
  19. $conf['page_compression'] = FALSE;
  20. $conf['preprocess_css'] = FALSE;
  21. $conf['css_gzip'] = FALSE;
  22. $conf['preprocess_js'] = FALSE;
  23. $conf['javascript_aggregator_gzip'] = FALSE;

Typically, staging should be as close to the production site as possible. However, it's still at a different url and using test APIs, so dump the cache disable stuff but make sure to keep API settings.

settings.stage.php

  1. // Local
  2. global $conf;
  3.  
  4. // Environment Indicator Staging
  5. $conf['environment_indicator_color'] = 'orange';
  6. $conf['environment_indicator_enabled'] = TRUE;
  7. $conf['environment_indicator_text'] = 'Staging Server';
  8.  
  9. // Secure Pages
  10. $conf['securepages_enable'] = TRUE;
  11. $conf['https'] = TRUE;
  12. $conf['securepages_basepath'] = 'http://mysitestage.com';
  13. $conf['securepages_basepath_ssl'] = 'https://mysitestage.com';
  14.  
  15.  
  16. // Robots disable Staging, development
  17. $conf['robotstxt'] = 'User-agent: *
  18. Disallow: /';
  19.  
  20. // Mobile Tools
  21. $conf['mobile_tools_desktop_url'] = 'http://mysitestage.com';
  22. $conf['mobile_tools_mobile_url'] = 'http://m.mysitestage.com';
  23.  
  24. // UC test gateway.
  25. $conf['uc_payment_credit_gateway'] = 'test_gateway';
  26.  
  27. // File Directory (d7)
  28. $conf['file_public_path'] = 'sites/default/files';
  29. $conf['file_temporary_path'] = 'sites/default/temp';
  30. $conf['file_private_path'] = 'sites/default/private';

A warning:, I rarely, if ever, enter info into settings.prod.php. It’s a good thing to have available, but it can make it difficult to fix issues on the fly and have unexpected results. I once had the cache variable set to true on a prod site and it completely disabled cache flush on that site. Meaning the content did not ever refresh. This may have been isolated to some of the fancy caching we did on that site, but it should make the point. So typically I’ll only be setting environment indicator.

settings.prod.php:

  1. // Environment Indicator
  2. $conf['environment_indicator_color'] = 'red';
  3. $conf['environment_indicator_enabled'] = TRUE;
  4. $conf['environment_indicator_text'] = 'Warning || Live Server';

There you have it! I can pull a recent backup copy of the live database, upload it to my test system, and start development immediately without worrying about test APIs’s, secure pages, mobile site settings, or Google indexing my development site.