Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apache, mod_wsgi, Enironment Variables #53

Closed
schacki opened this issue Feb 16, 2014 · 7 comments
Closed

Apache, mod_wsgi, Enironment Variables #53

schacki opened this issue Feb 16, 2014 · 7 comments

Comments

@schacki
Copy link

schacki commented Feb 16, 2014

I found the following comment "Environment variables for configuration (This won't work with Apache/mod_wsgi)." at pydanny's https://github.com/pydanny/cookiecutter-django.

Since this surprised me, I opened a ticket: cookiecutter/cookiecutter-django#74. Pydanny replied correctly, that any solution for that is outside of the scope of the django project template.

Now I fully understand that the root cause for this issue is NOT related to django-configurations. But as a result, the purpose of django-cofigurations (especially in combination with envdir) is somehow corrupted with mod_wsgi and Apache. So I am wondering, if we can come up with some kind of workaround for the problem.

The root cause is desciribed here: http://httpd.apache.org/docs/2.2/env.html. Grahame Dumpleton has commented on the issue at SO (last anwer): http://stackoverflow.com/questions/19754834/access-apache-setenv-variable-from-django-wsgi-py-file

One workaround is described the the same SO question or also in this post: http://drumcoder.co.uk/blog/2010/nov/12/apache-environment-variables-and-mod_wsgi/. Though Graham had a very clear negative opinion on that. Still would that be a valid option?

Are there any other options? If not so, I would at leat propose to add a section to the cookbook in the docs about mod_wsgi/Apache.

@jezdez
Copy link
Member

jezdez commented Feb 16, 2014

Hm, I wonder if you could simply move the issue from the server (Apache) into the application (wsgi.py). Couldn't you just use envdir's Python API to load the environment variables before Django initializes completely?
See my blog software for an example: https://github.com/jezdez/jezdez.com/blob/dc7f7bc765b68e285a0e68f026b74c20aaf0e4c6/jezdez/wsgi.py#L22

Note: the used envdir.read function is deprecated, use the envdir.open instead: http://envdir.readthedocs.org/en/latest/api.html#envdir.open

Would that help fix the issue in cookiecutter-django, @pydanny?

@schacki
Copy link
Author

schacki commented Feb 16, 2014

Well, I think as long as you do not want to control any settings from your virtualhost via environemt variables, you are fine. But what about the following situation:

You have 2 envdir directories, one for debugging, one for production. You now have 2 options to load these programmatically:
a) In your manage.py or wsgi.py you want to control der envdir directory to load based on a environment variable. This is basically what you are doing here:
path = parent / 'envs' / os.environ.get('ENVDIR', 'dev')

But this requires that you are able to set this variable in the virtualhost (or somehwere else) and retrieve this value in python somehow. Because otherwise you will always end upt with 'dev'. This wouled be possible based on the approach in SO, but Graham does not like it.
b) you have a dev_manage.py / prod_manage.py as well as a dev_wsgi.py /prod_wsgi.py and control the envdir directory to be opend this way. But this somehow is completely against the idea of django-configuraitons and envdir.

Is there an option c)?

@schacki
Copy link
Author

schacki commented Feb 17, 2014

So just checking where we are: do we agree on the problem? If so, would be be willing to get the Apache Env variables from the request (other than recommended by Graham)

@jezdez
Copy link
Member

jezdez commented Jan 6, 2015

I've just added support for .env files, a different strategy for storing environment variables. See 01e3f58

The bottom line is:

Set the DOTENV setting in your class to a file path that you want to let django-configurations read. They can differ per environment class of course. See http://ddollar.github.io/foreman/#ENVIRONMENT for more info. This is also used on Heroku as .env files during local development with Foreman.

@jezdez jezdez closed this as completed Jan 6, 2015
@pydanny
Copy link
Member

pydanny commented Jan 6, 2015

Since I see Apache, mod_wsgi, and environment variables are the title of this issue, it behooves me to point out a downstream issue that was opened for cookiecutter-django: cookiecutter/cookiecutter-django#160

@jezdez
Copy link
Member

jezdez commented Jan 7, 2015

@pydanny Thanks!

I'm a bit sad to see this issue being dragged between that many projects and that some people involved seem to be passive-aggressive about it. That said, it seems to be an issue for some users stuck on Apache so I'll take a look at how to best document it. Probably with the idea of having separate wsgi/*.py files to set the environment variables for each environment. It's an additional chore for the users but that's the gist of my experience with Apache anyway.

@pydanny
Copy link
Member

pydanny commented Jan 7, 2015

@jezdez, Sending you a private email.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants