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

我有以下可执行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 the
environment dictionary passed to the WSGI application.
It is possible to use strings, nginx variables and their
combination as values. Directives not set are inherited from the
outer level.

如果在(gunicorn | tornado | twisted | etc)监督下运行应用程序,请参见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注入环境变量-在(c | fc | ws)gi.py脚本的开头执行此操作.硒也sys.path

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

转载注明原文:从Web服务器执行时,为什么Python环境变量不同? - 代码日志