To me, it's incredibly refreshing when code matches through all stages of development. Whether I'm using a diff tool like Changes and WinMerge or a source/version control system, the less server-specific code differences I see, the better.
Two files that typically have differences in C5 are /config/site.php
and /.htaccess
. Fortunately, we've come up with some snippets that keep these files identical from development to production. To keep this short and specific, I'll cover /config/site.php
here and save .htaccess
for another post.
A fresh C5 install on my mac running MAMP, creates a config.php
file that looks like this:
<?php define('DB_SERVER', 'localhost'); define('DB_USERNAME', 'root'); define('DB_PASSWORD', 'root'); define('DB_DATABASE', 'concrete5tricks'); define('PASSWORD_SALT', 'OAj8Cg617yiK2utYKHOtoIhiemu9WL7nBTk9m78sLx7x38D1nqpQhyXrjUehello');
In our setup, when I want to move this C5 site to production, I'll need to change DB_USERNAME
, DB_PASSWORD
. In some setups, DB_SERVER
and DB_DATABASE
might need to change as well. But PASSWORD_SALT
and other variables wouldn't necessarily need to.
It'd be pretty great if we didn't have to change any variables when we move code to production. Well, this is a PHP file after all. And PHP's $_SERVER tells us a lot about our environment. So, let's take advantage and set these variables conditionally.
<?php /* SERVER SPECIFIC VARS ----------------------------------------------------*/ /*** LOCAL ***/ if (strpos($_SERVER['SERVER_NAME'], 'localhost') !== false) { define('DB_USERNAME', 'root'); define('DB_PASSWORD', 'root'); /*** PRODUCTION ***/ } else { define('DB_USERNAME', 'concrete5tricks'); define('DB_PASSWORD', 'thisisnotourrealpasswordsmileyface'); } /* SERVER INDEPENDENT VARS ----------------------------------------------------*/ define('DB_SERVER', 'localhost'); define('DB_DATABASE', 'concrete5tricks'); define('PASSWORD_SALT', 'OAj8Cg617yiK2utYKHOtoIhiemu9WL7nBTk9m78sLx7x38D1nqpQhyXrjUehello');
In the title, I also mentioned "stage". It's pretty common to put up a stage site for clients to review or work in prior to going live on production. We usually launch these sites at a subdomain like http://clientname.myagency.com
. So, staging can be handled by adding the following between the local and production conditionals above:
/*** STAGE ***/ } else if (strpos($_SERVER['SERVER_NAME'], '.myagency.com') !== false) { define('DB_USERNAME', 'concrete5tricks'); define('DB_PASSWORD', 'alsonotourrealpassword');
So, the final config would look like this:
<?php /* SERVER SPECIFIC VARS ----------------------------------------------------*/ /*** LOCAL ***/ if (strpos($_SERVER['SERVER_NAME'], 'localhost') !== false) { define('DB_USERNAME', 'root'); define('DB_PASSWORD', 'root'); /*** STAGE ***/ } else if (strpos($_SERVER['SERVER_NAME'], '.myagency.com') !== false) { define('DB_USERNAME', 'concrete5tricks'); define('DB_PASSWORD', 'alsonotourrealpassword'); /*** PRODUCTION ***/ } else { define('DB_USERNAME', 'concrete5tricks'); define('DB_PASSWORD', 'thisisnotourrealpasswordsmileyface'); } /* SERVER INDEPENDENT VARS ----------------------------------------------------*/ define('DB_SERVER', 'localhost'); define('DB_DATABASE', 'concrete5tricks'); define('PASSWORD_SALT', 'OAj8Cg617yiK2utYKHOtoIhiemu9WL7nBTk9m78sLx7x38D1nqpQhyXrjUehello');
There you have it. A config file that can be the same across all servers.
Comments
I have come across a great tool that can handle the dev > live migration of your files, it has a very simple interface, can show you changed files and whats changed and you can simply 1 click transfer files between your dev and live systems.
http://migrationmagic.co.uk
/application/config/database.php:
Commenting has been disabled.