python设置执行服务环境变量_从网络服务器执行时,为什么Python环境变量不同?...

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?

解决方案

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')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值