My system is Ubuntu and I have set my environment variables in /etc/environment
.
我的系统是Ubuntu,我在/ etc / environment中设置了我的环境变量。
If I'm running PHP script using CLI - environment variables from /etc/environment
are recognized.
如果我使用CLI运行PHP脚本 - 可以识别来自/ etc / environment的环境变量。
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.
但是,如果我通过http://domain/test.php(即apache2handler)执行PHP脚本,则完全相同的脚本会输出NULL,这意味着未加载/ etc / environment中的环境变量。
The fix I did was adding variables in /etc/apache2/envvars
and that solved the problem.
我做的修复是在/ etc / apache2 / envvars中添加变量,这解决了问题。
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)?
如何从/ etc / environment(系统)加载PHP / Apache并识别环境变量?
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'.
编辑:为了澄清事情,当我说“未加载到PHP”时,它意味着/ etc / environment中的变量未在$ _SERVER,$ _ENV,getenv()中设置,并且在$ GLOBALS中不存在。换句话说'没有加载到PHP'。
4 个解决方案
#1
48
I had exactly the same problem. To solve it, I just sourced /etc/environment
inside /etc/apache2/envvars
.
我有完全相同的问题。为了解决这个问题,我只在/ etc / apache2 / envvars中找到了/ etc / environment。
The content of /etc/environment
:
/ etc / environment的内容:
export MY_PROJECT_PATH=/var/www/my-project
export MY_PROJECT_ENV=production
export MY_PROJECT_MAIL=support@my-project.com
The content of /etc/apache2/envvars
:
/ 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.
现在,我可以在Apache Virtual Host配置文件和PHP中使用这些变量。
Here's an example of an Apache virtual host:
以下是Apache虚拟主机的示例:
<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访问它们的示例:
<?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";
?>
#2
1
On ubuntu, PHP uses different ini files for regular and CLI processes.
在ubuntu上,PHP对常规和CLI进程使用不同的ini文件。
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
应该有很少的ini文件,如/etc/php5/cli/php.ini,/etc/php5/fpm/php.ini或/etc/php5/php.ini。打开相关的INI文件并更改
variables_order = "GPCS"
variables_order =“GPCS”
line to
到
variables_order = "EGPCS"
.
variables_order =“EGPCS”。
After that, you would get the environment variables which you set before using $_ENV['varname'].
之后,您将获得在使用$ _ENV ['varname']之前设置的环境变量。
From php.ini about variables_order
:
从php.ini关于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[].
因此,您可以尝试使用getenv()而不是$ _ENV []。
#3
0
Recently i wrote a library to get values from environment variables and parse to the PHP data types. This library can be used to parse environment variables to PHP data types (like the casting to integer, float, null, boolean), parse the complex data structures like a JSON string and more with the contribution of the commnunity.
最近我写了一个库来从环境变量中获取值并解析为PHP数据类型。该库可用于将环境变量解析为PHP数据类型(如转换为整数,浮点数,空值,布尔值),解析复杂数据结构(如JSON字符串等)以及社区的贡献。
The library is available here: https://github.com/jpcercal/environment
该库可在此处获取:https://github.com/jpcercal/environment
Put your environment variables into "/etc/environment" and "/etc/apache2/envvars", after restart your Apache Server and load your environment variables to operational system:
在重新启动Apache Server并将环境变量加载到操作系统后,将环境变量放入“/ etc / environment”和“/ etc / apache2 / envvars”:
# source /etc/environment
# source /etc/apache2/envvars
And to get the values from environment variable (independently of the environment CLI, Apache, Nginx, PHP Built-in Server and more) to do it:
要从环境变量中获取值(独立于环境CLI,Apache,Nginx,PHP内置服务器等),请执行以下操作:
<?php
// ...
require "vendor/autoload.php";
// ...
var_dump(Cekurte\Environment\Environment::get("YOUR_ENV_VARIABLE_NAME"));
Enjoy it.
好好享受。
#4
-5
i think you can only use to set an environment variable from .htaccess:
我认为你只能用来设置.htaccess的环境变量:
SetEnv mohammad hello
PHP:
PHP:
print $_SERVER["mohammad"];
#1
48
I had exactly the same problem. To solve it, I just sourced /etc/environment
inside /etc/apache2/envvars
.
我有完全相同的问题。为了解决这个问题,我只在/ etc / apache2 / envvars中找到了/ etc / environment。
The content of /etc/environment
:
/ etc / environment的内容:
export MY_PROJECT_PATH=/var/www/my-project
export MY_PROJECT_ENV=production
export MY_PROJECT_MAIL=support@my-project.com
The content of /etc/apache2/envvars
:
/ 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.
现在,我可以在Apache Virtual Host配置文件和PHP中使用这些变量。
Here's an example of an Apache virtual host:
以下是Apache虚拟主机的示例:
<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访问它们的示例:
<?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";
?>
#2
1
On ubuntu, PHP uses different ini files for regular and CLI processes.
在ubuntu上,PHP对常规和CLI进程使用不同的ini文件。
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
应该有很少的ini文件,如/etc/php5/cli/php.ini,/etc/php5/fpm/php.ini或/etc/php5/php.ini。打开相关的INI文件并更改
variables_order = "GPCS"
variables_order =“GPCS”
line to
到
variables_order = "EGPCS"
.
variables_order =“EGPCS”。
After that, you would get the environment variables which you set before using $_ENV['varname'].
之后,您将获得在使用$ _ENV ['varname']之前设置的环境变量。
From php.ini about variables_order
:
从php.ini关于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[].
因此,您可以尝试使用getenv()而不是$ _ENV []。
#3
0
Recently i wrote a library to get values from environment variables and parse to the PHP data types. This library can be used to parse environment variables to PHP data types (like the casting to integer, float, null, boolean), parse the complex data structures like a JSON string and more with the contribution of the commnunity.
最近我写了一个库来从环境变量中获取值并解析为PHP数据类型。该库可用于将环境变量解析为PHP数据类型(如转换为整数,浮点数,空值,布尔值),解析复杂数据结构(如JSON字符串等)以及社区的贡献。
The library is available here: https://github.com/jpcercal/environment
该库可在此处获取:https://github.com/jpcercal/environment
Put your environment variables into "/etc/environment" and "/etc/apache2/envvars", after restart your Apache Server and load your environment variables to operational system:
在重新启动Apache Server并将环境变量加载到操作系统后,将环境变量放入“/ etc / environment”和“/ etc / apache2 / envvars”:
# source /etc/environment
# source /etc/apache2/envvars
And to get the values from environment variable (independently of the environment CLI, Apache, Nginx, PHP Built-in Server and more) to do it:
要从环境变量中获取值(独立于环境CLI,Apache,Nginx,PHP内置服务器等),请执行以下操作:
<?php
// ...
require "vendor/autoload.php";
// ...
var_dump(Cekurte\Environment\Environment::get("YOUR_ENV_VARIABLE_NAME"));
Enjoy it.
好好享受。
#4
-5
i think you can only use to set an environment variable from .htaccess:
我认为你只能用来设置.htaccess的环境变量:
SetEnv mohammad hello
PHP:
PHP:
print $_SERVER["mohammad"];