Environment variables and artisan not working on production server

I use Github to deploy my sites on my production server. Because of this, I do not want to store .env files. *. Php so they are in my .gitignore.

Inside each environment directory, I set up the database configuration 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' => '', ), ), ); 

Then I went into the apache conf file for my virtual hosts and set the environment variables, for example:

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

All this works fine on a production server.

However, I only manually imported the database at the moment, and not using php artisan migrate , because it does not work.

I know that this does not work because it does not hit the Apache, so the variables are not set, but I tried many ways to try to work around this problem, but I have not been lucky yet.

I tried to make the environment see if it can get variables using php artisan --env=production migrate

I also tried to verify that it was using the correct environment by running php artisan env and using the correct production environment.

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

We will be very grateful for any ideas.

+5
source share
1 answer

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

To set environment variables in your production, you use a file named .env.php in the root of the project:

 <?php # .env.php return array( 'DB_HOST' => 'localhost', 'DB_NAME' => 'my_database', 'DB_USER' => 'user_name', 'DB_PASS' => 'super-secret-sauce', ); 

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

When running artisan commands, such as migrate or db:seed , you can specify the environment using the --env= parameter.

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

+1
source

Source: https://habr.com/ru/post/1201418/


All Articles