Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 811
Gravy Avatar asked Mar 02 '14 10:03

Gravy


People also ask

How to configure Laravel to automatically detect the environment?

Just specify a machine name for the host that matches a given environment, then laravel will automatically detect the environment (default is production ), for example:

How do I build a Laravel application on azure?

You can use Azure Pipelines to build your Laravel application. Here is an example in how to implement Azure Pipelines with App Service Linux. Create a new DevOps project then go to Pipelinesand select Create Pipeline. Select your code repository. Select PHP as Linux Web App on Azuretemplate. Select the web app where you will deploy.

What is the default web server for Laravel in Linux?

PHP 7.x on Azure App Service Linux use Apache as the Web Server. Since Laravel uses /publicas the site root we need to use an .htaccessto rewrite these requests accordingly. Create an .htaccessin the root of your repo with the following:

Does Piping yes cancel the yes command in Laravel?

I can confirm once again that piping yes cancels the command, although there's also an -n flag (which stands for 'no interaction') and using that prevents Artisan from asking for confirmation. Also, I'm using Laravel 5+ for this particular application.


1 Answers

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;
like image 167
Gravy Avatar answered Oct 03 '22 15:10

Gravy