Using one Drupal settings file online and locally

Sep 03, 2010
by Ivan

Let's say you have this setup: You work on a site offline connected to a local mysql server (say MAMP), and you make a lot of changes and upload them online to the production site. The usual problem is that you have to avoid adding the settings.php file to your repository because if you deploy using something like Capistrano, you will have to change the database settings every time you deploy. 

A simple solution is using $_SERVER[]

if ($_SERVER['SERVER_NAME'] === 'productionserver.com')
	$db_url = 'mysqli://username:password@onlineserver.com/production_database';
else
	$db_url = 'mysqli://root:root@localhost/local_database';

 Pretty simple, right? Now it will load different databases depending on the environment (domain) you are working on. You can obviously play around with this.

Posted in