6

I'm trying to push a site live today and am running into a problem I'm having trouble solving. I have a staging site which is a subdomain on the live server and everything works fine there but when I try to load the live site I get the following error (strange characters and all):

An exception has been thrown during the rendering of a template ("Craft can't connect to the database with the credentials in craft/config/db.php.") in "_includes/_main_nav" at line 19.

The only difference between the two config files is the database name in db.php and the siteUrl and fileSystemPath in general.php. I do have a multi-environment config based on the one at straughtupcraft.com but I don't see any other difference between my live and staging environments other then what is already noted above.

Anyone run across this problem yet? Solutions?

UPDATE:

index.php:

<?php

//die('hey');


// Path to your craft/ folder
$craftPath = '../craft';

// Do not edit below this line
$path = rtrim($craftPath, '/').'/app/index.php';

if (!is_file($path))
{
    exit('Could not find your craft/ folder. Please ensure that <strong><code>$craftPath</code></strong> is set correctly in '.__FILE__);
}

require_once $path;


// Check the SERVER_NAME variable ourselves
switch ($_SERVER['SERVER_NAME']) 
{
    // If the SERVER_NAME variable matches our case, 
    // assign the CRAFT_ENVIRONMENT variable a keyword 
    // that identifies this environment that we can 
    // use in our multi-environment config

    case 'site.com' :
        define('CRAFT_ENVIRONMENT', 'live');
        break;

    case 'staging.site.com' :
        define('CRAFT_ENVIRONMENT', 'staging');
        break;

    default :
        define('CRAFT_ENVIRONMENT', 'dev');
        break;
}

general.php

return array(
    '*' => array(
        'cpTrigger' => 'site_admin',
        'omitScriptNameInUrls' => true,
        'slugWordSeparator' => "_",
    ),
    'live' => array(
        'environmentVariables' => array(
            'siteUrl' => 'http://site.com',
            'fileSystemPath' => '/path/to/domains/site.com/html'
        )
    ),
    'staging' => array(
        'devMode' => true,
        'environmentVariables' => array(
            'siteUrl' => 'http://staging.site.com',
            'fileSystemPath' => '/path/to/domains/staging.site.com/html'
        )
    ),
    'dev' => array(
        'devMode' => true,
        'environmentVariables' => array(
            'siteUrl' => 'http://site.dev',
            'fileSystemPath' => '/users/stubear/sites/site/dev/html'
        )
    ),
);

db.php

return array(
    '*' => array(
        'tablePrefix' => 'craft',
    ),
    'live' => array(
        'server' => 'server',
        'user' => 'username',
        'password' => '********',
        'database' => 'craft_live_db',
    ),
    'staging' => array(
        'server' => 'server',
        'user' => 'username',
        'password' => '********',
        'database' => 'craft_staging_db',
    ),
    'dev' => array(
        'server' => 'localhost',
        'user' => 'root',
        'password' => 'root',
        'database' => 'site-dev',
    ),
);
  • Can you post your db and config file (with masked passwords)? – Victor In Jun 16 '14 at 16:05
  • Updated question to add requested files. I've removed all the secure information from them and scrubbed the domain name as well. – Stuart McCoy Jun 16 '14 at 16:25
  • 1
    I can't see anything wrong with the config. Can you check the db-connection and also add an echo to the switch, to be sure the server-name gets matched correctly? – Victor In Jun 16 '14 at 16:29
  • Can you enable devMode, then reproduce the error then check your craft/storage/runtime/logs files for an underlying error? – Brad Bell Jun 16 '14 at 16:41
  • Brad, it appears to be throwing a 404 error per this line in the log file (path edited): 2014/06/16 16:48:28 [error] [exception.Craft\HttpException.404] exception 'Craft\HttpException' in /path/to/domains/site.com/craft/app/etc/web/WebApp.php:751 – Stuart McCoy Jun 16 '14 at 16:53
10

Move the check the SERVER_NAME switch up above the require_once $path;. You need to set CRAFT_ENVIRONMENT earlier.

  • That did it. Why would the staging and dev sites work though and not the live site? – Stuart McCoy Jun 16 '14 at 17:23
  • 1
    If you had staging as part of the staging url, it would match (same with dev). I'm guessing there is no live in the live site url. – Marion Newlevant Jun 16 '14 at 17:26
  • 1
    Not sure why I placed that code at the bottom of the index page on this site because two other sites I'm working on currently have it where it should go, above the //do not edit below this line comment, but there you have it. Problem solved. Craft SE to the rescue. – Stuart McCoy Jun 16 '14 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.