Easy multi-server WordPress configuration

comment 0

In a professional environment to create a site for a customer, you usually need to work on three different servers:

  • The development server: where you develop the site
  • The test server: where you show the site to your customer, very useful if the customer is not located in the town where you live
  • The production server: where the “real” site is hosted and everyone can see it

In such environment its easy to replace the wp-config.php file with the wrong credentials – specially when you already have a number of other tasks in mind…

Personally I found very useful to use a multi-server configuration where the wp-config.php file is “clever” enough to know which credentials to use pending on where it “resides”.

switch( $_SERVER['SERVER_NAME'] ) {

  case 'example.lan':
     * development
    define('DB_NAME', 'DEVdatabasename');
    define('DB_USER', 'DEVusername');
    define('DB_PASSWORD', 'DEVpassword');
    define('DB_HOST', 'localhost');
    $table_prefix = 'wp_';

  case 'test.example.com:
     * test
    define('DB_NAME', 'TESTdatabasename');
    define('DB_USER', 'TESTusername');
    define('DB_PASSWORD', 'TESTpassword');
    define('DB_HOST', 'localhost');
    $table_prefix = '0TLtqkZ3jzV1b_';

  case 'www.example.com:
  case 'example.com:
     * production
    define('DB_NAME', 'PRODUCTIONdatabasename');
    define('DB_USER', 'PRODUCTIONusername');
    define('DB_PASSWORD', 'PRODUCTIONpassword');
    define('DB_HOST', 'localhost');
    $table_prefix = 'qkbZ1tTVL0z3j_';

    die('Houston, we have a problem!');

If you get the “Houston” message be sure you spelled the server name correctly and that the $_SERVER['SERVER_NAME'] variable is properly rendered!

Using a similar technique will let you copy your wp-config.php between your servers without worrying to edit it to make the WordPress installation working on each server.

If you like you can download the code using the link in the sidebar.

Leave a Reply