getenv not working in EC2 Server - laravel

I'm trying to deploy to production my app that is working well in local.
Thing is when try:
dd(getenv('APP_ENV'));
it returns "false"
but when I connect ssh and type:
php artisan env
I get
production
Any idea why it stopped working???
For the record, In production, my deploy script execute 3 commands:
composer dump-autoload -o
php artisan route:cache
php artisan config:cache
I mention it because it is possibly the only software config that is different.
EDIT: I identify that the problematic command is:
php artisan config:cache
If if do:
php artisan config:clear
problem is solved.
Tx!

When using a cached config the .env file is not used anymore so getenv is useless, because the config is loaded from:
bootstrap/cache/config.php
Instead you can get the current environment from the loaded application configuration like so:
config('app.env');
Or directly using the app helper function:
app('env');
As a third option you can always use the environment method to get the current environment:
app()->environment(); // or App::environment()
Laravel uses the dotenv library internally to load the configuration keys from the .env file and add them to the environment variables using putenv, but when you cache your configuration that loading part is not done anymore because Laravel detects that there is a cache file present and uses that instead, so those keys from .env are not loaded into the environment, this not accessible via getenv.
And because configuration values from the .env file are only cached when they are used in an actual configuration file from the config directory, you need to create a configuration option for them to be cached and accessible when you're using the cache.
So if you want to have a BASE_URL key in your .env file with this value:
BASE_URL=http://domain.com/
If you want to be able to access its value when the configuration is cached, you need to use it in a configuration file. For example, you can add it to your config/app.php file like so:
'base_url' => env('BASE_URL')
Then you can access even when the configuration iit using:
config('app.base_url')
You can read more about accessing configuration values in the Laravel Documentation.

Related

Laravel route returns localhost despite I changed the env variable

I have APP_URL env variable set to domainname.com and I have url set to the same domain in config file as well. route('route.name') in artisan tinker returns the proper domain. yet when used in the applicaiton code, it returns localhost. Any thoughts from you would be appreciated.
Edit: My environment is Github codespaces
Try running php artisan optimize:clear command to clear cache,
routes etc all in one go.
If you have separate frontend then change it there as well to hit the
right backend with your provided domain name.
Restart your docker containers if you are using Docker.

Laravel vuejs. In vue files data from .env file shows wrong value

guys. I'm working on laravel 8 + Vue 3. I real server .vue files (console.log(process.env.MIX_APP_URL) - for instance) shows wrong values from .env.
I checked my .env file in server:
MIX_APP_URL=https://laravel.process.kz.
But in .vue files it gives me "http://localhost:8888/laravelprocess".
I tried php artisan config:cache, php artisan config:clear, php artisan cache:clear, php artisan key:generate etc. Please, help to find out whats the problem.
If you change the env file you might need to restart serve if its still running.
It might also load additional .env files for instance:
.env # loaded in all cases
.env.local # loaded in all cases, ignored by git
.env.[mode] # only loaded in specified mode
.env.[mode].local # only loaded in specified mode, ignored by git

How can I save changes in .env file in Laravel?

I have a huge problem with .env file in Laravel. When I make a change in .env file, it is not changed. I think all of you know what I'm talking about. I run this php artisan config:cache, but yet, nothing changed! I echo out a variable in .env file and nothing changed. What can I do for that?
When you cache the configuration all calls to env will return null because it does not load the .env any more. You should not have any calls to env outside of the config files.
You use the configuration system to get the values you want, which is why the config files make calls to env to get values from the environment.
If you using php artisan serve you have to reload it after changing .env file

Laravel env('APP_URL') is not returning the correct value for localhost

When trying to retrieve the APP_URL from the Laravel config it returns the wrong URL for development only.
My env file has the following:
APP_URL=http://127.0.0.1:9000
However when I call env('APP_URL') it returns me:
'http://localhost'
Which will not work with my current docker set-up it has to be 127.0.0.1:9000
In my config/app.php file I have the following:
'url' => env('APP_URL')
I have tried php artisan config:cache and php artisan config:clear but I still get the same result of http://localhost
Any ideas on where it could be getting http://localhost from other than the .env or config/app.php?
Thought it would be worth noting using config('app.url') also returns http://localhost
I found out there is an undocumented feature when loading .env files. If you have a .env.dev file in your project folder it will load this config over everything else if your application is in dev mode. It's documented that .env.testing is used in testing which makes sense but it also does it for dev with .env.dev
The application I was working on had a .env.dev and it was this file that had the http://localhost string inside of it.
Just in case anyone is having this issue in production, these are the steps that worked for me:
Make sure that the .env file is specifying that the app is in production. APP_ENV=production
Run php artisan optimize:clear
"localhost" is probably the default value. You error indicates that the .env file is not read at all. Make sure you're not editing .example.env, check if other env variables are accessible, check for typos, check file permissions on .env and if it's located in the root folder of the project.

Laravel 5.4 Token Mismatch Exception on live server

I am facing Token mismatch exception in Laravel 5.4 on live server. On my local machine running Windows XAMPP, the application works fine. When I deployed it to live machine running CENTOS 7 and LAMP stack, I see a redirecting to myhostname.com/login at the top left corner of the browser and then lands on an error page showing:
token mismatch exception on line 68 of VerifyCsrfToken.php.
I had this same problem and what i did was:
First in your .env file remove: SESSION_DOMAIN
And in set session driver like: SESSION_DRIVER=file (if your want it to save to a file)
then do: php artisan cache:clear
then `php artisan config:clear
that worked for me.
Try to clear config cache on the server with :
artisan cache:config
You will need to clear config cache also if you edited config/app.php for some reason.
In addition there is an other possible reason : you copied not only the application but also the cache/session files when you deployed your application to production. Basically everything in the storage folder should be ignored on deployment.
Run this command
php artisan config:clear
Verify .env file
delete current .env file and create new .env file from .env.example
verify that config/session.php has is correct or not modified.
You can check the file permission as well of storage directory

Resources