I am trying to configure nginx+uWSGI to serve my Django application and avoid putting env vars into uWSGI config (since manage.py also needs them).
In uWSGI ini-file I have:
uid = username
gid = username
exec-pre-app = . /home/username/.env
(note that I tried to source the file, but I also tried exec-pre-app = /home/username/.env)
In /home/username/.env:
#!/bin/sh
export DJANGO_SITE_KEY="..."
In uWSGI logs I see this line:
running ". /home/username/.env" (pre app)...
But my Django app is unable to access the environment variable with os.environ.
Why it should ? this is not how environment variables work. They are per-process ad inherited by children, if a children set an env var the parent cannot see it. You need to set them in the uWSGI process itself to allow children to see them. (the hook setenv, or the env options will do the trick)
I understand. But I don't want to store the same env vars in multiple places. With the env settings, I will have to parse uWSGI config when running manage.py. I want to set env vars in one file and just source it from both uWSGI and manage.py runner.
Will hooks help me? I don't see the setenv hook in the docs.
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 =
Thank you!
What if I want the filename to be an externally configured variable?
The following does not work right now because how uWSGI parses config files.
[uwsgi]
print = ENVFILE: $(ENVFILE)
for-readline = $(ENVFILE)
env = %(_)
print = environment set: %(_)
endfor ==
Any ideas?
This just saved me a lot of headache. Thanks @unbit
This is a bit old, but does this solution work with globally set environment variables in /etc/environment ? EDIT : it works indeed, but is this a good way to do it ?
Since manage.py commands uses the variables defined here this would be the only place where they're stored ?
Wouldn't it be better to simply have a directive like this:
env_file = /path/to/env/file
And using this, all the env vars are loaded.
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 doesn鈥檛 work when you have some variables with quoted value (e.g. FOO="lorem ipsum") or comments in the vars file. So it鈥檚 really not a proper solutoin.
try something like this
for line in `cat your_env_file`; do if [[ $line != \#* ]];then export $line; fi;done
I'm trying to import my .env file, and the examples above havent helped, can some one please explain to me how to bring in those variables stored there so i can access them when running in uwsgi with nginx?
Most helpful comment
just place each of them (one per line) in a text file in the form
VAR=VALUE
then in uWSGI config