Skip to content

Instantly share code, notes, and snippets.

@leevigraham
Last active March 7, 2025 16:49
Show Gist options
  • Select an option

  • Save leevigraham/448166 to your computer and use it in GitHub Desktop.

Select an option

Save leevigraham/448166 to your computer and use it in GitHub Desktop.
<?php
/**
* Custom configuration bootsrtap file for ExpressionEngine
*
* Place config.php in your site root
* Add require(realpath(dirname(__FILE__) . '/../../config.php')); to the bottom of system/expressionengine/config/config.php
* Add require(realpath(dirname(__FILE__) . '/../../config.php')); to the bottom of system/expressionengine/config/database.php
* If you have moved your site root you'll need to update the require_once path
*
* Also includes custom DB configuration file based on your environment
*
* Posiible DB configuration options
*
* $env_db_config['hostname'] = "";
* $env_db_config['username'] = "";
* $env_db_config['password'] = "";
* $env_db_config['database'] = "";
*
* @author Leevi Graham <http://leevigraham.com>
* @link http://expressionengine.com/index.php?affiliate=leevigraham&page=wiki/EE_2_Config_Overrides/
* @link http://eeinsider.com/blog/eeci-2010-how-erskine-rolls-with-ee/ - Hat tip to: Erskine from EECI2010 Preso
* @version 1.1
*
* == Changelog ==
*
* Version 1.1
* - Changed 'gv_' to 'global:'
* - Added {global:cm_subscriber_list_slug} for campaignmonitor.com integration
* - Added {global:google_analytics_key} for Google Analytics integration
* - Make $_POST array available as global vars with 'post:' prefix
* - Make $_GET array available as global vars with 'get:' prefix
* - Added more inline commenting
* - Swapped order of system config and global vars
*
* Version 1.2
* - Removed $_GET and $_POST parsing. You should use Mo Variables instead. https://github.com/rsanchez/mo_variables
*/
// Setup the environment
if(!defined('NSM_ENV')) {
define('NSM_SERVER_NAME', $_SERVER['SERVER_NAME']);
define('NSM_SITE_URL', 'http://'.NSM_SERVER_NAME);
define('NSM_BASEPATH', dirname(__FILE__));
define('NSM_SYSTEM_FOLDER', 'ee-admin');
// Set the environment
if ( strstr( NSM_SERVER_NAME, 'local.' ) ) define('NSM_ENV', 'local');
elseif( strstr( NSM_SERVER_NAME, 'dev.' ) ) define('NSM_ENV', 'development');
elseif( strstr( NSM_SERVER_NAME, 'stage.' ) ) define('NSM_ENV', 'staging');
elseif( strstr( NSM_SERVER_NAME, 'm.' ) ) define('NSM_ENV', 'mobile');
else define('NSM_ENV', 'production');
}
// Define the environment settings
$env_config = array();
$env_db_config = array();
$env_global_vars = array();
// Set the environmental config and global vars
if (NSM_ENV == 'local') {
$env_db_config = array(
'hostname' => '',
'username' => '',
'password' => '',
'database' => '',
);
$env_global_vars = array(
'global:cm_subscriber_list_slug' => ''
);
}
elseif(NSM_ENV == 'development') {
$env_db_config = array(
'hostname' => '',
'username' => '',
'password' => '',
'database' => '',
);
$env_global_vars = array();
}
elseif(NSM_ENV == 'staging') {
$env_db_config = array(
'hostname' => '',
'database' => '',
'username' => '',
'password' => '',
);
$env_global_vars = array(
'global:cm_subscriber_list_slug' => ''
);
}
elseif(NSM_ENV == 'mobile') {
$env_db_config = array(
'hostname' => '',
'username' => '',
'password' => '',
'database' => '',
);
$env_config['tmpl_file_basepath'] = NSM_BASEPATH . '/templates_mobile/';
}
else {
$env_global_vars = array(
'global:cm_subscriber_list_slug' => '',
'global:google_analytics_key' => 'XX-XXXX'
);
}
// Config bootsrap... GO!
if(isset($config)) {
/**
* Custom global variables
*
* This is a bit sucky as they are pulled straight from the $assign_to_config array.
* See EE_Config.php around line 90 or search for: 'global $assign_to_config;'
* Output the global vars in your template with:
* <?php $EE = get_instance(); print('<pre><code>'.print_r($EE->config->_global_vars, TRUE) . '</code></pre>'); ?>
*/
$default_global_vars = array(
// General - Set the production environment so we can test / show / hide components
'global:env' => NSM_ENV,
// Tag parameters - Short hand tag params
'global:param_disable_default' => 'disable="categories|pagination|member_data"',
'global:param_disable_all' => 'disable="categories|custom_fields|member_data|pagination"',
'global:param_cache_param' => 'cache="yes" refresh="10"',
'-global:param_cache_param' => '-cache="yes" refresh="10"', // disable by adding a '-' to the front of the global
// Date and time - Short hand date and time
'global:date_time' => '%g:%i %a',
'global:date_short' => '%F %d, %Y',
'global:date_full' => '%F %d %Y, %g:%i %a',
/**
* Theme - URL to theme assets
* Example: <script src="{global:theme_url}/js/libs/modernizr-1.6.min.js"></script>
*/
'global:theme_url' => NSM_SITE_URL . '/themes/site_themes/default',
/**
* CampaignMonitor - Slug for CM signup forms
* Example: <form action="http://newism.createsend.com/t/y/s/{global:cm_subscriber_list_slug}/" method="post">...</form>
*/
'global:cm_subscriber_list_slug' => false,
/**
* Google Analytics Key
* Example:
* <script type="text/javascript">
* var _gaq = _gaq || [];
* _gaq.push(['_setAccount', 'UA-{global:google_analytics_key}']);
* _gaq.push(['_trackPageview']);
* </script>
*/
'global:google_analytics_key' => false,
// NSM Gravatar
'global:nsm_gravatar_default_avatar' => NSM_SITE_URL . '/uploads/member/avatars/default.png'
);
// Make this global so we can add some of the config variables here
global $assign_to_config;
if(!isset($assign_to_config['global_vars']))
$assign_to_config['global_vars'] = array();
$assign_to_config['global_vars'] = array_merge($assign_to_config['global_vars'], $default_global_vars, $env_global_vars);
/**
* Config. This shouldn't have to be changed if you're using the Newism EE2 template.
*/
$default_config = array(
// General preferences
'is_system_on' => 'y',
'license_number' => '',
'site_index' => '',
'admin_session_type' => 'cs',
'new_version_check' => 'y',
'doc_url' => 'http://expressionengine.com/user_guide/',
'site_url' => NSM_SITE_URL,
'cp_url' => NSM_SITE_URL.'/'.NSM_SYSTEM_FOLDER.'/index.php',
// Set this so we can use query strings
'uri_protocol' => 'PATH_INFO',
// Datbase preferences
'db_debug' => 'n',
'pconnect' => 'n',
'enable_db_caching' => 'n',
// Site preferences
// Some of these preferences might actually need to be set in the index.php files.
// Not sure which ones yet, I'll figure that out when I have my first MSM site.
'is_site_on' => 'y',
'site_404' => 'site/four04',
'webmaster_email' => 'webmaster@' . NSM_SERVER_NAME,
'webmaster_name' => 'Webmaster',
// Localization preferences
'server_timezone' => 'UP10',
'server_offset' => FALSE,
'time_format' => 'eu',
'daylight_savings' => 'n',
'honor_entry_dst' => 'y',
// Channel preferences
'use_category_name' => 'y',
'word_separator' => 'dash',
'reserved_category_word' => 'category',
// Template preferences
'strict_urls' => 'y',
'save_tmpl_files' => 'y',
'save_tmpl_revisions' => 'y',
'tmpl_file_basepath' => NSM_BASEPATH . '/templates/',
// Theme preferences
'theme_folder_path' => NSM_BASEPATH . '/content/themes/',
'theme_folder_url' => NSM_SITE_URL . '/themes/',
// Tracking preferences
'enable_online_user_tracking' => 'n',
'dynamic_tracking_disabling' => '500',
'enable_hit_tracking' => 'n',
'enable_entry_view_tracking' => 'n',
'log_referrers' => 'n',
// Member preferences
'allow_registration' => 'n',
'profile_trigger' => '--sdjhkj2lffgrerfvmdkndkfisolmfmsd' . time(),
'prv_msg_upload_path' => NSM_BASEPATH . '/content/uploads/member/pm_attachments',
'enable_emoticons' => 'n',
'enable_avatars' => 'n',
'avatar_path' => NSM_BASEPATH . '/content/uploads/member/avatars/',
'avatar_url' => NSM_SITE_URL . '/uploads/member/avatars/',
'avatar_max_height' => 100,
'avatar_max_width' => 100,
'avatar_max_kb' => 100,
'enable_photos' => 'n',
'photo_path' => NSM_BASEPATH . '/content/uploads/member/photos/',
'photo_url' => NSM_SITE_URL . '/uploads/member/photos/',
'photo_max_height' => 200,
'photo_max_width' => 200,
'photo_max_kb' => 200,
'sig_allow_img_upload' => 'n',
'sig_img_path' => NSM_BASEPATH . '/content/uploads/member/signature_attachments/',
'sig_img_url' => NSM_SITE_URL . '/uploads/member/signature_attachments/',
'sig_img_max_height' => 80,
'sig_img_max_width' => 480,
'sig_img_max_kb' => 30,
'sig_maxlength' => 500,
'captcha_font' => 'y',
'captcha_rand' => 'y',
'captcha_require_members' => 'n',
'captcha_path' => NSM_BASEPATH . '/content/'.NSM_SYSTEM_FOLDER.'/images/captchas/',
'captcha_url' => NSM_SITE_URL.'/'.NSM_SYSTEM_FOLDER.'/images/captchas/',
// NSM htaccess Generator
'nsm_htaccess_generator_path' => NSM_BASEPATH . "/content/.htaccess",
);
// Build the new config object
$config = array_merge($config, $default_config, $env_config);
}
// DB bootsrap... GO!
if(isset($db['expressionengine']))
{
$default_db_config = array("cachedir" => APPPATH . "cache/db_cache/");
$db['expressionengine'] = array_merge($db['expressionengine'], $default_db_config, $env_db_config);
}
@leevigraham

