So I have a docker image for moodle, and it includes a config.php file that can "automate" connection to its database.
<?php // Moodle configuration file
unset($CFG);
global $CFG;
$CFG = new stdClass();
$CFG->dbtype = 'pgsql';
$CFG->dblibrary = 'native';
$CFG->dbhost = 'db';
$CFG->dbname = getenv('MOODLE_DB_NAME');
$CFG->dbuser = getenv('MOODLE_DB_USER');
$CFG->dbpass = getenv('MOODLE_DB_PASSWORD');
$CFG->prefix = 'mdl_';
$CFG->dboptions = array (
'dbpersist' => 0,
'dbport' => '',
'dbsocket' => '',
);
$CFG->wwwroot = getenv('MOODLE_WWW_ROOT');
$CFG->dataroot = getenv('MOODLE_DATA_ROOT');
...
If I replace all of these with just a string, everything seems to work just fine. However, having a file that just has credentials in plaintext is a very clear security concern, and trying to use getenv()
, $_ENV[]
, or $_SERVER[]
seem to throw this error:
Error: Database connection failed
It is possible that the database is overloaded or otherwise not running properly.
The site administrator should also check that the database details have been correctly specified in config.php
I have confirmed that all of these ENV variables do exist, and even have them currently hard-coded in the dockerfile.
Example: ENV MOODLE_DB_PASSWORD=pass
Additionally, running docker moodle-container env
shows that all of these variables do exist, and have values.
What am I missing?
TLDR: When I try to use getenv() instead of hardcoded strings, moodle can no longer connect to my DB, I am unsure why.
EDIT: After @Russel England suggested I turn on error reporting, I now have an additional error message:
Warning: Undefined property: stdClass::$lang in /var/www/html/lib/weblib.php on line 2239
EDIT 2: After parsing through the terminal output, I am also getting the following error (not displayed in the UI when trying to sign in to the server)
pg_connect(): Unable to connect to PostgreSQL server: FATAL: password authentication failed for user "appuser" in /var/www/html/lib/dml/pgsql_native_moodle_database.php on line 197
This error pops up, even when I hardcode the password (in the Dockerfile), and only dissapears when all the variables are hard coded as strings (In the php file) instead of a getenv() call.
So looking at the php-fpm configuration
; Clear environment in FPM workers
; Prevents arbitrary environment variables from reaching FPM worker processes
; by clearing the environment in workers before env vars specified in this
; pool configuration are added.
; Setting to "no" will make all environment variables available to PHP code
; via getenv(), $_ENV and $_SERVER.
; Default Value: yes
;clear_env = no
uncommenting clear_env = no
fixed it!