简体   繁体   中英

Laravel Artisan - Cli doesnt reads environment variables

I had made a custom artisan command. In homestead everything goes right, but when I execute the command on server it cant read environment variables. When I ssh the server and try php artisan external:import the connection timeouts cause of null env vars.

On artisan tinker env() function returns null.

How can I read env vars on server?

IMPORTANT: This server is part of AWS, so it does not have .env file.

did you clear config cache already?

php artisan config:clear

i think env function did not work after you cached the config

I know it's a bit late to answer this question, however, I'll share my experience in case it can help someone.

The environment variables will be available depending on how they have been configured. The different ways to configure these variables are:

  1. Setting them for the current user ~/.bash_profile, ~/.bash_login or ~/.profile
  2. Setting them globally with the /etc/profile file or with a file in the /etc/profile.d/ directory
  3. Establishing them at the web server software level. For example, in the httpd.conf file with SetEnv, in apache.

If you have set environment variables through server software, they will be available only to scripts that handle http requests. This leaves out the artisan commands.

My recommendation is that you add a step to your deployment process to set environment variables globally by creating a new file in /etc/profile.d/. For example, /etc/profile.d/webapp.sh. So webapp.sh would have lines like

MY_VAR=my-value

I hope this helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM