Apache Environment Variables

279 views
Skip to first unread message

Dashdrum

unread,
Dec 16, 2009, 1:58:34 PM12/16/09
to Django users
I'm using mod_wsgi to host my Django site, and all is working well,
except...

I'd like to access an environment variable set in the Apache
configuration like so:

SetEnv TIER dev

The os.environ.get() function seems appropriate for this, but no
luck. Using this code:


PATH = os.environ.get('PATH','')
DJANGO_SETTINGS_MODULE = os.environ.get
('DJANGO_SETTINGS_MODULE','')
SERVER_NAME = os.environ.get('SERVER_NAME','')
TIER = os.environ.get('TIER','')

PATH comes from the operating system (Red Hat) and the proper value is
returned. Likewise, DJANGO_SETTINGS_MODULE returns the correct string
after it is set in django.wsgi.

However, both SERVER_NAME and the custom variable TIER are not
returned.

Any advice would be most appreciated.

Dan Gentry

Graham Dumpleton

unread,
Dec 16, 2009, 2:58:09 PM12/16/09
to Django users
You need to use the Django request object to get access to the WSGI
environ variable set and look them up there. When using SetEnv with
mod_wsgi, they are not pushed into os.environ. Although mod_python
does allow one to push variables into os.environ from Apache
configuration, that is arguably broken and can cause various problems.

So, from memory use:

request.META['TIER']

Graham

On Dec 17, 5:58 am, Dashdrum <dgen...@gmail.com> wrote:
> I'm usingmod_wsgito host my Django site, and all is working well,

fivegrainja

unread,
Dec 16, 2009, 2:52:45 PM12/16/09
to Django users
I wrestled with this same issue a few days ago. The section
"Application Configuration" on this page provides some guidance:

http://code.google.com/p/modwsgi/wiki/ConfigurationGuidelines

In a nutshell, the apache SetEnv isn't setting values in the process
environment that os.environ represents. Instead SetEnv is setting
values in the context of the WSGI request. Within your code you can
reference that context at request.environ:

def myView(request):
tier = request.environ['TIER']

Hope that helps,
Jason Stevens

Dashdrum

unread,
Dec 17, 2009, 2:27:45 PM12/17/09
to Django users
Thanks to both responders for the explanations.

Dan

Info Cascade

unread,
Dec 17, 2009, 11:57:45 PM12/17/09
to django...@googlegroups.com
Dan,

This works for me -- using mod_python.

VirtualHost defininition:
> SetEnv VIRTUAL_HOST_NAME dev.hostname.com
> SetEnv DATABASE_NAME dbname
settings.py:
> VIRTUAL_HOST_NAME = os.environ.get('VIRTUAL_HOST_NAME')
> DATABASE_NAME = os.environ.get('DATABASE_NAME')
Liam

> --
>
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>
>
>
>

Graham Dumpleton

unread,
Dec 18, 2009, 12:05:21 AM12/18/09
to Django users

On Dec 18, 3:57 pm, Info Cascade <informationcasc...@gmail.com> wrote:
> Dan,
>
> This works for me -- using mod_python.
>
> VirtualHost defininition:> SetEnv VIRTUAL_HOST_NAME dev.hostname.com
> >     SetEnv DATABASE_NAME dbname
> settings.py:
> > VIRTUAL_HOST_NAME = os.environ.get('VIRTUAL_HOST_NAME')
> > DATABASE_NAME = os.environ.get('DATABASE_NAME')

That is mod_python. You cannot do that with mod_wsgi and for the good
reason that what the Django mod_python adapter did in that respect was
flawed because of the problems it can cause. You are ill advised to
rely on that behaviour in the Django mod_python adapter as it isn't
portable and may well bite you one day. I'd very much suggest you not
recommend it to people unless you understand the problems it can cause
and can explain properly to others what those problems are.

Graham

Reply all
Reply to author
Forward
0 new messages