5

I am using Github to deploy my sites to my production server. Because of this I don't want to be storing .env.*.php files everything so they're in my .gitignore.

Within each environment directory I've set the database config to use getenv(), for example:

<?php

'connections' => array(

    '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'    => '',
    ),


),

);

I've then gone in to the apache conf file for my virtual hosts and set the environment variables, like so:

SetEnv DB_HOST ***.***.***.***
SetEnv DB_NAME database
SetEnv DB_USERNAME databaseuser
SetEnv DB_PASSWORD databasepass

This all works perfectly on the production server.

However, I've just manually imported the database at the moment rather than using php artisan migrate because it doesn't work.

I know it doesn't work because it's not hitting apache so the variables aren't being set, but I've tried numerous ways in order to try and get around this issue but I've have no luck as of yet.

I've tried forcing the environment to see if it can get the variables by using php artisan --env=production migrate

I've also tried checking that it's using the correct environment by running php artisan env and it's using the production environment which is correct.

My next idea was to create a .env.production.php file manually on the production server. I created one and used getenv() just in case it worked like this, but the same error occurred, so I tried setting the variables manually without using getenv() and I've still had no luck.

Any ideas would be greatly appreciated.

Karl
  • 5,435
  • 11
  • 44
  • 70
  • production .env files are called .env.php not .env.production.php – Matt Burrow Aug 29 '14 at 13:17
  • I've set up an environment within bootstrap/start.php with the name 'production', would this not work anyway? – Karl Aug 29 '14 at 13:21
  • I've just tried the .env.php file, which works if I set the variables manually, is there any way I could use the variables which are set through apache to work with artisan? – Karl Aug 29 '14 at 13:23
  • Im not too sure, I have never used apache to set these variables. Much easier using these files and ignoring them within .gitignore. – Matt Burrow Aug 29 '14 at 13:27
  • Okay, thanks for the advice anyway. It's definitely a possibility for me to use the env files, I just though doing everything through apache (webmin) would be easier than having to change the env files. – Karl Aug 29 '14 at 13:29
  • 4
    When using `artisan` you don't have access to anything that comes from apache because artisan runs through the command line, so those environment variables aren't set. – j boschiero Sep 15 '14 at 20:58
  • Can't you set the environment variables in your terminal before running artisan? – Thomas Jensen Sep 26 '14 at 13:56
  • Yes, you can, by using `--env=` but since artisan never hits Apache, it can't get the environment variables stored in the apache conf file. So the only realistic option that I've found is to use the env.php files. – Karl Sep 26 '14 at 15:02

1 Answers1

1

You should use .env.php files as described in the laravel docs.

For setting environment variables in production, you use a file named .env.php in your project root:

<?php

# .env.php

return array(

    'DB_HOST' => 'localhost',
    'DB_NAME' => 'my_database',
    'DB_USER' => 'user_name',
    'DB_PASS' => 'super-secret-sauce',

);

For different environments, you use different .env files. For example, in local environment you would use .env.local.php, and for testing you would use .env.testing.php.

When running artisan commands like migrate or db:seed you can specify the environment by using the --env= option.

For example $ php artisan db:seed --class=UsersTableSeeder --env=testing

ryanwinchester
  • 11,737
  • 5
  • 27
  • 45
  • .env files only work in laravel 5. He is using laravel 4 – Richard Torcato Apr 23 '15 at 13:51
  • @RichardTorcato: `.env.*.php` files are for laravel 4. `.env` files are for laravel 5. The easiest way to tell the difference is that the env files in L4 were php files. Also, the link I pointed to in the docs is Laravel 4 specific. – ryanwinchester Apr 23 '15 at 20:31