Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
386 views
in Technique[技术] by (71.8m points)

python - Django + mod_wsgi. Set OS environment variable from Apache's SetEnv

I need to split Django's development and production settings. I decided that if USKOVTASK_PROD variable is set, then app should use production settings. I read this article and tried to do it.

My snippets:

/etc/apache2/sites-enabled/uskovtask.conf:

<VirtualHost *:80>

ServerName uskovtask.*.com
ServerAlias uskovtask.*.com
DocumentRoot /mnt/ebs/uskovtask


Alias /static /mnt/ebs/uskovtask/static/
<Directory /mnt/ebs/uskovtask/static>
    Require all granted
</Directory>

#WSGIPythonPath /mnt/ebs/uskovtask
WSGIDaemonProcess uskovtask.*.com python-path=/mnt/ebs/uskovtask:/usr/lib/python2.7/site-packages
WSGIProcessGroup uskovtask.*.com
WSGIScriptAlias / /mnt/ebs/uskovtask/uskovtask/wsgi.py
SetEnv USKOVTASK_PROD 1


<Directory /mnt/ebs/uskovtask/uskovtask>
<Files wsgi.py>
    Require all granted
</Files>
</Directory>

</VirtualHost>

wsgi.py:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "uskovtask.settings")

from django.core.wsgi import get_wsgi_application

_application = get_wsgi_application()

def application(environ, start_response):
    if 'USKOVTASK_PROD' in environ:
        os.environ.setdefault('USKOVTASK_PROD', environ['USKOVTASK_PROD'])
    return _application(environ, start_response)

settings.py's part:

import os

if 'USKOVTASK_PROD' in os.environ:
    from settings_prod import *
else:
    from settings_dev import *

But it always imports settings_dev's settings. Why?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I solved this problem by changing wsgi.py to this:

from django.core.handlers.wsgi import WSGIHandler
import django
import os

class WSGIEnvironment(WSGIHandler):

    def __call__(self, environ, start_response):

        os.environ['USKOVTASK_PROD'] = environ['USKOVTASK_PROD']
        os.environ.setdefault("DJANGO_SETTINGS_MODULE", "uskovtask.settings")
        django.setup()
        return super(WSGIEnvironment, self).__call__(environ, start_response)

application = WSGIEnvironment()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...