apache - PHP not loading ENV vars set in /etc/sysconfig/httpdGet the solution ↓↓↓
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.
People are also looking for solutions of the problem: xml parsing error: no root element found
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.