15 5 / 2011
How To: Configure ExpressionEngine for Multiple Server Environments
One of the challenges ExpressionEngine developers who have separate development, staging, and production environments face is configuring EE to work in all three of those environments without having to modify code each time. Over the last few months, I’ve been looking for ways to make the EE configuration as dynamic as possible and here is what I’ve learned. The methods described here only pertain to ExpressionEngine 2.x.
My Workflow
At Paramore, I usually develop locally using MAMP. At regular intervals, I’ll check in my code changes to Subversion which automatically pushes them to our staging server via my commit hooks so the rest of the team (and the client for that matter) can see the new versions. When we’re ready to go-live, we push the code from staging to production. In all three of these environments, it’s possible that the urls, file paths, database credentials, and even the database itself can be different. We don’t want to have to change these paths by hand each time we change environments and I don’t like the idea of keeping our configuration files out of version control and placing them manually in each environment. There has to be a better way.
The Database
If you open your /system/expressionengine/config/database.php file, you can guess what we’re going to be doing here. We need to tell the server to change the database configuration for each of our servers. I’ve accomplished this by adding a conditional that changes the active database group based on the server’s IP address:
if ($_SERVER['SERVER_ADDR'] == '10.1.10.11') {
$active_group = 'staging';
} elseif ($_SERVER['SERVER_ADDR'] == '184.254.63.2') {
$active_group = 'production';
} else {
$active_group = 'development';
}
Then, you would just duplicate this code for each of your configuration groups:
$db['production']['hostname'] = "localhost";
$db['production']['username'] = "xxx";
$db['production']['password'] = "xxx";
$db['production']['database'] = "xxx_ee";
$db['production']['dbdriver'] = "mysql";
$db['production']['dbprefix'] = "exp_";
$db['production']['pconnect'] = FALSE;
$db['production']['swap_pre'] = "exp_";
$db['production']['db_debug'] = FALSE; # False for production
$db['production']['cache_on'] = FALSE; # True for production
$db['production']['autoinit'] = FALSE;
$db['production']['char_set'] = "utf8";
$db['production']['dbcollat'] = "utf8_general_ci";
$db['production']['cachedir'] = "{$_SERVER['DOCUMENT_ROOT']}/admin/expressionengine/cache/db_cache/";
Notice the last line, it changes the cache directory based on the server’s DOCUMENT ROOT environmental variable. This will help us avoid path issues in the separate environments.
The Config File
The next step is to open your /system/expressionengine/config/config.php file. Here we’re going to make use of some really nice configuration overrides to make our ExpressionEngine paths more flexible. Add the following lines just below the $config[‘cookie_prefix’] line:
// Config Overrides $config['tmpl_file_basepath'] = $_SERVER['DOCUMENT_ROOT'] . '/templates/'; # Path to your templates as files directory $config['theme_folder_url'] = '/themes/'; # URL to your themes directory $config['theme_folder_path'] = $_SERVER['DOCUMENT_ROOT'] . '/themes/'; # Path to your themes directory $config['captcha_path'] = $_SERVER['DOCUMENT_ROOT'] . '/images/captchas/'; # Parth to your captchas directory $config['captcha_url'] = '/images/captchas/'; # URL to your captchas directory $config['allow_extensions'] = "y"; # Allow extensions
What I’ve done here is told EE the locations of all my important directories in a way that will change as needed depending on which environment the web site is in. No more needing to change these paths when you move the site to production. Note: These variables will set the paths shown in the EE control panel. There is no need to make any changes to what is shown inside of EE as they will be overridden. For a list of all the possible config file overrides available to you here, see this wiki article.
File Upload Locations
These have always been a thorn in my side. The trick here is to use relative server paths for each of your configuration directories. Your paths should look something like this (assuming your system directory is in your web root. If it’s not, you will have to modify the server path slightly):

In case the image above is hard to read, here are the values:
Server Path: ../content/uploads/pdf_downloads/
URL: /content/uploads/pdf_downloads/
Here is why this works: The server path is only used in the EE control panel for uploading/resizing/etc. Therefore, you can give a server path that is relative to your control panel’s index.php file. The good news here is that if your hosting environment changes like it normally would between local development and staging/production, the path will not be affected because your file structure is likely not to change. For the URL path, I always recommend using a web-root relative path (begins with a /).
As a matter of fact, I’ve found that anytime you’re asked to provide a server path to EE, you can use a path relative to the control panel index.php file. If you find an instance where that doesn’t work, please leave a comment to let me know.
Using the methods above, I’ve been able to achieve a completely hands-off push to staging/production from my local MAMP install or the staging environment. Not having to mess with paths or database credentials saves me considerable time. Do you know if a way to make the EE configuration more dynamic? If so, leave a comment!
Permalink 6 notes