PHP not loading ENV vars set in /etc/sysconfig/httpd - php

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.

Related

PHP sessions: $_SESSION is empty running locally on Mac

I know there are similar other questions here, but none of them solved my problem.
The problem: I'm setting data into $_SESSION, but in upcoming calls it is appearing as empty.
What I did to try solving it?
Make sure to run an up-to-date version of PHP
Make sure to know which php.ini is used
Log status of $_SESSION before and after setting it
Set various variables as suggested in other questions and forums
Here below I provide all the info I could supply, and hoping somebody will be able to suggest a solution!
I have installed PHP on my Mac using brew install php
I am running it locally for software development using this command:
php -S localhost:8099 -d display_errors=on -t .
[Tue Nov 17 23:12:55 2020] PHP 7.4.11 Development Server (http://localhost:8099) started
I'm calling session_start(); at the beginning of my script.
I am setting values into $_SESSION, but it is always empty when I am testing it in the following calls:
$ses = json_encode($_SESSION);
site_log("before: {$ses}");
session_regenerate_id();
$_SESSION["upath"] = 'XXXXXX';
$ses = json_encode($_SESSION);
site_log("after: {$ses}");
The result is always the same (while I'm expecting to see it on first call but not for the following calls):
20201118_040124_484 before: []
20201118_040124_485 after: {"upath":"XXXXXX"}
I was trying to follow many ideas from web forums:
Verify which is the INI file:
php -i | grep 'Configuration File'
>Configuration File (php.ini) Path => /usr/local/etc/php/7.4
>Loaded Configuration File => /usr/local/etc/php/7.4/php.ini
I was setting various INI file variables:
session.save_path = "/Users/myuser/Prog/MyApp/sessions"
+
chmod 777 /Users/myuser/Prog/MyApp/sessions
+
session.use_cookies = 1
+
session.cookie_secure = 0
But the result is still the same. Any idea please?
The following link points to the result of calling phpinfo(): link
So finally I have found a solution.
It is based on information provided here: Session cookie not being set
As #deceze was commenting correctly to my question, the first steps to debug such problem are these:
Check whether session files are created in the save_path folder (in my case: yes)
Use the dev-tools of the browser to see if cookies are set for the site (in my case: no)
So the actual problem is: why cookies are not set?
The solution:
Add an entry to alias 127.0.0.1 as a 'dot com' name. This is what I did:
# for Mac! see linked answer for other platforms
sudo vi /private/etc/hosts
# add this line:
127.0.0.1 mylocal.com
Activate the local php server for this domain (from the site root folder):
php -S mylocal.com:8099 -d display_errors=on -t .
Load the site from the php server. I'm using Brackets, and I had to open the menu: File -> Project Settings and set the live preview base URL to be:
http://mylocal.com:8099/
Some variables in the INI file might need to be set too, as already detailed in the question above
Hoping this info will help to others!

Can php-fpm service interpret environment variables inside www.conf to create unique log files?

I'm trying to set multiple cloud servers behind an ALB to write their log files to my common EFS. I have a single shared drive EFS, which I can access via a single development instance, while all my production instances are securely behind a VCN. I am trying to use system environment variables (specifically the HOSTNAME) so that each of the production instances, which use the same configuration file to start php-fpm (so that I can do auto-scaling), write a different log.
I've seen answers that say that the *nix way of using environment variables should work, but they are not working for me. Specifically ${HOSTNAME} returns blank (and I have checked it is set for the apache and root users; root starts the php-frm service). I also tried interpolations of that, and the bash flavor of things "%()". None of them work.
All of them return blank strings (but not errors).
I did create a single variable in my environment variable, and got that to work.
I also read that you can't assign a env value to a variable directly, but when I tried to create a separate variable, that actually returned an error when starting the service.
This is what I would expect to work:
slowlog = "/shared/logs/php-fpm/www-slow-${HOSTNAME}.log"
Tried all of the following:
slowlog = "/shared/logs/php-fpm/www-slow-".${HOSTNAME}.".log"
--> blank for hostname
slowlog = "/shared/logs/php-fpm/www-slow-${HOSTNAME}.log"
--> blank for hostname
slowlog = "/shared/logs/php-fpm/www-slow-%(HOSTNAME).log"
--> the file name includes %(HOSTNAME)
slowlog = "/shared/logs/php-fpm/www-slow-$HOSTNAME.log"
--> the file name includes $HOSTNAME
slowlog = "/shared/logs/php-fpm/www-slow-{$HOSTNAME}.log"
--> the file name includes {$HOSTNAME}
slowlog_name = "/shared/logs/php-fpm/www-slow-{$HOSTNAME}.log"
slowlog = slowlog_name
--> start error: unknown variable slowlog_name
Thank you for any tips/advice!
PHP 7.3 does support environment variable interpolation for log file names in config files - I just tested this. I suspect that where you're getting caught is that in bash, $HOSTNAME is a bash variable but not automatically an environment variable, and Linux systems vary in whether $HOSTNAME is set as an environment variable. RedHat distros do set it, Ubuntu does not:
jacob#nitrogen ~ $ cat /etc/redhat-release
Fedora release 29 (Twenty Nine)
jacob#nitrogen ~ $ echo $HOSTNAME
nitrogen
jacob#nitrogen ~ $ env | grep HOSTNAME
HOSTNAME=nitrogen
jacob#iad11:~$ grep PRETTY_NAME /etc/os-release
PRETTY_NAME="Ubuntu 18.04.2 LTS"
jacob#iad11:~$ echo $HOSTNAME
iad11
jacob#iad11:~$ env | grep HOSTNAME
You can test with a different environment variable, and then set $HOSTNAME in a custom startup script that then starts PHP.

How to set and retrieve environment variables using PHP on EC2 instance

I am having an issue with trying to set environment variables in my EC2 instance and then retrieving them with php.
So I have an EC2 instance running a Bitnmai Wordpress site. I ssh's into the server and added the environment variable in
/etc/environment
export VARIABLE_NAME=example_value
on the front end of things, my php to retrieve the value is:
<?php $env_var = $_ENV["VARIABLE_NAME"];
But it returns blank
I have also tried
$env_var = array('environment' => getenv("VARIABLE_NAME"));
But that just returns {environment: false}
Any help would be greatly appreciated
I typically set environment variables on EC2 instances in an .htaccess file. For example, here is MySQL connection information which is consumed later by a configuration file:
SetEnv DBL "mysql:dbname=rocknroll;host=rocknroll.local;port=3306"
SetEnv DB_USER "Elvis"
SetEnv DB_PASS "1amth3K1ng"
Then in my PHP file(s) I do this:
define('DBL', getenv('DBL'));
define('USER', getenv('DB_USER'));
define('PASS', getenv('DB_PASS'));
This uses PHP's getenv() function and makes the variables easy to retrieve.
So when I would restart the apache server, it would overwrite the .htaccess file to its default state and all my changes would be gone.
So I found a solution that works for what I need. I added the environment variables into the wp-config.php file define('ENV_VARIABLE','VALUE');
Then I was able to use that value by $value = ENV_VARIABLE;
Thanks for all of the help!

Getting environement variables in Symfony 2.4

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)

How to add ignored files into heroku?

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)

Categories