php 使用Apache时设置PHP访问的环境变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10902433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:18:57  来源:igfitidea点击:

Setting environment variables for accessing in PHP when using Apache

phpapacheubuntuenvironment-variables

提问by Abishek

I have a Linux environment and I have a PHP Web Application that conditionally runs based on environment variables using getenvin PHP. I need to know how these environment variables need to be set for the application to work correctly. I am not sure how to set this up on Apache.

我有一个 Linux 环境,我有一个 PHP Web 应用程序,它根据getenvPHP 中使用的环境变量有条件地运行。我需要知道如何设置这些环境变量才能使应用程序正常工作。我不确定如何在 Apache 上进行设置。

Also, I need to be able to configure separate environment variables for each domain separately.

此外,我需要能够分别为每个域配置单独的环境变量。

Please advice on how can I achieve this.

请就我如何实现这一目标提出建议。

回答by wroniasty

Something along the lines:

沿线的东西:

<VirtualHost hostname:80>
   ...
   SetEnv VARIABLE_NAME variable_value
   ...
</VirtualHost>

回答by paquettg

You can also do this in a .htaccess file assuming they are enabled on the website.

您也可以在 .htaccess 文件中执行此操作,假设它们已在网站上启用。

SetEnv KOHANA_ENV production

Would be all you need to add to a .htaccess to add the environment variable

将是您需要添加到 .htaccess 以添加环境变量的全部内容

回答by danday74

Unbelievable, but on httpd 2.2 on centos 6.4 this works.

令人难以置信,但是在 centos 6.4 上的 httpd 2.2 上这是有效的。

Export env vars in /etc/sysconfig/httpd

导出环境变量 /etc/sysconfig/httpd

export mydocroot=/var/www/html

Then simply do this...

然后简单地做这个......

<VirtualHost *:80>
  DocumentRoot ${mydocroot}
</VirtualHost>

Then finally....

然后终于……

service httpd restart;

回答by Sachin Raghav

If your server is Ubuntu and Apache version is 2.4

如果您的服务器是 Ubuntu 并且 Apache 版本是 2.4

Server version: Apache/2.4.29 (Ubuntu)

服务器版本:Apache/2.4.29 (Ubuntu)

Then you export variables in "/etc/apache2/envvars" location.

然后在“/etc/apache2/envvars”位置导出变量。

Just like this below line, you need to add an extra line in "/etc/apache2/envvars" export GOROOT=/usr/local/go

就像下面这行一样,您需要在“/etc/apache2/envvars”中添加额外的一行 export GOROOT=/usr/local/go