Setting Up WordPress Environments Like A Boss

In this tutorial we are going to learn:

  • How to create multiple environments?

Let's create a directory first wpboss then add two sub directories config and public

Now download WordPress and extract it in public folder.

Let's create couple of more directories and files. The final directory structure will look like this

We also need to create index.php and wp-config.php in public folder.

We are just going to include wordpress.php from config folder in wp-config.php


require dirname(__DIR__) . '/config/wordpress.php';  

and in index.php we are going to load WP by adding wp-blog-header.php

 * Front to the WordPress application. This file doesn't do anything, but loads
 * wp-blog-header.php which does and tells WordPress to load the theme.
 * @package WordPress

 * Tells WordPress to load the WordPress theme and output it.
 * @var bool
define('WP_USE_THEMES', true);

/** Loads the WordPress Environment and Template */
require dirname(__FILE__) . '/wordpress/wp-blog-header.php';  

Now let's talk about config/wordpress.php file. In this file we are going to load configuration for different environments. We are going to use PHP's getenv function to determine which config to load.


 * The base configurations of the WordPress.
 * This file has the following configurations: MySQL settings, Table Prefix,
 * Secret Keys, WordPress Language, and ABSPATH. You can find more information
 * by visiting {@link Editing
 * wp-config.php} Codex page. You can get the MySQL settings from your web host.
 * This file is used by the wp-config.php creation script during the
 * installation. You don't have to use the web site, you can just copy this file
 * to "wp-config.php" and fill in the values.
 * @package WordPress

define('APP_ROOT', dirname(__DIR__));  

if (file_exists(APP_ROOT . '/config/env/' . APP_ENV . '.php')) {  
    require APP_ROOT . '/config/env/' . APP_ENV . '.php';
} else {
    require APP_ROOT . '/config/env/server.php';

 * WordPress Localized Language, defaults to English.
 * Change this to localize WordPress. A corresponding MO file for the chosen
 * language must be installed to wp-content/languages. For example, install
 * to wp-content/languages and set WPLANG to 'de_DE' to enable German
 * language support.
define('WPLANG', '');

/* That's all, stop editing! Happy blogging. */

/** Absolute path to the WordPress directory. */
if (!defined('ABSPATH')) {  
    define('ABSPATH', APP_ROOT . '/public/wordpress/');

/** Sets up WordPress vars and included files. */
require_once ABSPATH . 'wp-settings.php';  

We can set environment for apache in .htaccess file and for nginx we can set fastcgi_param which will be pass on to PHP.

For apache paste the following code in .htaccess file


For nginx paste the following code in config file

fastcgi_param APPLICATION_ENVIRONMENT staging;  

Our wordpress.php file get the environment by using PHP getenv function and will select the environment file from env folder accordingly.

Hers is the example of staging config file


define('WP_HOME', '');  
define('WP_SITEURL', WP_HOME . '/wordpress/');

define('WP_CONTENT_DIR', APP_ROOT . '/public/content');  
define('WP_CONTENT_URL', WP_HOME . '/content');

define('WP_DEBUG', true);  
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'staging');

/** MySQL database username */
define('DB_USER', 'admin');

/** MySQL database password */
define('DB_PASSWORD', 'password');

/** MySQL hostname */
define('DB_HOST', 'mysql');  
/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');

/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');

 * WordPress Database Table prefix.
 * You can have multiple installations in one database if you give each a unique
 * prefix. Only numbers, letters, and underscores please!
$table_prefix = 'wp_';
 * Authentication Unique Keys and Salts.
 * Change these to different unique phrases!
 * You can generate these using the {@link secret-key service}
 * You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.
 * @since 2.6.0
define('AUTH_KEY', '');  
define('SECURE_AUTH_KEY', '');  
define('LOGGED_IN_KEY', '');  
define('NONCE_KEY', '');  
define('AUTH_SALT', '');  
define('SECURE_AUTH_SALT', '');  
define('LOGGED_IN_SALT', '');  
define('NONCE_SALT', '');


Now you have fully working WordPress with multiple environments.


comments powered by Disqus