My system is Ubuntu and I have set my environment variables in /etc/environment
.
If I'm running PHP script using CLI - environment variables from /etc/environment
are recognized.
But, if I go to execute PHP script thru http://domain/test.php
(that is apache2handler) exactly the same script prints out NULL, meaning environment variables from /etc/environment
are not loaded.
The fix I did was adding variables in /etc/apache2/envvars
and that solved the problem.
But that is two different files, which then have to be kept in sync.
How can I make PHP / Apache load and recognize environment variables from /etc/environment
(system)?
EDIT: To clarify things, when I say 'not loaded into PHP' it means variables from /etc/environment
are not set in $_SERVER
, $_ENV
, getenv()
and do not exists in $GLOBALS
. In other words 'are not loaded into PHP'.
Using getenv() In addition to using PHP's Superglobals, you can also use getenv() to retrieve an environment variable. If the function is called without an argument, then it returns all available environment variables. If an argument is passed, however, the value of an environment variable with that name is returned.
To list all the environment variables, use the command " env " (or " printenv "). You could also use " set " to list all the variables, including all local variables. To reference a variable, use $varname , with a prefix '$' (Windows uses %varname% ).
There are two ways to read environment variables in PHP. One is getenv() function and another is $_ENV array. The uses of the getenv() function are shown in this tutorial.
On the Windows taskbar, right-click the Windows icon and select System. In the Settings window, under Related Settings, click Advanced system settings. On the Advanced tab, click Environment Variables.
I had exactly the same problem. To solve it, I just sourced /etc/environment
inside /etc/apache2/envvars
.
The content of /etc/environment
:
export MY_PROJECT_PATH=/var/www/my-project export MY_PROJECT_ENV=production export [email protected]
The content of /etc/apache2/envvars
:
# Load all the system environment variables. . /etc/environment
Now, I'm able to use these variables in the Apache Virtual Host config files and in PHP.
Here's an example of an Apache virtual host:
<VirtualHost *:80> ServerName my-project.com ServerAlias www.my-project.com ServerAdmin ${MY_PROJECT_MAIL} UseCanonicalName On DocumentRoot ${MY_PROJECT_PATH}/www # Error log. ErrorLog ${APACHE_LOG_DIR}/my-project.com_error.log LogLevel warn # Access log. <IfModule log_config_module> LogFormat "%h %l %u %t \"%m %>U%q\" %>s %b %D" clean_url_log_format CustomLog ${APACHE_LOG_DIR}/my-project.com_access.log clean_url_log_format </IfModule> # DocumentRoot directory <Directory ${MY_PROJECT_PATH}/www> # Disable .htaccess rules completely, for better performance. AllowOverride None Options FollowSymLinks Includes Order deny,allow Allow from All Include ${MY_PROJECT_PATH}/config/apache/inc.mime-types.conf Include ${MY_PROJECT_PATH}/config/apache/inc.cache-control.conf # Rewrite rules. <IfModule mod_rewrite.c> RewriteEngine on RewriteBase / # Include all the common rewrite rules (for http and https). Include ${MY_PROJECT_PATH}/config/apache/inc.rewriterules-shared.conf </IfModule> </Directory> </VirtualHost>
And this is an example of how to access them with PHP:
<?php header('Content-Type: text/plain; charset=utf-8'); print getenv('MY_PROJECT_PATH') . "\n" . getenv('MY_PROJECT_ENV') . "\n" . getenv('MY_PROJECT_MAIL') . "\n"; ?>
On ubuntu, PHP uses different ini files for regular and CLI processes.
There should be few ini files like /etc/php5/cli/php.ini
, /etc/php5/fpm/php.ini
or /etc/php5/php.ini
. Open related INI file and change the
variables_order = "GPCS"
line to
variables_order = "EGPCS"
.
After that, you would get the environment variables which you set before using $_ENV['varname'].
From php.ini about variables_order
:
Abbreviations for the following respective super globals: GET, POST, COOKIE, ENV and SERVER. There is a performance penalty paid for the registration of these arrays and because ENV is not as commonly used as the others, ENV is is not recommended on productions servers. You can still get access to the environment variables through getenv() should you need to.
So you can try to use getenv() instead of $_ENV[].
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With