Why are Python environment variables different whe

2020-03-31 06:30发布

I have the following executable python script:

#!/usr/bin/python

import os
print os.environ

when I execute it from the command line as root, I get the following:

{
    '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': '...'
}

But when I run this from a php script (running on nginx server) and echo back the result I get:

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

Why are they different? Specifically, how can I get the web version to include the 'PATH' variable?

标签: php python nginx
1条回答
太酷不给撩
2楼-- · 2020-03-31 06:56

For apache, see 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.

If you are using Apache's FCGId, see 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.

For Nginx CGI, see the env setting.

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.

If you are using NgxWSGIModule, see the wsgi_var setting.

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.

If you are running your application under (gunicorn|tornado|twisted|etc)+supervisord, see the environment directive.

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.

If you are using something else, let me know.

You can always inject environment variables using os.environ - do it at the start of your (c|fc|ws)gi.py script. Se also sys.path:

sys.path.append('/usr/local/django')
查看更多
登录 后发表回答