Skip to content

Commit

Permalink
Fixed #25203 -- Documented how to pass Apache environment variables t…
Browse files Browse the repository at this point in the history
…o Django.
  • Loading branch information
paulrentschler authored and timgraham committed Sep 11, 2015
1 parent e3720b9 commit 47016d4
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions docs/howto/deployment/wsgi/modwsgi.txt
Expand Up @@ -125,6 +125,47 @@ mode`_.

.. _details on setting up daemon mode: http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide#Delegation_To_Daemon_Process

Apache environment variables
============================

If you want to specify a different Django settings file or additional variables
for your Django application via the Apache configuration, you would do it like
this:

.. code-block:: apache

SetEnv DB_USER dbusername
SetEnv DJANGO_SETTINGS_MODULE mysite.alternate-settings

The ``SetEnv`` directive creates Apache environment variables instead of OS
environment variables, so you will need to replace the default ``wsgi.py`` file
with::

import os

from django.core.wsgi import get_wsgi_application

# A tuple of Apache environment variables to pass through to Django.
env_variables_to_pass = ('DB_USER', )

def application(environ, start_response):
"""
Wrapper for the WSGI application that passes environment variables.
"""
os.environ['DJANGO_SETTINGS_MODULE'] = environ.get('DJANGO_SETTINGS_MODULE', 'mysite.settings')
for var in env_variables_to_pass:
os.environ[var] = environ.get(var, '')
return get_wsgi_application()(environ, start_response)

Now you can specify a new settings file in Apache using the
``SetEnv DJANGO_SETTINGS_MODULE ...`` line, and if that setting isn't
specified, it uses ``'mysite.settings'`` as a default. You'll want to change
``mysite.settings`` to reference your own ``settings.py`` module.

Additionally, you can use the ``env_variables_to_pass`` tuple to specify any
other Apache environment variables, such as database login credentials, that
should be passed along to the Django application as OS environment variables.

.. _serving-files:

Serving files
Expand Down

3 comments on commit 47016d4

@apollo13
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this certainly works to some extend I'd like to know what @GrahamDumpleton thinks about this. IIRC he has a blog post advicing against this pattern.

@carljm
Copy link
Member

@carljm carljm commented on 47016d4 Sep 11, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pattern is fundamentally weird, and I don't really like documenting it. WSGI environ vars are per-request, it's strange and inefficient to re-set process-global OS environ vars based on them on every single request. I also wonder (but am not sure; Graham would know) if this technique could cause strange race conditions if you fail to use daemon mode with a separate process group per site, because it would actually be changing the environ for all your sites on each request to any of the sites.

IMO the right way to set environment variables in Apache is the envvars file. That doesn't work if you're serving multiple Django sites from a single Apache instance. In that case I think it's better to just direct each site to a different WSGI script file, which sets the env vars it needs once at startup to fixed values.

@timgraham
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted in 83ea3bc.

Please sign in to comment.