Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Environment variables and artisan not working on production server

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.

like image 544
Karl Avatar asked Nov 11 '22 02:11

Karl


1 Answers

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

like image 128
ryanwinchester Avatar answered Nov 12 '22 18:11

ryanwinchester