Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid putting environment variables into multiple places with Django, nginx and uWSGI?

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.

like image 869
utapyngo Avatar asked May 15 '14 06:05

utapyngo


1 Answers

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:
like image 161
utapyngo Avatar answered Oct 23 '22 17:10

utapyngo