Django + mod_wsgi。从Apache的SetEnv设置OS环境变量 [英] Django + mod_wsgi. Set OS environment variable from Apache's SetEnv
问题描述
我需要拆分Django的开发和生产设置。我决定如果 USKOVTASK_PROD
变量被设置,那么应用程序应该使用生产设置。我阅读了这篇文章,并尝试这样做。 / p>
我的片段:
/etc/apache2/sites-enabled/uskovtask.conf: strong>
< VirtualHost *:80>
ServerName uskovtask。*。com
ServerAlias uskovtask。*。com
DocumentRoot / mnt / ebs / uskovtask
别名/静态/ mnt / ebs / uskovtask / static /
< Directory / mnt / ebs / uskovtask / static>
要求全部授予
< / 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
<目录/ MNT / EBS / uskovtask / uskovtask>
<文件wsgi.py>
要求全部授予
< / Files>
< / Directory>
< / VirtualHost>
wsgi.py:
import os
os.environ.setdefault(DJANGO_SETTINGS_MODULE,uskovtask.settings)
from django.core.wsgi导入get_wsgi_application
_application = get_wsgi_application()
def应用程序(environ,start_response):
如果环境中的USKOVTASK_PROD:
os.environ。 setdefault('USKOVTASK_PROD',environ ['USKOVTASK_PROD'])
return _application(environ,start_response)
settings.py的部分
import os
if os.environ中的USKOVTASK_PROD:
from settings_prod import *
else:
from settings_dev import *
但它总是导入settings_dev的设置。为什么?
这与问题从Django wsgi.py文件访问Apache SetEnv变量
您需要继承WSGIHandler作为答案说。
正如Graham Dumpleton在第二个答案中所述,
所有这一切,你提到的博客文章通常不会有帮助。这个
是因为它正在使用一个令人讨厌的技巧来设置每个请求上的
环境变量,这是基于每个请求使用SetEnv在Apache中设置的WSGI
environ设置。如果
环境变量的值可以根据URL上下文而有所不同,这可能导致多线程配置中的各种
问题。对于
Django的情况,这是没有帮助的,因为Django设置模块通常会在处理任何请求之前导入
,这意味着
环境变量不可用时间
需要。
我认为这是你的情况发生了。
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?
This is related to question Access Apache SetEnv variable from Django wsgi.py file
You need to inherit WSGIHandler as the answer says.
As Graham Dumpleton explains in the second answer,
That all said, the blog post you mention will not usually help. This is because it is using the nasty trick of setting the process environment variables on each request based on the per request WSGI environ settings set using SetEnv in Apache. This can cause various issues in a multi threading configuration if the values of the environment variables can differ based on URL context. For the case of Django, it isn't helpful because the Django settings module would normally be imported before any requests had been handled, which means that the environment variables would not be available at the time required.
and I think this is what is happening in your case.
这篇关于Django + mod_wsgi。从Apache的SetEnv设置OS环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!