本文介绍了php无法访问环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 php 中访问操作系统环境变量时遇到问题我在 centos 6.3 映像上安装了 apache/php

Im having issues accessing OS environment variables in phpI have apache/php installed on a centos 6.3 image

在 httpd.conf mod mod_env.so 中加载在 php.ini 我设置 variables_order = "EGPCS"重新启动 httpd(多次)

in httpd.conf mod mod_env.so is loadedin php.ini I have set variables_order = "EGPCS"restarted httpd (many times)

如果我在 shell 中输入env",我会得到

in shell if I type "env" I get

DB_PORT_28017_TCP_PROTO=tcp
HOSTNAME=c6188a8bd77f
DB_NAME=/rockmongo/db
DB_PORT_27017_TCP=tcp://172.17.0.36:27017
TERM=xterm
DB_PORT_28017_TCP_PORT=28017
DB_PORT=tcp://172.17.0.36:27017
DB_PORT_27017_TCP_PORT=27017
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/etc/php.d
DB_PORT_27017_TCP_PROTO=tcp
DB_PORT_28017_TCP_ADDR=172.17.0.36
DB_PORT_28017_TCP=tcp://172.17.0.36:28017
SHLVL=1
HOME=/
DB_PORT_27017_TCP_ADDR=172.17.0.36
container=lxc
_=/usr/bin/env
OLDPWD=/etc

后面有变量,但是如果我在 php 中执行 print_r($_ENV); 我得到

which has the variables im after, however if I execute print_r($_ENV); in php I get

Array ( [TERM] => xterm [PATH] => /sbin:/usr/sbin:/bin:/usr/bin [PWD] => / [LANG] => C [SHLVL] => 2 [_] => /usr/sbin/httpd )

还查看了 $_SERVER &$全球.

have also looked in $_SERVER & $GLOBALS.

有趣的是,如果我在 shell 中执行 php -i 我会看到 _ENV 中正确设置了 env 变量

Interestingly if I execute php -i in shell I see the env variables set correctly in _ENV

我应该注意到我在 docker 容器中运行它,但是我不认为这是一个问题,因为变量在 #env & 中正确显示#php -i.我认为我的 httpd/php 配置有问题

I should note im running this in a docker container, however I dont believe it is a issue as variables display correctly in #env & #php -i. I think I have a issue with my httpd/php config

有人对此有什么建议吗?谢谢

Anyone have advice for this?thanks

推荐答案

我最终有几个选择

  1. 如果 docker 容器需要运行多个服务,将环境变量设置为/etc/environment 将使它们可供所有用户使用.我将以下行添加到我的 Dockerfile CMD

  1. if docker container needs to run multiple services, setting env vars to /etc/environment will make them available for all users. I added the following line to my Dockerfile CMD

CMD ["env | grep _ >>/etc/environment"]

如果 docker 容器运行单个服务,最好将入口点设置为所需的应用程序,env vars 将自动传递给应用程序用户.这是我的 Dockerfile CDM &运行 apache 的入口点

if docker container runs a single service, its best to set the entry point to the desired application, env vars will automatically be passed to application user. this is my Dockerfile CDM & ENTRYPOINT to run apache

入口点 ["/usr/sbin/httpd"]CMD ["-D", "FOREGROUND"]

这篇关于php无法访问环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 11:39