문제

For some reason, my production laravel app thinks that it is in the local environment.

/var/www/appname/.env.php

<?php

return 
[
    'APP_ENV'   =>  'production',
    'DB_HOST'   =>  'HIDDEN',
    'DB_NAME'   =>  'HIDDEN',
    'DB_PASSWORD'   =>  'HIDDEN'
];

/var/www/appname/bootstrap/start.php

$env = $app->detectEnvironment(function()
{
    return getenv('APP_ENV') ?: 'local';
});

/var/www/appname/app/config/database.php

...
...
'mysql' => array(
        'driver'    => 'mysql',
        'host'      => getenv('DB_HOST'),
        'database'  => getenv('DB_NAME'),
        'username'  => getenv('DB_USERNAME'),
        'password'  => getenv('DB_PASSWORD'),
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => 'lar_',
    ),
...
...

sudo php artisan env (via SSH)

`Current application environment: local

php artisan tinker then getenv('DB_NAME')

$ php artisan tinker
[1] > getenv('DB_NAME');
// false

So either my environment variables are not being set, or Laravel is not recognising my .env.php file for the production environment.

Update

With some help from Anultro on IRC, it appears that .env.php is not loaded yet. As such APP_ENV must be set before laravel tries to detect environments. This makes sense, because Laravel needs to know what environment is running before determining whether to use .env.php or .env.local.php.

Having said this, .env.php should still be used to store db credentials and secret keys etc... But I am still having a problem because the app is still returning false when I try to run getenv('DB_NAME')

Any suggestions?

도움이 되었습니까?

해결책

For all who want to know... to solve this issue, I just edited my httpd.conf file on the production server as follows:

SetEnv APP_ENV production

Now laravel knows that the app is in production.

If you are using nginx which I have now migrated my site to add the following where you pass scripts to the FCGI server in the active sites-available /etc/nginx/sites-available/{sitename}:

fastcgi_param APP_ENV production;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top