Copy link
Copy Markdown
Author

There's still some issues with setting a couple of the config vars via this method. You may need to use the $assign_to_config variable in the index.php files. See this forum post for more info

@leevigraham

Copy link
Copy Markdown
Author

Revision https://gist.github.com/448166/2e02b8af83f44c3c9c3604b1088be99c921e5ff1 adds some global variables to the config.php file

@leevigraham

Copy link
Copy Markdown
Author

Revision: http://gist.github.com/448166/5e7320b79a3b6dd932adb34ab4e8561d2d594fc4 removed config_db.php and merged the db prefs into the main file.

@leevigraham

Copy link
Copy Markdown
Author

Revision: https://gist.github.com/448166/ed62b01ab30cbe95802f1c9814b058de920feaaa (version 1.1)

  • Changed 'gv_' to 'global:'
  • Added {global:cm_subscriber_list_slug} for campaignmonitor.com integration
  • Added {global:google_analytics_key} for Google Analytics integration
  • Make $_POST array available as global vars with 'post:' prefix
  • Make $_GET array available as global vars with 'get:' prefix
  • Added more inline commenting
  • Swapped order of system config and global vars

@leevigraham

Copy link
Copy Markdown
Author

Revision: https://gist.github.com/448166/93d66d2d71bd22db59103a921b1dfe21ef630df9 (version 1.2)

