PHP 和 Apache 环境变量 [英] PHP and Apache environment variables

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

问题描述

我想在启动时将 linux 中的环境变量传递给 apache.

I want to pass an environment variable in linux to apache on start up.

导出 MYVAR=5--我在命令行中定义了我的环境变量

export MYVAR=5 --I define my environment variable on the command line

PassEnv MYVAR--设置apache导入apache配置文件中的变量

PassEnv MYVAR --set apache to import the variable in apache config file

apachectl 重启--当我重新启动 apache 时,我没有收到错误消息.然而我注意到如果我没有在第一步中创建环境变量,我会收到一条警告消息,所以必须在这里工作

apachectl restart --when I restart apache I don't get an error message. However I have noticed that if I do not create the environment variable in my first step, I get a warning message, so must be working here

echo $_SERVER['MYVAR']--我尝试访问PHP中的环境变量,但未定义

echo $_SERVER['MYVAR'] --i try to access the environment variable within PHP but it is not defined

我观察到,如果我尝试 PassEnv 一个已经存在的环境变量(我自己没有创建),它可以正常工作.SetEnv 指令也可以正常工作.

I've observed that if I try to PassEnv an environment variable that already exits (one that I havn't created myself) it works fine. The SetEnv directive also works fine.

我真的很想将环境变量即时传递给 apache,而无需将其写入文件.非常感谢帮助.

I'd really like to pass an environment variable to apache on the fly without writing it in a file. Help much appreciated.

我使用 CentOS、PHP5 和 Apache2.

I'm using CentOS, PHP5 and Apache2.

谢谢.

更新如果我直接使用 httpd 调用 apache 启动而不使用 shell 脚本 apachectl,则似乎环境变量会被传递.我原以为导出"会将变量导出到 shell 脚本,不是吗?我不是 linux 大师,所以请原谅我缺乏知识.

update it seems the environment variable gets passed if i invoke the apache startup directly with httpd and not use apachectl which is a shell script. I would have thought that the "export" would have exported the variable to the shell script no? I am not a linux guru so excuse my lack of knowledge.

推荐答案

如果您希望将环境变量传递给 apache,您应该确保该环境变量是为 apache 运行的环境定义的.最简单的选择是将 export MYVAR=value 行添加到 envvars(应位于/etc/apache2)或启动 apache 的脚本(在/etc/init.d 中),以及在您的 apache 配置中添加您的 PassEnv MYVAR.

If you want the environment variable to be passed to apache, you should make sure that said environment variable is defined for the environment that apache is running in. To do that, the easiest option is to add an export MYVAR=value line to envvars (should be located in /etc/apache2) or the script that starts apache (in /etc/init.d), and add your PassEnv MYVAR where it's wanted in your apache configuration.

重新启动 apache 应该确保传递了 MYVAR 环境变量.AFAIK 您将无法在 apache 运行时更改此 var 的值...

Restarting apache should make sure the MYVAR environment variable is passed. AFAIK you won't be able to change the value of this var while apache is running though...

CentOS 的一些相关信息:http://php.dzone.com/news/inserting-variable-headers-apa&default=false&zid=159&browser=16&mid=0&refresh=0

Some relevant info for CentOS : http://php.dzone.com/news/inserting-variable-headers-apa&default=false&zid=159&browser=16&mid=0&refresh=0

这篇关于PHP 和 Apache 环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