如何避免将环境变量放置在Django,nginx和uWSGI的多个地方? [英] How to avoid putting environment variables into multiple places with Django, nginx and uWSGI?

查看:118
本文介绍了如何避免将环境变量放置在Django,nginx和uWSGI的多个地方?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试配置nginx + uWSGI来为我的Django应用程序提供服务。



当我将环境变量放入 myapp_uwsgi.ini 时:

  uid =用户名
gid =用户名
env = DJANGO_SITE_KEY =...

它按预期工作。



但是,我的应用有一些管理命令,它们也可以访问我定义的环境变量。



如果我将环境变量放到 /home/username/.bashrc

  export DJANGO_SITE_KEY =...

uWSGI不加载它们。



我已经尝试将环境变量放在一个单独的文件中:

 #!/ bin / sh 
export DJANGO_SITE_KEY =...

然后从bo调用它th .bashrc

 。 / home / username / environment 

myapp_uwsgi.ini

  exec-pre-app =。 / home / username / environment 

在uWSGI日志中,我看到这一行:

  running。/ home / username / environment(pre app)... 

但是我的Django应用程序无法使用 os.environ 访问环境变量。 / p>

我还尝试将 export 命令放在预激活 virtualenvwrapper的钩子,并使用uWSGI的 virtualenv = 设置,但它也不起作用(我假设钩子仅在使用virtualenvwrapper命令时执行,如 workon

解决方案

这里是答案来自uWSGI开发人员:


只要放置每个(一行一行)在文本文件中的形式



VAR = VALUE



然后在uWSGI c onfig

  [uwsgi] 
for-readline = yourfile
env =%(_)
endfor =


这也适用于yml配置文件: p>

  for-readline:filename 
env:%(_)
endfor:


I am trying to configure nginx+uWSGI to serve my Django application.

When I put environment variables into myapp_uwsgi.ini:

uid = username
gid = username
env = DJANGO_SITE_KEY="..."

it works as expected.

However, my app has some management commands which should also have access to the environment variables I have defined.

If I put the environment variables to /home/username/.bashrc:

export DJANGO_SITE_KEY="..."

uWSGI does not load them.

I have tried to put the environment variables into a separate file:

#!/bin/sh
export DJANGO_SITE_KEY="..."

and then call it from both .bashrc:

. /home/username/environment

and myapp_uwsgi.ini:

exec-pre-app = . /home/username/environment

In uWSGI logs I see this line:

running ". /home/username/environment" (pre app)...

But my Django app is unable to access the environment variables with os.environ.

I have also tried putting the export commands to the preactivate hook of virtualenvwrapper and use the virtualenv = setting of uWSGI, but it does not work too (I assume the hooks are only executed when using virtualenvwrapper commands like workon.

解决方案

Here is the answer from uWSGI developers:

just place each of them (one per line) in a text file in the form

VAR=VALUE

then in uWSGI config

[uwsgi]
for-readline = yourfile
  env = %(_)
endfor =

This also works with yml config files:

  for-readline: filename
    env: %(_)
  endfor:

这篇关于如何避免将环境变量放置在Django,nginx和uWSGI的多个地方?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