1

My app/config/database.php has been setup like this:

'mysql' => array(
    'driver'    => 'mysql',
    'host'      => getenv('DB_HOST'),
    'database'  => getenv('DB_NAME'),
    'username'  => getenv('DB_USER'),
    'password'  => getenv('DB_PASS'),
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
),

I have updated my env detection code like this in bootstrap/start.php:

$env = $app->detectEnvironment(function() {
    return file_exists(dirname(__FILE__) . '/../.env.production.php')
        ?
        'production'
        :
        'local';
});

Now, I have uploaded .env.production.php with the following:

<?php

return
[
    'DB_HOST' => '178.xxx.xxx.xxx',
    'DB_NAME' => 'USERNAME',
    'DB_USER' => 'PASSWORD',
    'DB_PASS' => 'DBNAME',
];

I tested the environment detection like this:

[www-data@server]$ php artisan env
Current application environment: production

As you can see, it's working as intended. I then proceeded to run the migration like this:

php artisan migration:install

When I do, I get the following error:

[www-data@server]$ php artisan migrate:install --env=production

  [PDOException]                                    
  SQLSTATE[HY000] [2002] No such file or directory  

migrate:install [--database[="..."]]

Any idea why this might be? it looks like it's failing to load the .env.production.php file.

Does anyone know how to fix this error? Thanks in advance.

Latheesan
  • 23,247
  • 32
  • 107
  • 201
  • That sounds like an inability to connect to the MySQL server, rather than with loading the environment file. Are you certain that the host is correct? 178... looks like an external ip, isn't the database server on your own machine? – Joel Hinz Feb 20 '15 at 18:06
  • I have a split server server setup. One machine for web/php and another for mysql only. Perhaps I forgot to give the db remote access. Let me check... – Latheesan Feb 20 '15 at 18:07
  • If the problem persists, I'd suggest replacing the getenv() calls in the database configuration file - not permanently, just to confirm whether the server connection is indeed the problem. – Joel Hinz Feb 20 '15 at 18:09
  • I just checked, the database is setup correctly. Okay, i will try that. – Latheesan Feb 20 '15 at 18:09
  • I removed getenv and typed the access details directly and it worked. So, it looks like the env file is NOT getting loaded as I originally suspected. – Latheesan Feb 20 '15 at 18:11
  • What's weird is, i have a local environment setup on my dev machine and exact same setup works with *.env.local.php* - just not on the production server. – Latheesan Feb 20 '15 at 18:13
  • Right... that's really weird, then. Sorry for sidetracking you, hope you are able to find the error eventually. – Joel Hinz Feb 20 '15 at 18:39
  • 2
    Actually, I think I may have figured it out. Laravel 4 defaults to naming the environment production, so the file should be just .env.php, not .env.production.php. Worth a try at least. :) – Joel Hinz Feb 20 '15 at 18:41

1 Answers1

1

In the Laravel 4 docs it states that it needs to be .env.php on your production server.

Now, on your production server, create a .env.php file in your project root that contains the corresponding values for your production environment. Like the .env.local.php file, the production .env.php file should never be included in source control.

So simply change your environment detection code to this

$env = $app->detectEnvironment(function() {
    return file_exists(dirname(__FILE__) . '/../.env.php')
        ?
        'production'
        :
        'local';
});

Note - you still use .env.local.php on your development server.

Laurence
  • 58,936
  • 21
  • 171
  • 212