Laravel 4.1 部署 - 无法识别生产 .env.php

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22126709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:07:18  来源:igfitidea点击:

Laravel 4.1 Deployment - Production .env.php not being recognised

laravellaravel-4environment-variablesproduction-environment

提问by Gravy

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

出于某种原因,我的生产 laravel 应用程序认为它在本地环境中。

/var/www/appname/.env.php

/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

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

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

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

/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)

sudo php artisan env(通过 SSH)

`Current application environment: local

php artisan tinkerthen getenv('DB_NAME')

php artisan tinker然后 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.phpfile for the production environment.

所以要么我的环境变量没有设置,要么 Laravel 没有识别我.env.php的生产环境文件。

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.phpor .env.local.php.

在 IRC 上 Anultro 的帮助下,似乎 .env.php 尚未加载。因此,必须在 Laravel 尝试检测环境之前设置 APP_ENV。这是有道理的,因为 Laravel 在决定是否使用.env.php或之前需要知道正在运行的环境.env.local.php

Having said this, .env.phpshould 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')

话虽如此,.env.php仍应用于存储数据库凭据和密钥等......但我仍然遇到问题,因为当我尝试运行时,该应用程序仍然返回 falsegetenv('DB_NAME')

Any suggestions?

有什么建议?

采纳答案by Gravy

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

对于所有想知道的人......为了解决这个问题,我刚刚在生产服务器上编辑了我的 httpd.conf 文件,如下所示:

SetEnv APP_ENV production

Now laravel knows that the app is in production.

现在 laravel 知道该应用程序正在生产中。

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}:

如果您使用的是 nginx,我现在已经迁移了我的站点以添加以下内容,在其中将脚本传递到活动站点可用的 FCGI 服务器/etc/nginx/sites-available/{sitename}

fastcgi_param APP_ENV production;