configuring wsgi app from apache wsgi script

2 views
Skip to first unread message

Aljosa Mohorovic

unread,
Nov 26, 2009, 1:34:00 PM11/26/09
to modwsgi
short intro:
- i'm talking about django project but use case should be relevant for
any wsgi app
- i'm using virtualenv/pip to install packages from private pypi
(python libraries, django apps, ...) for this project
- the only thing that isn't installed via virtualenv/pip is django
project (settings.py, urls.py and similar), it's updated from source
repository (using git)

my goal:
1) create python package for django project and install it via pip
(this also removes any git usage in project deployment)
2) configure django project/wsgi app via apache wsgi script

#1 is pretty simple so no questions about that.

my solution for #2 is to define in <PROJECT NAME>.wsgi:
----------------------------------------
PROJECT_SETTINGS = {
'PROJECT_ROOT': PROJECT_ROOT,
'DATABASE_ENGINE': 'sqlite3',
'DATABASE_NAME': os.path.join(PROJECT_ROOT, 'dev.db'),
}

for k,v in PROJECT_SETTINGS.iteritems():
os.environ["DJANGO_%s" % k] = v
os.environ['DJANGO_SETTINGS_MODULE'] = 'wsgi_app.settings'
----------------------------------------

and then handle this in wsgi app (in my case django project
settings.py) using something like:
----------------------------------------
for k,v in os.environ.iteritems():
if k != "DJANGO_SETTINGS_MODULE" and k.startswith("DJANGO_"):
key = k[7:]
locals()[k] = v
----------------------------------------

can you recommend a better way? any ideas/tips are appreciated.

Aljosa Mohorovic

p.s.
feel free to tell me i'm an idiot, i can handle it. just recommend a
better way to do this...

Graham Dumpleton

unread,
Nov 26, 2009, 5:11:37 PM11/26/09
to mod...@googlegroups.com
2009/11/27 Aljosa Mohorovic <aljosa.m...@gmail.com>:
Using environment variables for arbitrary configuration is a bad idea.
To understand some of the problems with doing that in Python in multi
interpreter process environment read:

http://code.google.com/p/modwsgi/wiki/ApplicationIssues#Application_Environment_Variables

What you could instead do is:

import os

import wsgi_app.settings as settings

settings.PROJECT_ROOT = PROJECT_ROOT
settings.DATABASE_ENGINE = 'sqlite3',
settings.DATABASE_NAME = os.path.join(PROJECT_ROOT, 'dev.db')

os.environ['DJANGO_SETTINGS_MODULE'] = 'wsgi_app.settings'

In other words, have WSGI script file import your baseline settings
file and then override the values in it from WSGI script file by way
of assignment. Then set DJANGO_SETTINGS_MODULE as per normal etc.

Dangers of this are that you might have password database information
in WSGI script file. Technically WSGI script file is going to be
marked as a file that Apache can serve up to clients. Normally it
doesn't serve the content and instead the content is interpreted by
mod_wsgi, but stuff up your Apache configuration bad and you
inadvertently provide a way of it being able to be requested by a
client as a static file.

As such, always better to keep sensitive configuration information out
of the WSGI script file. If you are confident in your ability to
configure Apache, then no problems.

BTW, what most would do is have multiple settings files in project,
ie., development_settings.py, production_settings.py, etc. These would
do 'from settings import *' then they would override values. The
DJANGO_SETTINGS_MODULE for production would then specify
'wsgi_app.production_settings'.

Graham
Reply all
Reply to author
Forward
0 new messages