gpt4 book ai didi

php - 为什么从 Web 服务器执行时 Python 环境变量不同?

转载 作者:行者123 更新时间:2023-11-30 23:29:03 25 4
gpt4 key购买 nike

我有以下可执行 python 脚本:

#!/usr/bin/python

import os
print os.environ

当我以 root 身份从命令行执行它时,我得到以下信息:

{
'LANG': 'en_US.UTF-8',
'TERM': 'xterm-256color',
'SHELL': '/bin/bash',
'LESSCLOSE': '/usr/bin/lesspipe %s %s',
'LANGUAGE': 'en_US:en',
'SHLVL': '1',
'SSH_TTY': '/dev/pts/0',
'OLDPWD': '/var/www/bais-mordechai-laravel',
'COMP_WORDBREAKS': ' \t\n"\'><;|&(:',
'PWD': '/var/www/bais-mordechai-laravel/public',
'LESSOPEN': '| /usr/bin/lesspipe %s',
'SSH_CLIENT': '71.205.188.8 56489 22',
'LOGNAME': 'root',
'USER': 'root',
'PATH': '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games',
'MAIL': '/var/mail/root',
'LS_COLORS':'...',
'HOME': '/root',
'_': './pythontest',
'SSH_CONNECTION': '...'
}

但是当我从 php 脚本(在 nginx 服务器上运行)运行它并回显我得到的结果时:

{
'HOME': '/var/www',
'PWD': '/var/www/bais-mordechai-laravel/public',
'USER': 'www-data'
}

为什么它们不同?具体来说,如何让网络版本包含“PATH”变量?

最佳答案

对于 apache,请参阅 mod_setenv

This module allows for control of internal environment variables that are used by various Apache HTTP Server modules. These variables are also provided to CGI scripts as native system environment variables, and available for use in SSI pages. Environment variables may be passed from the shell which invoked the httpd process. Alternatively, environment variables may be set or unset within the configuration process.

如果您使用 Apache 的 FCGId,请参阅 FcgidInitialEnv

Use FcgidInitialEnv to define environment variables to pass to the FastCGI application. This directive can be used multiple times.

This setting will apply to all applications spawned for this server or virtual host. Use FcgidCmdOptions to apply this setting to a single application.

对于 Nginx CGI,请参阅 env 设置。

By default, nginx removes all environment variables inherited from its parent process except the TZ variable. This directive allows preserving some of the inherited variables, changing their values, or creating new environment variables.

如果您使用 NgxWSGIModule,请参阅 wsgi_var 设置。

Directive assigns the variable, which will be added to theenvironment dictionary passed to the WSGI application.It is possible to use strings, nginx variables and theircombination as values. Directives not set are inherited from theouter level.

如果您在 (gunicorn|tornado|twisted|etc)+supervisord 下运行应用程序,请参阅 environment 指令。

A list of key/value pairs in the form KEY="val",KEY2="val2" that will be placed in the supervisord process’ environment (and as a result in all of its child process’ environments). This option can include the value %(here)s, which expands to the directory in which the supervisord configuration file was found. Values containing non-alphanumeric characters should be quoted (e.g. KEY="val:123",KEY2="val,456"). Otherwise, quoting the values is optional but recommended. Note that subprocesses will inherit the environment variables of the shell used to start supervisord except for the ones overridden here and within the program’s environment option.

如果您使用其他东西,请告诉我。

您始终可以使用 os.environ 注入(inject)环境变量 - 在 (c|fc|ws)gi.py 脚本的开头执行此操作。另请参阅 sys.path:

sys.path.append('/usr/local/django')

关于php - 为什么从 Web 服务器执行时 Python 环境变量不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21324027/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com