apache mod_wsgi错误与django在virtualenv [英] apache mod_wsgi error with django in virtualenv
问题描述
django apache配置与WSGIDaemonProcess不工作
django项目,virtualenv文件夹或两者都需要由apache组拥有?在没有指定港口的情况下,需要有什么所有权来为django项目提供服务?为什么我得到以下内容?
根问题:
调用'site.addsitedir )'''null''
当我启动apache时,我得到这个错误。我遵循了几个不同的指南,其中包括:
http://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html
和
https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/modwsgi/
但没有成功。
我的虚拟环境路径是 / usr / local / virtualenvs / servicesite
p>
我的django项目路径是 / home / addohm / projects / rtservice / servicesite
其中manage.py驻留,
将作为我的wsgi.py的位置留下 / home / addohm / projects / rtservice / servicesite / servicesite
wsgi.py:
SERVICESITE = ['/usr/local/virtualenvs/servicesite/lib/python3.5/site-packages']
import os
import sys
import site
prev_sys_path = list(sys.path)
用于SERVICESITE
site.addsitedir(目录)
new_sys_path = []
列表中的项目(sys.path):
如果项目不在prev_sys_path中:
new_sys_path.append(item)
sys.path.remove(item)
sys.path [:0] = new_sys_path
**似乎没有工作,在apache日志中抛出错误**
site.addsitedir('/ usr / local / virtualenvs / servicesite / lib / python3.5 / site-packages')
from django.core.wsgi import get_wsgi_application
os.environ.setdefault(DJANGO_SETTINGS_MODULE,servicesite.settings)
application = get_wsgi_application()
DJANGO_PATH = os.path.join (os.path.abspath(os.path.dirname(__ file__)),'..')
sys.path.append(DJANGO_PATH)
apache2.conf
[...]
WSGID aemonProcess servicesite python-path = / home / addohm / projects / rtservice / servicesite:/usr/local/virtualenvs/servicesite/lib/python3.5/site-packages
WSGIProcessGroup servicesite
WSGIScriptAlias / / home / addohm / projects / rtservice / servicesite / servicesite / wsgi.py
别名/静态/ / home / addohm / projects / rtservice / servicesite / static /
< Directory / home / addohm /项目/ rtService选/ servicesite /静态/>
要求全部授予
< / Directory>
<目录/ home / addohm / projects / rtservice / servicesite / servicesite>
<文件wsgy.py>
要求全部授予
< / Files>
< / Directory>
[...]
您不需要更改Django为您生成的原始 wsgi.py
中的任何内容。通常情况下,您可以通过以下方式获取以下内容:
从django.core.wsgi导入os
import get_wsgi_application
os .environ.setdefault(DJANGO_SETTINGS_MODULE,servicesite.settings)
application = get_wsgi_application()
您的Apache配置最好应该是:
WSGIDaemonProcess服务站点python-home = / usr / local / virtualenvs / servicesite \
python-path = / home / addohm / projects / rtservice / servicesite
WSGIProcessGroup servicesite
WSGIScriptAlias / /home/addohm/projects/rtservice/servicesite/servicesite/wsgi.py
别名/静态/ / home / addohm / projects / rtservice / servicesite / static /
< Directory / home / addohm / projects / rtservice / servicesite / static />
要求全部授予
< / Directory>
<目录/ home / addohm / projects / rtservice / servicesite / servicesite>
<文件wsgy.py>
要求全部授予
< / Files>
< / Directory>
也就是说,使用 python-home
用于虚拟环境的由 sys.prefix
指定的目录的位置。避免使用 python-path
并参考 site-packages
目录。使用 python-home
已经是很长一段时间的首选方式,并且使用它确保了当您不以正确的方式做事情时,事情会以更明显的方式失败。 / p>
一些非常重要的事情。
第一个是mod_wsgi必须编译为特定的Python专业/次要您要使用的版本。
其次,Python虚拟环境必须从与mod_wsgi编译的相同的Python安装中创建。您不能针对系统Python安装编译mod_wsgi,但是您的虚拟环境基于单独的Python安装,可以在 / usr / local
中使用相同的主要/次要版本。 / p>
第三,Apache运行您的代码的用户必须具有对主Python安装,虚拟环境和应用程序代码的任何目录/文件的读取访问权限。当您将东西放在主目录下时,通常不会有访问权限,因为主目录禁止其他人读取主目录中的任何内容。
第四,如果mod_wsgi守护进程进程组设置为与Apache用户不同的用户运行,Apache用户仍然至少必须能够读取 wsgi.py
文件以及所有目录
进一步阅读更新更新的虚拟环境:
I can't seem to find a good answer to this. Who needs to own the virtualenv when running it as a WSGIDaemon? I assume on my OS (Ubuntu 16) www-data, but I want to be sure. Trying some new things to get this thing working based off of the answer from this post...
django apache configuration with WSGIDaemonProcess not working
Does the django project, the virtualenv folder, or both need to be owned by the apache group? What ownerships need to be in place to serve a django project without specifying a port? Why do I get the following?
The root problem:
Call to 'site.addsitedir()' failed for '(null)'
When I start apache, I get this error. I've followed several different guides, including: http://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html and https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/modwsgi/ but have had zero success.
My virtual environment path is /usr/local/virtualenvs/servicesite
My django project path is /home/addohm/projects/rtservice/servicesite
this is where manage.py resides,
which leaves /home/addohm/projects/rtservice/servicesite/servicesite
as the location of my wsgi.py.
wsgi.py:
SERVICESITE = ['/usr/local/virtualenvs/servicesite/lib/python3.5/site-packages']
import os
import sys
import site
prev_sys_path = list(sys.path)
for directory in SERVICESITE
site.addsitedir(directory)
new_sys_path = []
for item in list(sys.path):
if item not in prev_sys_path:
new_sys_path.append(item)
sys.path.remove(item)
sys.path[:0] = new_sys_path
""" **Doesn't seem to work, throwing error in apache logs**
site.addsitedir('/usr/local/virtualenvs/servicesite/lib/python3.5/site-packages')
"""
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "servicesite.settings")
application = get_wsgi_application()
DJANGO_PATH = os.path.join(os.path.abspath(os.path.dirname(__file__)), '..')
sys.path.append(DJANGO_PATH)
apache2.conf
[...]
WSGIDaemonProcess servicesite python-path=/home/addohm/projects/rtservice/servicesite:/usr/local/virtualenvs/servicesite/lib/python3.5/site-packages
WSGIProcessGroup servicesite
WSGIScriptAlias / /home/addohm/projects/rtservice/servicesite/servicesite/wsgi.py
Alias /static/ /home/addohm/projects/rtservice/servicesite/static/
<Directory /home/addohm/projects/rtservice/servicesite/static/>
Require all granted
</Directory>
<Directory /home/addohm/projects/rtservice/servicesite/servicesite>
<Files wsgy.py>
Require all granted
</Files>
</Directory>
[...]
You should not have need to change anything in the original wsgi.py
generated by Django for you. It is generally sufficient to have:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "servicesite.settings")
application = get_wsgi_application()
Your Apache configuration should then preferably be:
WSGIDaemonProcess service site python-home=/usr/local/virtualenvs/servicesite \
python-path=/home/addohm/projects/rtservice/servicesite
WSGIProcessGroup servicesite
WSGIScriptAlias / /home/addohm/projects/rtservice/servicesite/servicesite/wsgi.py
Alias /static/ /home/addohm/projects/rtservice/servicesite/static/
<Directory /home/addohm/projects/rtservice/servicesite/static/>
Require all granted
</Directory>
<Directory /home/addohm/projects/rtservice/servicesite/servicesite>
<Files wsgy.py>
Require all granted
</Files>
</Directory>
That is, use python-home
for the location of the directory specified by sys.prefix
for the virtual environment. Avoid using python-path
and referring to the site-packages
directory. Using python-home
has been preferred way for a very long time and using it ensures that things fail in a more obvious way when you don't do things the correct way.
A few very important things.
The first is that mod_wsgi must be compiled for the specific Python major/minor version you want to use.
Second, the Python virtual environment must be created from the same Python installation as mod_wsgi was compiled for. You can't have mod_wsgi compiled against a system Python installation, but have your virtual environment based off a separate Python installation for same major/minor version in /usr/local
.
Third, the user that Apache runs your code as must have read access to any directories/files for the main Python installation, the virtual environment and your application code. When you stick stuff under a home directory, it will not generally have access as the home directory prohibits others from reading anything in the home directory.
Fourth, if the mod_wsgi daemon process group is set to run as a different user than the Apache user, the Apache user still must at least have ability to read the wsgi.py
file and all the directories down to that point.
Further reading on virtual environments which is more up to date:
这篇关于apache mod_wsgi错误与django在virtualenv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!