How do I pass env variables to uwsgi?
Asked Answered
J

4

6

I have a WSGI handler configured under Apache and I'm defining some environmental variables in the Apache virtual host configuration.

SetEnv APP_CONFIG "/var/lib/myapp/app.config"
SetEnv LOG_CONFIG "/var/lib/myapp/app.logging.yml"

To test the handler in development without having to install and configure Apache I'm using uWSGI with the --http option.

uwsgi --http :9090 --ini uwsgi.ini --wsgi-file wsgi.py

wsgi.py

def application(environ, start_response):
    config_file_path = environ['APP_CONFIG']
    start_response('200 OK', [('Content-Type','text/html')])
    return ["Hello World"]

Using the uWSGI http server how can I pass these variables to my application as part of the environ argument?

I have tried setting environmental variables in the uwsgi.ini file:

[uwsgi]
env = APP_CONFIG="/var/lib/myapp/app.config"

but I get:

File "wsgi.py", line 5, in application
config_file_path = environ['APP_CONFIG']
KeyError: 'APP_CONFIG'
Jobina answered 2/5, 2019 at 17:1 Comment(0)
S
7
[uwsgi]
env             = RAY_REDIS_PASS=ray_pass
env             = RAY_REDIS_PORT=6380
strict          = true
chdir           = /Users/judas/projects/myapp
master-fifo     = /tmp/myapp_fifo0
master-fifo     = /tmp/myapp_fifo1
module          = myapp.wsgi:application
master          = true
vacuum          = true
need-app        = true
processes       = 4
die-on-term     = true
procname-prefix = myapp
harakiri        = 30
socket          = /tmp/myapp_uwsgi.sock
lazy-apps       = true
logger          = file:logfile=/tmp/apps.log,maxsize=2000000000
import          = postfork
Streamy answered 24/2, 2020 at 19:50 Comment(1)
broken link, and not at wayback machine. do you have another that works? thanks!Sid
F
3

As pointed in https://mcmap.net/q/1069678/-django-ignoring-environment-variables-when-run-via-uwsgi do not use whitespaces:

[uwsgi]
env=MY_VAR_NAME=foobar
Fijian answered 5/1, 2021 at 14:27 Comment(1)
This is quite odd since INI files' key values don't normally require spaces. Does this mean only using the env option requires omitting white space?Valdis
Y
1

I think you just need to specify you .ini file

uwsgi --ini uwsgi.ini --http = :9090 --wsgi-file wsgi.py
Yount answered 2/5, 2019 at 18:36 Comment(2)
Thanks, I was doing that and I have edited the question to make it clearer.Jobina
@Jobina Sorry my bad. Can you post your entire WSGI.py file.Yount
J
1

I discovered how to do this using addvar in the wsgi.ini:

[uwsgi]
route-run = addvar:APP_CONFIG="/var/lib/myapp/app.config"
Jobina answered 3/5, 2019 at 17:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.