在linux中重新初始化系统范围的环境变量

时间:2022-03-22 22:41:24

I just want my apache to register some of my predefined environment so that i can retrieve it using getenv function in php. How can i do this? I tried adding /etc/profile.d/foo.sh with export FOO=/bar/baz using root and restarted apache.

我只是希望我的apache注册我的一些预定义环境,以便我可以使用php中的getenv函数检索它。我怎样才能做到这一点?我尝试使用root并重新启动apache,使用export FOO = / bar / baz添加/etc/profile.d/foo.sh。

4 个解决方案

#1


6  

Environment variables are inherited by processes in Unix. The files in /etc/profile.d are only executed (in the current shell, not in a subshell) when you log in. Just changing the value there and then restarting a process will not update the environment.

环境变量由Unix中的进程继承。 /etc/profile.d中的文件仅在您登录时执行(在当前shell中,而不是在子shell中)。只需更改其中的值然后重新启动进程将不会更新环境。

Possible Fixes:

  • log out/log in, then start apache
  • 注销/登录,然后启动apache

  • source the file: # . /etc/profile.d/foo.sh, then restart apache
  • 源文件:#。 /etc/profile.d/foo.sh,然后重启apache

  • source the file in the apache init script
  • 在apache init脚本中获取文件

You also need to make sure that /etc/profile.d/ is sourced when Apache is started by init rather than yourself.

您还需要确保在由init而不是您自己启动Apache时获取/etc/profile.d/。

The best fix might also depend on the distribution you are using, because they use different schemes for configuration.

最佳修复还可能取决于您使用的分发,因为它们使用不同的配置方案。

#2


0  

You can use SetEnv in your config files (/etc/httpd/conf.d/*.conf, .htaccess ...). Additionally you should be able to define them in /etc/sysconfig/httpd (on RPM-based distribs) and export them (note: not tested).

您可以在配置文件中使用SetEnv(/etc/httpd/conf.d/*.conf,.htaccess ...)。此外,您应该能够在/ etc / sysconfig / httpd(在基于RPM的发布器上)定义它们并导出它们(注意:未经测试)。

Note: it wouldn't surprise me if some distributions tried quite hard to hide as much as possible, as far as system config is concerned, from a publically accessible service such as Apache. And if they don't, they might start doing this in a future version. Hence I advise you to do this explicitly. If you need to share such a setting between Apache and your shells, you could try sourcing /etc/profile.d/yourprofile.sh from /etc/sysconfig/httpd

注意:如果涉及系统配置,从一个公共可访问的服务(如Apache)中尽可能地尽可能地隐藏某些发行版,我不会感到惊讶。如果他们不这样做,他们可能会在未来的版本中开始这样做。因此我建议你明确地这样做。如果你需要在Apache和shell之间共享这样的设置,你可以尝试从/ etc / sysconfig / httpd中获取/etc/profile.d/yourprofile.sh

#3


0  

Apache config files allow you to set environment variables on a per site basis.

Apache配置文件允许您基于每个站点设置环境变量。

So if your web server is serving pages from two logical sites you can have the same environment variable set differently for each site and thus get your PHP to react differently.

因此,如果您的Web服务器正在为来自两个逻辑站点的页面提供服务,则可以为每个站点设置不同的环境变量,从而让您的PHP做出不同的反应。

See the Apache mod_env for details:

有关详细信息,请参阅Apache mod_env:

#4


0  

If you need env vars for Apache only, what worked for me was editing the /etc/apache2/envvars and restart of Apache. I added these settings:

如果你只需要用于Apache的env vars,对我来说有用的是编辑/ etc / apache2 / envvars并重新启动Apache。我添加了以下设置:

export LANG='en_US.UTF-8'
export LC_ALL='en_US.UTF-8'

#1


6  

Environment variables are inherited by processes in Unix. The files in /etc/profile.d are only executed (in the current shell, not in a subshell) when you log in. Just changing the value there and then restarting a process will not update the environment.

环境变量由Unix中的进程继承。 /etc/profile.d中的文件仅在您登录时执行(在当前shell中,而不是在子shell中)。只需更改其中的值然后重新启动进程将不会更新环境。

Possible Fixes:

  • log out/log in, then start apache
  • 注销/登录,然后启动apache

  • source the file: # . /etc/profile.d/foo.sh, then restart apache
  • 源文件:#。 /etc/profile.d/foo.sh,然后重启apache

  • source the file in the apache init script
  • 在apache init脚本中获取文件

You also need to make sure that /etc/profile.d/ is sourced when Apache is started by init rather than yourself.

您还需要确保在由init而不是您自己启动Apache时获取/etc/profile.d/。

The best fix might also depend on the distribution you are using, because they use different schemes for configuration.

最佳修复还可能取决于您使用的分发,因为它们使用不同的配置方案。

#2


0  

You can use SetEnv in your config files (/etc/httpd/conf.d/*.conf, .htaccess ...). Additionally you should be able to define them in /etc/sysconfig/httpd (on RPM-based distribs) and export them (note: not tested).

您可以在配置文件中使用SetEnv(/etc/httpd/conf.d/*.conf,.htaccess ...)。此外,您应该能够在/ etc / sysconfig / httpd(在基于RPM的发布器上)定义它们并导出它们(注意:未经测试)。

Note: it wouldn't surprise me if some distributions tried quite hard to hide as much as possible, as far as system config is concerned, from a publically accessible service such as Apache. And if they don't, they might start doing this in a future version. Hence I advise you to do this explicitly. If you need to share such a setting between Apache and your shells, you could try sourcing /etc/profile.d/yourprofile.sh from /etc/sysconfig/httpd

注意:如果涉及系统配置,从一个公共可访问的服务(如Apache)中尽可能地尽可能地隐藏某些发行版,我不会感到惊讶。如果他们不这样做,他们可能会在未来的版本中开始这样做。因此我建议你明确地这样做。如果你需要在Apache和shell之间共享这样的设置,你可以尝试从/ etc / sysconfig / httpd中获取/etc/profile.d/yourprofile.sh

#3


0  

Apache config files allow you to set environment variables on a per site basis.

Apache配置文件允许您基于每个站点设置环境变量。

So if your web server is serving pages from two logical sites you can have the same environment variable set differently for each site and thus get your PHP to react differently.

因此,如果您的Web服务器正在为来自两个逻辑站点的页面提供服务,则可以为每个站点设置不同的环境变量,从而让您的PHP做出不同的反应。

See the Apache mod_env for details:

有关详细信息,请参阅Apache mod_env:

#4


0  

If you need env vars for Apache only, what worked for me was editing the /etc/apache2/envvars and restart of Apache. I added these settings:

如果你只需要用于Apache的env vars,对我来说有用的是编辑/ etc / apache2 / envvars并重新启动Apache。我添加了以下设置:

export LANG='en_US.UTF-8'
export LC_ALL='en_US.UTF-8'