Yii 1.1: Configuring Yii to run on both Windows and OSX


If you develop your Yii project on both Windows and Mac, there can be a problem setting up the configuration unless the configurations for both system are identical. On my machines I have the following configurations:

Mac: MAMP with the default port settings (8888 for Apache and 8889 for MySQL).

Windows: Apache is configured to use port 80 and MySQL to port 3306.

To handle these differences, I have modified index.php like this:

$yii = dirname(__FILE__) . '/framework/yii.php';
if (substr_compare($_SERVER['HTTP_HOST'], 'localhost', 0, 8) === 0){
    if ($_SERVER['HTTP_HOST'] == 'localhost') {
        $config = dirname(__FILE__) . '/protected/config/development_win.php';
    } else {
        $config = dirname(__FILE__) . '/protected/config/development_osx.php';
    defined('YII_DEBUG') or define('YII_DEBUG', true);
    defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL', 3);
} else {
    $config = dirname(__FILE__) . '/protected/config/production.php';
$app = Yii::createWebApplication($config);

You then need three configuration files – one for the production environment, one for Windows and one for OSX.

That’s it.


To further enhance the configuration files, you can keep the common configuration items in /protected/config/main.php and merge it in from the three config files. Here’s an example:

return CMap::mergeArray(
    require(dirname(__FILE__) . '/main.php'),
        'modules' => array(
            'gii' => array(
                'class' => 'system.gii.GiiModule',
                'password' => false,
                // If removed, Gii defaults to localhost only. Edit carefully to taste.
                'ipFilters' => array('', '::1'),
        'components' => array(
            'db' => array(
                'tablePrefix' => 'tbl_',
                'connectionString' => 'mysql:host=localhost;dbname=mydatabase',
                'emulatePrepare' => true,
                'enableParamLogging' => true,
                'username' => 'me',
                'password' => 'supersecret',
                'charset' => 'utf8',


Total 2 comments

#16435 report it
trond at 2014/02/22 12:03pm

Only if there are differences that need to be addressed. In my case the db login is different.

BTW: feel free to update the article with more info. It is always good to have a choice.

#16431 report it
hemc at 2014/02/22 10:02am
Why separate config?

For Apache port issue create virtual host on your machine. And for db set host to localhost.

Is there any specific reason to do this extra work?

Leave a comment

Please to leave your comment.

Write new article
  • Written by: trond
  • Updated by: CeBe
  • Category: How-tos
  • Yii Version: 1.1
  • Votes: +2
  • Viewed: 4,946 times
  • Created on: Feb 13, 2014
  • Last updated: Apr 14, 2014
  • Tags: config