When developing a site it’s good to have levels of separation between your development environment and a live production server. I normally have three levels when creating a site:
- This server is mine, I can do what I want on it and destroy what I please. Clients and users will have absolutely no visibility of it. There will be frequent changes of varying size and complexity.
- This is where clients start to come in. Using this server they can view major versions that I release from Development when they are ready. This should be a stable server but will have well controlled occasional changes.
- The live server, the production site. This is where approved changes finally end up, this is the site that all users will see. Changes will be infrequent and well checked.
I use vague timings in the sections above because it really depends on the customer and their needs. From my personal experience development changes will happen on a daily or hourly basis. QA changes will happen a few times a week once features or bug fixes have been completed. Live changes will have every week or two when the client is happy with any changes.
I do a lot of code control using SVN/Git and branches with this methodology. Individual features or groups of features reside with their own branch. The development branch is merged into the QA branch for testing. Once the QA branch is approved it is merged into trunk, which is what resides on live.
When you’re managing three different environments you don’t want to have to manually make any changes to branches to ensure that they will work. I don’t want a development specific WP config file that I’ll have to change when I want to update the QA server! This would be time consuming and rather pointless.
WP Config and Environment variables
When developing, inevitably your servers will have minor differences in configuration. This could be different database names, different requirements such as memcache, or maybe other variables.
To give ourselves a generic WP Config file that will adapt to the server it’s on I use Apache environment variables.
SetEnv ENVIRONMENT QA
In your apache configuration file for your site you can set environment variables. These can be utilised in PHP to identify the server you are on, and be used to adapt your WP Config file. With just this simple line in your wp-config.php file you will now have a variable to identify the server you are on:
if( isset( $_SERVER[ 'ENVIRONMENT' ] ) ) define( 'ENVIRONMENT', $_SERVER[ 'ENVIRONMENT' ] );
If I wanted to change my database name based on the server environment I’m on I can now use the variable ‘ENVIRONMENT’. I could name my databases to follow this ‘database_dev’, ‘database_qa’, ‘database_live’ for example, and then I would only need one line in my WP config file to make full use of the environment variable:
define( 'DB_NAME', 'database_' . ENVIRONMENT );
This variable can be used to change many variables in your WP config should you require it. Or if you want to be a bit less generic you can use some if clauses:
if ( 'LIVE' == ENVIRONMENT )
This could be used to set up some live specific variables, like memcache.
Having access to this one variable will give you a huge amount of control over your sites set up and maintenance. Use it!