I have 3 environments
development
staging
production
When I use amazon beanstalk every request goes through a load balancer from which point you end up on an ec2 server which could be different every time (hence the problem of figuring out the environment, I can't use the machine name as it's different every time).
I found out about environment variables in beanstalk configuration so I pass an environment variable PARAM_1 from .ebextensions.myapp.config that essentially dictates the environment to use with the following trick.
$env = $app->detectEnvironment(array(
'staging' => $_SERVER['PARAM_1'] == 'staging' ? array(gethostname()) : array('not-staging'),
'production' => $_SERVER['PARAM_1'] == 'production' ? array(gethostname()) : array('not-production'),
'development' => array('mylocalname')
));
This works fine except for php artisan commands! For some reason when running the artisan commands I can not access this environment variable so I'm stuck!
I would like to run php artisan migrate but how do I tell it the environment it's in! (based on the environment I have different database configs)
You can define the environment like this:
php artisan migrate --env=Development
You can also use another SO answer to hard code the artisan enviornment into your code: Environment driven database settings in Laravel?
Related
I have deployed a Laravel app via EBS but without the .env file.
Instead, i have entered all of the variables in the EBS configuration > Software tab.
I did a test just so I can check if they are properly read so I set APP_ENV=stage but when I ssh to the ec2 insteance created by EBS and I run the php artisan env command it shows production instead of stage which means the variables are not injected properly.
I tried rebuilding the environment several times but no clue. Anyone can help?
When you use EBS configuration > Software tab to define the environment variables, they take effect when the serve a request. they dont update the .env file.
when using CLI artisan commands, it will only follow the .env variables
This is the env variable in my config/app.php
'env' => env('APP_ENV', 'production'),
I changed the APP_ENV variable to staging from .env like the following:
APP_ENV=staging
I run php artisan config:clear.
After that, php artisan env returned:
Current application environment: staging
However, the App::environment() still returns local.
Route::get('/', function () {
dd(App::environment());
});
Any ideas? Did I miss something?
It seems, on the application level, the environment variable is set correctly. The value should be staging.
However, it can be overridden by your web server settings.
The current application environment detection can be overridden by
defining a server-level APP_ENV environment variable.
Source: Laravel 9 Determining The Current Environment
You could check the web server settings.
E.g. with Apache server, the environment variable could be set, in files like httpd.conf as following:
SetEnv ENVIRONMENT "local"
I tried
php artisan serve --env=local
But its still serving the development site.
Laravel development server started on http://localhost:8000
I know I can edit the app config, but for the project I'm working on I need to switch between the two modes often and thats going to be a pain.
ps. I'm using 4.2
It's a bit hacky, but I got it to work like this.
Put at the very top of artisan
if(in_array('serve', $argv))
if(in_array('--env=local', $argv))
file_put_contents('.isLocal','');
else if(file_exists('.isLocal'))
unlink('.isLocal');
And replace the existing environment detection logic with this
$env = $app->detectEnvironment(array(
'local' => file_exists('.isLocal'),
));
Basically artisan will now create an .isLocal file whenever the --env=local option is set and delete it when its not. Make sure to add .isLocal to your .gitignore
For instance, if I set the environment variable DB_HOST with value localhost through Forge, it creates an entry in the nginx file of fastcgi_param DB_HOST "localhost";
When I enter the console and try to run any command, any command that depends on that environment variable returns false if I do getenv('DB_HOST').
However, if I run the site from the browser, there is no problem. Only the console app seems to have this issue.
What is erasing the contents of the environment variables, or preventing them from being read, when in the console?
When you're running from the command line, no environment is set as those are for the HTTPD. Console requests do not go through nginx, just PHP.
You can set the environment for the command you're running, with the --env option. Example:
php artisan migrate --env=develop
I could not find a way to keep the environment variables in one place (the nginx config file) but there is a workaround of creating a .env.php file in your app root dir, which makes them available for production.
To do this, I added the following to my .env.php file:
return [
'DB_HOST' => 'localhost',
'DB_NAME' => 'thename',
'DB_USER' => 'theuser',
'DB_PASS' => 'thepass'
];
Trying to set the environment variable from commandline like
php artisan serve --env=someenv
But a var_dump(App::environment()) outputs string 'production' (length=10).
Shouldn't it be someenv ?
This environment you set on php artisan seve is just for that particular command, running on cli.
Note that this is not you application running, just a webserver, provided by PHP so you don't need to install a full apache or nginx to test your application.
Your web application will run under a different environment and you still need to provide a correct environment adding it to your bootstrap/start.php file:
$env = $app->detectEnvironment(array(
'local' => array('localhost', '127.0.0.1', 'example.com'),
));
Laravel 5 can use a .env file in your installation base directory to determine your environment. For example, to set the current environment as the „local“ one, create a file .env with
APP_ENV = local
Please see another answer for more details on this mechanism.