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

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.

Related

Shifting from App Services to a Docker container due to file persistence?

While connected to my App Service (vanilla PHP 7.4 App Service) through SSH, I can see:
Note: Any data outside '/home' is not persisted
If my php.ini and apache2.conf reside outside of /home, I'll never be able to have changes to these files persisted correct? IIRC, I can't modify apache2.conf to set another location for php.ini within /home, the Apache configuration change will never persist the Apache restart.
This only means that I need to build a docker container to have full control of my settings? Am I understanding this correctly?
Thank you #CSharpRocks posting your suggestion as an answer to help other community members for similar issue.
Based on the MS DOC:
To Customize PHP_INI_SYSTEM directives we can't use the _.htaccess_ approach. App Service provides a separate mechanism using the PHP_INI_SCAN_DIR app setting. First, run the
following command in the Cloud Shell to add an app setting called
PHP_INI_SCAN_DIR .
az webapp config appsettings set --name <app-name> --resource-group <resource-group-name> --settings PHP_INI_SCAN_DIR="/usr/local/etc/php/conf.d:/home/site/ini"
For example if want to change the value of expose_php run
the following commands:
cd /home/site
mkdir ini
echo "expose_php = Off" >> ini/setting.ini
For more information please refer this SO THREAD

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:
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.

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

Symfony2 bundle creating error

I am using windows system. I have just installed the symfony to my localhost as it is install with no error inside.
bellow url run fine with browser.
http://localhost/symfony/web/app_dev.php
now i have tried to create bundle from command prompt with following syntax.
php app/console generate:bundle --namespace=Acme/HelloBundle --format=yml
it shows an error like. "could not open input file."
however the environment variable is already set for command prompt.
so can any one please help me create bundle or give another way to create bundle in symfony.
Thanks.
What environment variable is set? Best thing is to run this command while you are in main project folder (in your case symfony folder).
Could not open input file.
implifies that app/console, does not exist. Make sure to run
cd path/to/your/symfony/folder
before trying to generate the bundle. :)
Run your command from your symfony root folder. Also try to do:
php app/console generate:bundle
it will ask you to specify a folder and format. Also that it needs to generate the structure.
Also check that your php-cli.ini has all the necessary configurations to execute the code.

Categories