1

I am currently deploying a Laravel project on my shared hosting account. It is an open project and hosted on GitHub as a public repository. As a result I'm using dynamic variables set by an .htaccess file in my database.php configuration file for my production environment. This allows me to also update my deployment using a git pull command on my host which helps speed up work.

The database.php file has something similar to

$database = $_SERVER['DBNAME'];

$database_user = $_SERVER['DBUSER'];

This is much like what is done when deploying to PagodaBox & works perfectly fine for the application with all things functioning as expected in the browser, no complaints.

The problem I have is that artisan is unable to use these variables and will attempt instead to connect to the database using what I believe to empty variables when processing a migrate instruction. I get an error that artisan tried to connect to the databases with no password. I have been calling artisan using --env=production and have tested this but found that it will only work if the database.php file has the variables specified explicitly instead of as environment variables.

Is there a way of causing artisan to "see" these environment variables?


answers that have proved useful to me so far:

http://forums.laravel.io/viewtopic.php?pid=8455

and

Environment driven database settings in Laravel?

Community
  • 1
  • 1
twmbx
  • 105
  • 2
  • 9

2 Answers2

5

Because Artisan is a CLI PHP request - the request never hits the .htaccess file - and therefore your variables are never set.

As a workaround - you could define the variables inside the artisan file itself on line 3 (just after the <?php)

$_SERVER['DBNAME'] = 'test';
$_SERVER['DBUSER'] = 'something';

edit: I just noticed you said this is public hosted on github - so you wont want to include your username/password in the file? Maybe put the artisan file as part of the .gitignore group - so you dont push/pull that single file?

Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
Laurence
  • 58,936
  • 21
  • 171
  • 212
  • Ah I see. Yes DEFINITELY can't put my user/pass in the public repo. Would have to use your solution of adding it to the ignored file list. THANKS! – twmbx Jan 19 '13 at 09:43
  • 1
    Actually upon thinking about this... Anyone needing to contribute to the github project would need the artisan file in order to work with laravel so excluding it in the repository would be counterproductive.. I'm going to conditionally require a config file for artisan and add THAT file to the ignore list. – twmbx Jan 19 '13 at 09:53
3

The capability to set up environment variables is built in to Laravel, so there's no reason to do it in .htaccess. Laravel's built-in way works with artisan without any trouble.

See this part of the docs about environment variables you would like to protect.

http://laravel.com/docs/configuration#protecting-sensitive-configuration

Quoting:

... create a .env.local.php file within the root of your project [...] The .env.local.php should return an array of key-value pairs, much like a typical Laravel configuration file:

<?php
return array(
    'TEST_STRIPE_KEY' => 'super-secret-sauce',
);

All of the key-value pairs returned by this file will automatically be available via the $_ENV and $_SERVER PHP "superglobals". You may now reference these globals from within your configuration files:

'key' => $_ENV['TEST_STRIPE_KEY']

Be sure to add the .env.local.php file to your .gitignore file. This will allow other developers on your team to create their own local environment configuration, as well as hide your sensitive configuration items from source control.

Add your private environment variables

<?php
return array(
    'MY_SECRET_KEY' => 'super-secret-sauce',
);
Moshe Katz
  • 15,992
  • 7
  • 69
  • 116
gerobk
  • 724
  • 1
  • 5
  • 14