Using WordPress in multiple environments

figure1

When developing software, it’s important to have different environments so that modifications made by developers don’t affect others outside their own “sandbox” or the “production” server. Best practices generally dictate three tiers: a local development environment for each developer, a staging environment to integrate changes from multiple developers, and a production environment.

WordPress is one of the best blogging platforms available, but it isn’t really designed to be used in multiple environments. It isn’t too difficult to setup multiple environments, though. Configuration variables can be read in from environment variables that tell WordPress which environment it is in.

With Apache, add the environment variables to your .htaccess file:

SetEnv WP_DB your_database_name
SetEnv WP_USERNAME your_username
SetEnv WP_PASSWORD your_password

Then you can grab these settings in the WordPress configuration file, wp-config.php:

// The name of the database for WordPress
define('DB_NAME', $_ENV['WP_DB']);
// MySQL database username
define('DB_USER', $_ENV['WP_USERNAME']);
// MySQL database password
define('DB_PASSWORD', $_ENV['WP_PASSWORD']);
// For developers: WordPress debugging
define('WP_DEBUG', $_ENV['WP_DEBUG']);
// Override the wp_options and set the site
define('WP_SITEURL', 'http://' . $_SERVER['SERVER_NAME']);
define('WP_HOME', 'http://' . $_SERVER['SERVER_NAME']);

You can also set these environment variables using nginx:

include fastcgi_params;
fastcgi_param WP_DB your_database;
fastcgi_param WP_USERNAME your_username;
fastcgi_param WP_PASSWORD your_password;

You probably also need to tell php to load your environment variables in php.ini:
variables_order = "EGPCS"

Now you can have multiple environments set up to use with your version control without any additional direct configuration of WordPress.

Leave a Reply

Your email address will not be published. Required fields are marked *