Laravel Artisan - Cli doesnt reads environment variables - php

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:
Setting them for the current user ~/.bash_profile, ~/.bash_login or ~/.profile
Setting them globally with the /etc/profile file or with a file in the /etc/profile.d/ directory
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.

Related

What cache stores the windows PATH and how do I clear it?

I am using PHP in an HTML5 document in Windows 10, calling shell_exec() which I want to use to call Rscript, ultimately. The problem I have run into is that the environmental %PATH% variable in Windows that shell_exec() is getting from some cache somewhere is yesterday's version, which doesn't include the path to Rscript, so the call fails.
I carefully set both the personal and the system environmental PATH variables in Windows using the Windows tool to include a path to Rscript, and then later to remove another link to PERL to make sure the total path was below 260 characters, then I moved the Rscript path up to the front. No joy.
The following command pulls up a %PATH% from somewhere but I have no idea where If I simply use CMD to go to that directory and issue the commands via that shell, they find the right path, but if I call it via shell_exec() it's the old path. I've tried rebooting and clearing the browser caches so I'm pretty sure this is a PHP cache thing, but I see many references to cahces here to figure out which one I'm facing.
So any help would be appreciated.
This code shows the OLD path, not the current one:
shell_exec("echo %PATH% > outputFilexx.Rout 2> errorFilexx.Rout");
Environment variables are not really cached but simply part of the run-time environment of a process. A new process inherits all environment variables and their values - at that time when it is started - from its parent process.
After that changing the value of an environment variable in one process does not affect other (already running) processes.
How to change an environment variable of a running process depends on the program and may not always be possible. To "reload" environment variables otherwise you need to restart the process - and possibly also their parent processes since otherwise they would inherit the "old" values from them again.
I.e. you not only should restart PHP/Apache but also the WAMP console so %PATH% is inherited from the system environment. To be completely sure you can just reboot the whole machine.
But environment variables may also be change via application depend configurations. So you could change the PATH only for
Apache via SetEnv
PHP via putenv()

Symfony command php bin/console can't read Apache SetEnv?

With Symfony 5.2, when I execute this command
APP_ENV=prod php bin/console d:m:m
I have this message :
WARNING! You are about to execute a migration in database "db_name" that could ...
However, in my Apache environment variables, I customized the database name :
SetEnv DATABASE_URL "mysql://website:password#localhost:3306/website_prod"
I am sure that this configuration works (when I access the site, I am in the prod environment while I left dev in the .env generated by Symfony).
Why is the wrong database displayed on the APP_ENV=prod php bin/console d:m:m command line? I think Apache variables are not taken into account in php bin / console ... command line and I need to create a specific .env.prod.local.
Can you confirme ? If yes, I don't see why Symfony mentions this in their documentation (https://symfony.com/doc/current/setup/web_server_configuration.html#apache-with-mod-php-php-cgi)
Console commands do not run under the web-server, hence they do not have access to whatever configuration you have for the Apache vhost or anything like that.
The best way to deal with this kind of configuration is storing this values in environment variable, .env files, or use Symfony's secret management features.
This way this configuration will be available both to the application when accessed via the webserver or through a command line script.

.env file wont load variables into config.php - student setting up first php dev environment

Afternoon everyone,
I'm a student studying Computer Science and I'm trying to recreate the environment my friend is using to host their PHP based web app. They're on a Mac using heroku local (Procfile calling heroku-php-apache2) to set up their environment. I'm on a Windows 10 PC, and from what research I'm done, heroku local is not supported in any way. So I enabled WSL installed Ubuntu 18.04 and Apache2 and, as far as I can tell, downloaded and installed all of the other components necessary to make it run (composer, modrewrite, modenv, etc). phpinfo(), heroku's sample project, and any simple php pages I make display properly. My friend's app on the other hand is still giving me trouble.
They're using a .env to declare project specific environment variables that are further defined in a config.php. The app is deployed and works in Heroku and on their machine, but when I try to load the app locally on my machine I get an exception thrown saying the environment variables aren't being loaded. If I add "local: php -S localhost:80" to the Procfile and run heroku local on Ubuntu, it sees the .env file and says its loaded, only to kick back the same errors my apache2 instance is throwing.
What could be causing this? I've edited php.ini to include an "E", enabled modrewrite and modenv, made sure my .env file was encoded in UTF-8 - I've searched far and wide for a reason this might be happening but I keep coming to a dead end. Is there something about the "heroku local" command and instance that I'm missing? I'm still new to php, web servers, and programming in general, so any relevant information regarding why my .env file isn't working or any possible ways I can get heroku local to work on Ubuntu using WSL would be massively appreciated.
I'm using WSL and am similarly having problems with my .env not working properly.
Although I'm still looking for a more elegant fix, the hack I'm relying on now is:
Copy the contents of your .env to a temporary text file.
In that text file, do a regex find-and-replace searching for "\n" and replacing with "\nexport " (with a trailing space).
Move the final line's "export " to the first line.
Copy the contents of this temporary text file.
Paste into your terminal to run these commands.
Now you should be able to run your app.
Straightforward Method
You can do this very simply by adding the environment variable to your .bashrc file, assuming that your Windows Environment variables are set correctly and WSL is installed, I'm going to use Java as an example, but any environment variable will work.
Use text editor in WSL or type code .bashrc from WSL home to initialize the WSL VSCode editor. Add the environment variable to the file:
# Shared environment variables
export JAVA_HOME=/mnt/d/Java/jdk11.0.4_10
Just ensure that you change the directory to point to your directory. In my case, it's in D:\Java\jdk11.0.4_10 which in WSL is /mnt/d/Java/jdk11.0.4_10
Also, since you're using Windows binaries, you must specify the file type when running from a WSL bash shell:
Example
Windows:
javac MyClass.java
java MyClass
WSL:
javac.exe MyClass.java
java.exe MyClass
Note that the .exe file extension is being used, since we're calling the Windows binary. If it was a native Linux distribution of the JDK you could simply use java command.
This holds true for any Windows binary that is being run through WSL.

PHP CLI environment detection

I have a Linux server with a bunch of different websites on it. When I program websites, I will develop them locally and set APPLICATION_ENV to development. On my server, I have a staging subdomain setup which I set APPLICATION_ENV to staging. Lastly, if I end up hosting the production website, I can set APPLICATION_ENV to production. All of this works fine, but lately I have been running CLI tasks after every deployment. My command line scripts obviously can't do anything with the APPLICATION_ENV variable I have set up through Apache. I know that you can export a variable like so:
export APPLICATION_ENV="staging"
However, this would be setting it on my whole server, when I really only want to set it on the staging subdomain. Is there a way to contain these variables to a subdomain on my server?
For title of your question:
if (PHP_SAPI == 'cli') {
echo 'This is CLI mode!' . PHP_EOL;
var_dump($argv);
}
For description:
I think you can set APPLICATION_ENV value with args or set it (in source code) as internal CLI APP ENV mode.
There is nothing wrong to have environmental variables server wide, since you can set them for a specific user (or even session) only.
Anyway, probably the easiest way would be to pass the "env" as a parameter, e.g.: cli.php --env=staging
Look how they solved it in Laravel: https://laravel.com/docs/4.2/artisan
Specifying The Configuration Environment
You may specify the configuration environment that should be used
while running a command using the --env switch:
php artisan migrate --env=local
Symfony: http://symfony.com/doc/current/cookbook/console/usage.html
$ php bin/console cache:clear --env=prod

How could I simulate an offline directory with Heroku?

I used to have an offline directory on my server with Perl scripts to dynamically create files.
Say this directory was in an offline directory for security reasons (/server/back/scripts) I used to access it with exec(/server/back/scripts/auto.pl $arguments)
Contents of auto.pl:
system('cp /server/back/includes/default /server/front/ann/'.$enc.'.php');
system('chmod 555 /server/front/ann/'.$enc.'.php');
system("perl -pi -e 's/string/".$key."/g' /server/front/ann/".$enc.".php");
This script copy-paste a default file with garbage values to a public directory, and replace garbage values with something else while setting up the rights we want.
how can I reproduce this on Heroku? - if not possible is there any way to at least reproduce the behavior of this script?
It looks like the goal of this script was to inject keys/credentials into your PHP application by searching/replacing.
Heroku encourages configuration via environment variables, especially keys/credentials.
You should add your keys via the Heroku command line tool:
heroku config:set MY_API_KEY=super-secret-hex-goes-here
... and then pull in the values from the environment on your dyno in your PHP code:
$api_key = getenv('MY_API_KEY');
This will allow you to provision keys/secrets for each running application on Heroku without having to store anything in source code.
Heroku does not expose the File system of the current web dyno. So you can't make any changes to the files to have an impact on the running server.
Files can be changed if you have the file in the code base itself. So, you could commit the change and deploy on Heroku.
If these variables, that you are talking about have to be used system wide then the best way is to use Environment variables as mentioned in Winfield's answer.
You can set and unset Environment variables using command :
heroku config:set VAR_NAME=value [VAR_NAME_2=value.........]
heroku config:unset VAR_NAME [VAR_NAME2.......]
If you need to maintain a file with current values of Environment variables , then you can use this command :
heroku config:pull --overwrite
This will get all the Environment variables currently set on server and store them in .env file.
Then you can update the values in .env file locally itself and update the same on server by command:
heroku config:push
This will replace all current values on server with the values in the .env file
Read more about environmet variables here : https://devcenter.heroku.com/articles/config-vars

Categories