@leevigraham

Copy link
Copy Markdown
Author

@leevigraham

Copy link
Copy Markdown
Author

Revision https://gist.github.com/448166/0cf9ed08dcb716a3534919eeb41f99c22d7a6c5d (1.4)

  • Updated NSM .htaccess path. v1.1.0 of the addon requires the config setting to be an array
    changed

@johndwells

Copy link
Copy Markdown

Hi Leevi,

Here's my MSM experience: I've resorted to commenting out site_url and cp_url from the bootstrap file, since in a typical MSM setup, those settings are set immediately within each site's index.php file. If you open up the index.php file and go to line 75, comments for MSM setup ask you to set site_url, site_name, and cp_url there. I keep these set here, and comment them out in bootstrap, and that seems to keep things working smoothly.

Perhaps you could keep the site_name setting in each MSM site's index.php file, and then within bootstrap set the site_url based on the environment + site_name value... But between subdomain vs subfolder vs distinct domain variations, plus the possibility for tens to hundreds of MSM sites, it might just become more hassle than worth.

Cheers
John

@TechRemarker

Copy link
Copy Markdown

@Leevi

I notice you recommend placing your config_bootstrap.php in the html public directory. I was wondering why that would be a more secure solution than adding it above the public html directory in the expressionengine/config directory with the other config.php and database.php file?

Was hoping you could shed some light. Thank you!

@farneman

farneman commented Jun 9, 2011

Copy link
Copy Markdown

@johndwells

I'm working on setting up the boostrap for an MSM system. My setup is managing multiple domains. Where do you keep your bootstrap file?

@johndwells

Copy link
Copy Markdown

I'm working with subdomains myself but shouldn't matter:

/ee_system/
/admin_html/ (EE admin access)
/public_html/ (default_site)
/public_html/site_1/
/public_html/site_2/
/public_html/site_3/
bootstrap.php

HTH,
John

@matalin

matalin commented Jan 17, 2012

Copy link
Copy Markdown

I've installed a number of your addons including a htaccess generator. This sounded awesome but when I added it, it gives me the error: The configuration file does not exist.

This is on my local mac using MAMP. Any idea why it wouldn't find the config file?

@matalin

matalin commented Jan 17, 2012

Copy link
Copy Markdown

in your code you say "Place config.php in your site root" what file is this? if it's not the system/config/ config file and it's not the config_bootstrap, could you elaborate on what it is? is it to do with http://expressionengine.com/wiki/EE_2_Config_Overrides/ ? thanks!

@leevigraham

Copy link
Copy Markdown
Author

@matalin: I've updated the docs:

* Add require(realpath(dirname(__FILE__) . '/../../config_bootstrap.php')); to the bottom of system/expressionengine/config/config.php
* Add require(realpath(dirname(__FILE__) . '/../../config_bootstrap.php')); to the bottom of system/expressionengine/config/database.php

which should make the instructions clearer

@andyhoman

Copy link
Copy Markdown

Just a quick heads up Leevi. I had to change the file upload code to:

'upload_preferences' => array(
    1 => array(                                                                                 // ID of upload destination
        'name'        => 'Staging Image Uploads',                          // Display name in control panel
        'server_path' => '/home/user/example.com/staging/images/uploads/', // Server path to upload directory
        'url'         => 'http://staging.example.com/images/uploads/'      // URL of upload directory
    )
),

Otherwise it killed the script.

@wssrstrm

wssrstrm commented Feb 7, 2012

Copy link
Copy Markdown

I'm wondering something pretty basic. Where do you set the db info for the production db if not in this file? Do you just set it normally through the expression engine install on the production server itself and use this file to identify the dbs for the other environments?

@leevigraham

Copy link
Copy Markdown
Author

@andyhoman sorted!

@ginkelb

ginkelb commented Mar 1, 2012

Copy link
Copy Markdown

@wssrstrm The db info for the production environment are entered near line 123 of the script. It woudn't be very handy if you ended up with configuration data in multiple files.

@leevigraham The NSM_SITE_URL near line 62 ends up WITH a trailing /. All other variables based off of this like theme_folder_url use a / between NSM_SITE_URL and the rest of the variable. Ending up with url.com//themes/. Is this intentional?

@leevigraham

Copy link
Copy Markdown
Author

@ginkelb I'll update the NSM_SITE_URL now.

@elliotlewis

Copy link
Copy Markdown

I think an important config is missing (excellent bootstrap btw).

http://expressionengine.com/wiki/EE_2_Config_Overrides
[base_url] => http://local.ee2.template/

site_url($uri = '') from ->helper('url') in Config.php as part of CI uses base_url to define paths.

This caught me out when developing a module using site_url() for the 'action' part of a form_declaration. I'm not sure why site_url doesn't use 'site_url' from the config array, maybe it's a hangover from CI and EE uses 'site_url'.

Easy fix, I've included after
'site_url' => NSM_SITE_URL,
'base_url' => NSM_SITE_URL

@leevigraham

Copy link
Copy Markdown
Author
  • Version 1.9
  • - Added $config['third_party'] param to ease updating
    
  • - Added $config['base_url']
    
  • - Moved webmaster_email config into dev environment
    

@alexroper

Copy link
Copy Markdown

missing a comma after:

'base_url' => NSM_SITE_URL

@jakemauer

Copy link
Copy Markdown

@alexroper wish I would've see your comment an hour ago. I was tearing my hair out!

ghost commented Sep 21, 2012

Copy link
Copy Markdown

@alexroper Just found out about the comma

@alexroper

Copy link
Copy Markdown

@leevigraham EE v2.5.3 adds a couple other hidden config variables for third-party themes:

'path_third_themes' => NSM_BASEPATH . '/content/themes/third_party/',
'url_third_themes' => NSM_SITE_URL . '/themes/third_party/',

@alexroper

Copy link
Copy Markdown

@leevigraham Also, the 'third_party' variable should be changed to 'third_party_path':

'third_party_path' => NSM_BASEPATH . '/third_party/',

@leevigraham

Copy link
Copy Markdown
Author

@alexroper took me a while but I've updated based on your comments

@philfreo

Copy link
Copy Markdown

@leevigraham you've got syntax errors now on Line 154 and 177 (double braces)

@susansan

Copy link
Copy Markdown

line 369 should be:

$env_config['upload_preferences'] = array(

@sharq88

sharq88 commented Apr 25, 2014

Copy link
Copy Markdown

@leevigraham Line 7,8 should be:

  • require_once instead of require (see line 9)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment