When setting up an application, I define a configuration value as env variable. The value is present when running env
REDIS_URL=redis://mypassword#myhost
The application does not work. When I copy the exact same value directly into the config.yml file, the application works.
dsn: "%env(REDIS_URL)%" // Does not work
dsn: "redis://mypassword#myhost" // This works fine
For every other configuration value, it is working, except this one. So is there any escaping to be done when using getenv? Or ENV values?
EDIT : when disabling the password on the redis server, it actually works. Seems to be something wrong with the password.
REDIS_URL=redis://myhost
Related
I am running Drupal sites (Apache and PHP), and would like to set some of the values in the settings files to be environment variables. After researching, the proper approach appears to be adding them into the /etc/sysconfig/httpd file. It appears that PHP (mod_php) is not loading the variables.
My /etc/sysconfig/httpd file looks like this -
LANG=C
db_server=internaldns.mine.com
db_port=3306
memcache_server1=otherinternaldns.mine.com:11211
I am loading the variables in Drupal (which is php 7 (mod_php)) like this -
$databases['default']['default'] = array (
...
'host' => $_ENV['db_server'],
'port' => $_ENV['db_port'],
...
);
However, it doesn't appear to be loading the variables, as when I do a status of the site, I see the following -
Drupal version : 8.9.11
Site URI : http://default
DB driver : mysql
DB port : (THIS SHOULD HAVE THE VARIABLE)
When I set the same values without variables they work normally.
Is there some step I am missing here for getting the variable loaded inside of the env?
To anyone who encounters something similar:
Even though the variables were set in the /etc/sysconfig/httpd file, they were not being imported into my Shell (but PHP was importing them correctly).
After creating a file in /etc/profile.d/ that exported the variables for the shell, it worked on the cmd line as well -
#!/bin/bash
# DB Vars
export db_server=db_server
export db_port=3306
As a note - Even though it looked like a failure on the CMD line before these variables were exported, the website was being served successfully by httpd. This implied that PHP was using the variables correctly.
How can I change the app environment at run time?
I have some classes that only bind in the service provider in production. I'd like to assert with a unit test that they are properly bound. For other environment variables, I can set them with the config helper and then simply call resetApplication in tearDown but for some reason the variable set by APP_ENV doesn't change.
dump(app()->environment()); // "testing"
config(['app.env' => 'production']);
dump(app()->environment()); // "testing"
What can I do to get app()->environment() to return production at run time?
app()->environment() reads directly from the variables specified in your .env file rather than the configuration files.
You could take two approaches to solve your problem.
1. Read the environment variables from the config file rather than the .env file.
dump(config('app.env')); // "testing"
config(['app.env' => 'production']);
dump(config('app.env')); // "production"
2. Change the value of 'env' in the current app instance by changing the value of app()['env'].
dump(app()->environment()); // "testing"
app()['env'] = 'production';
dump(app()->environment()); // "production"
I've noticed that above answer can be a bit dangerous: You might be overwriting the 'env' key, without truly switching environments. Suddenly your app says that you are in testing, while your DB connection is still set to production.
Normally you just want to really stick to the Laravel best practice of defining ONE environment for each literal environment, but for my use case I needed to temporarily & programmatically switch between multiple environments within a single artisan script.
My solution (works in Laravel 5.8+ with DotEnv3) would be to really reboot the application:
<?php
$basepath = app()->basePath();
$env = app()->basePath('.env.alternative');
$boot = app()->basePath('bootstrap/app.php');
// Overwrite webserver env
(new Dotenv($basepath,'.env.alternative'))->overload();
// Reboot the application
(require $boot)
->loadEnvironmentFrom($env)
->make(Kernel::class)
->bootstrap();
// This returns 'mysql_alternative', as defined in .env.alternative
dd(DB::connection()->getName())
Disclaimer: I've only tested this to the extend of my own code.
From a testing point of view, then in the setUp() method:
$this->app['env'] = 'production';
But remember, only do this in a test class, specifically testing production runs. Otherwise, do it at the top of the test method, remembering to set it back to testing at the end.
If you adjust the setUp() method, remember your tearDown() method and change it to testing. By altering the $this->app['env'], you are directly changing the Applications environment, which is why it's so important to remember to set it back as it will produce side effects, also only do this in testing .
I've been struggling to get the necessary env variables in a Symfony 2.4 application. The idea is to put the application in a docker container that will be managed by Amazon ECS.
I have tried the following :
1 - export a variable with :
export SYMFONY__DATABASE__HOST=blabla
then in parameters.yml.dist, get it with :
database_host : '%database.host%'
Didn't work. After composer install, en parameter.yml is created with that look like parameters.yml.dist. The values have not been translated to whatever is in the env variables.
I saw somewhere that I need to disable the incenteev bundle. Didn't help either.
2 - Add the variables in composer.json using incenteev's "env_map"
"env-map": {
"database_host": "DB_HOSTNAME"
}
DB_HOSTNAME being an env variable that I export in the same way
export DB_HOSTNAME=balbla.com
When I type php -i | grep DB, I do see those variables.
Then when I type composer install --no-interaction, in the first case I get this error message : You have requested a non-existent parameter "database.host".
In the second case, parameters.yml is created with whatever values are in parameters.yml.dist, and the env variable I added to composer.json is not used any where.
https://github.com/symfony/symfony/issues/7555
and
http://symfony.com/doc/current/configuration/external_parameters.html
did not help much.
Any Ideas guys? I really would like to get those env variables in the cleanest way possible.
I have run into the same trouble once. I found a way to get around it like Platform.sh do to host their Symfony applications. It's available on Github :
https://github.com/platformsh/platformsh-example-symfony
So basically, you create a parameters_prod.php config file wich you import in config.yml. Inside, you test to see if SYMOFNY_ENV=prod (which you need to set up when you deploy in production anyway). If this variable is set, you get what's in parameters_prod.php, otherwise you get what's in parameters.yml (for the dev environment).
And to get you environment variables in parameters_prod.php, you use the getenv() PHP method.
So basically, your parameters_prod.php will look something like this :
<?php
if (getenv('SYMFONY_ENV') == 'prod') {
$container->setParameter('database_driver', 'pdo_mysql');
$container->setParameter('database_port', 3306);
$container->setParameter('database_host', getenv('DATABASE_HOST'));
$container->setParameter('database_name', getenv('DATABASE_NAME'));
$container->setParameter('database_user', getenv('DATABASE_USER'));
$container->setParameter('database_password', getenv('DATABASE_PASSWORD'));
}
Docker will be able to get these environment variable without any thing else to do if you launch Apache in foreground mode as the entrypoint of your container (which you should anyway, otherwise the containers will not stay alive)
Under the authentication section in this link, it is given for Anonymous search and bind: simply set authenticatedBind to false. Could you please tell me how should I do that?
#esyana In GForge 6.3.x and earlier, each of the modules (plugins) in GForge has its own configuration file. They're all located in /etc/gforge, with the core settings in gforge.conf and plugin-specific settings in the appropriate folder under plugins/. You'll find the setting ldap.authenticatedBind in /etc/gforge/plugins/ldap/ldap.conf
If you update the value there, then run cd /opt/gforge/bin && php create_config_cache.php to make the change take effect.
In GForge 6.4 and newer, all settings are stored in the database and accessible using a shell script. Run /opt/gforge/bin/gf-config set "ldap.authenticatedBind" false to make the change. It takes effect immediately.
I have a php app into heroku. I use the free version. My app use codeigniter framework and I need ignore some files with a gitignore, for example I have a file database.php with the information of the database connection's. That information is different in each enviroment, my question is, how can upload these kind of files into heroku?
This is the wrong way to solve your problem. Any config information like:
api keys
database host / password
any other specific env configuration
shouldn't be versionned in git.
You should use environment variables. I assume you want to use different database host/username/password whether you're in development environment (your working directory), in staging, or in production.
1) Check how to set environment variables (it's different between Windows and Linux/Mac OS X), but you have helpers and it's a good practice to use that (even more when using heroku)
2) Since you're using php, you'll be able to retrieve those variable with getenv. You should have something like :
$dbUserName = getenv('DB_USER_NAME');
$dbHost = getenv('DB_HOST');
$dbPassword = getenv('DB_PASSWORD');
//use the variables above to make a db connexion
3) Time to set the env variables on your heroku vm (see more)
$ heroku config:set DB_USER_NAME=foo
$ heroku config:set DB_HOST=bar
$ heroku config:set DB_PASSWORD=fee
Your code will automatically use the correct config when you'll push to heroku (you won't have to bother to have to change something on your code base between your dev env and your production env + each team member can have different config on their local dev machine)