让 Python 2.7 与我的 django 项目一起工作的问题
Issues getting Python 2.7 to work with my django project
一段时间以来,我一直在尝试让我的 django 项目安装 badgr(github 上的 badgr-server)与 mod_wsgi 一起工作。我有点不知所措,但我相信我已经取得了一些进步。这是我当前收到的错误消息:
[Thu Nov 17 12:48:48.182772 2016] [core:notice] [pid 24556] AH00094: Command line: '/opt/rh/httpd24/root/usr/sbin/httpd'
[Thu Nov 17 12:48:50.421089 2016] [:error] [pid 24558] [remote 140.225.0.153:60440] mod_wsgi (pid=24558): Target WSGI script '/opt/badgr/code/apps/mainsite/wsgi.py' cannot be loaded as Python module.
[Thu Nov 17 12:48:50.421165 2016] [:error] [pid 24558] [remote 140.225.0.153:60440] mod_wsgi (pid=24558): Exception occurred processing WSGI script '/opt/badgr/code/apps/mainsite/wsgi.py'.
[Thu Nov 17 12:48:50.421200 2016] [:error] [pid 24558] [remote 140.225.0.153:60440] Traceback (most recent call last):
[Thu Nov 17 12:48:50.421228 2016] [:error] [pid 24558] [remote 140.225.0.153:60440] File "/opt/badgr/code/apps/mainsite/wsgi.py", line 27, in <module>
[Thu Nov 17 12:48:50.421348 2016] [:error] [pid 24558] [remote 140.225.0.153:60440] application = get_wsgi_application()
[Thu Nov 17 12:48:50.421374 2016] [:error] [pid 24558] [remote 140.225.0.153:60440] File "/opt/badgr/env/lib/python2.7/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
[Thu Nov 17 12:48:50.421450 2016] [:error] [pid 24558] [remote 140.225.0.153:60440] django.setup()
[Thu Nov 17 12:48:50.421472 2016] [:error] [pid 24558] [remote 140.225.0.153:60440] File "/opt/badgr/env/lib/python2.7/site-packages/django/__init__.py", line 20, in setup
[Thu Nov 17 12:48:50.421558 2016] [:error] [pid 24558] [remote 140.225.0.153:60440] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Thu Nov 17 12:48:50.421581 2016] [:error] [pid 24558] [remote 140.225.0.153:60440] File "/opt/badgr/env/lib/python2.7/site-packages/django/conf/__init__.py", line 46, in __getattr__
[Thu Nov 17 12:48:50.421705 2016] [:error] [pid 24558] [remote 140.225.0.153:60440] self._setup(name)
[Thu Nov 17 12:48:50.421728 2016] [:error] [pid 24558] [remote 140.225.0.153:60440] File "/opt/badgr/env/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
[Thu Nov 17 12:48:50.421764 2016] [:error] [pid 24558] [remote 140.225.0.153:60440] self._wrapped = Settings(settings_module)
[Thu Nov 17 12:48:50.421782 2016] [:error] [pid 24558] [remote 140.225.0.153:60440] File "/opt/badgr/env/lib/python2.7/site-packages/django/conf/__init__.py", line 98, in __init__
[Thu Nov 17 12:48:50.421811 2016] [:error] [pid 24558] [remote 140.225.0.153:60440] % (self.SETTINGS_MODULE, e)
[Thu Nov 17 12:48:50.421841 2016] [:error] [pid 24558] [remote 140.225.0.153:60440] ImportError: Could not import settings 'settings' (Is it on sys.path? Is there an import error in the settings file?): No module named settings`
这是我的 wsgi.py (/opt/badgr/code/apps/mainsite/wsgi.py) 的副本:
"""
WSGI config for badgr project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/1.7/howto/deployment/wsgi/
"""
import sys
import os
import site
activate_this = os.path.expanduser("/opt/badgr/env/bin/activate_this.py")
execfile(activate_this, dict(__file__=activate_this))
path = '/opt/badgr/code/apps/mainsite'
if path not in sys.path:
sys.path.append(path)
os.environ["DJANGO_SETTINGS_MODULE"] = 'settings'
site.addsitedir('/opt/badgr/env/lib/python2.7/site-packages')
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
我很确定我的设置文件是 /opt/badgr/code/apps/mainsite/settings.py(或 .pyc)。但是调用起来好像有点问题
还需要注意的是,我是 运行 Python 2.7 的环境:
/opt/badgr/env/lib/python2.7/
最后:这是我在 httpd24 中的 django 配置 (/opt/rh/httpd24/root/etc/httpd/conf.d/django.conf):
Alias /static /opt/badgr/code/staticfiles
<Directory /opt/badgr/code/staticfiles>
Require all granted
</Directory>
<Directory /opt/badgr/code/apps/mainsite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGISocketPrefix /var/run/wsgi/wsgi
WSGIDaemonProcess badgr
WSGIProcessGroup badgr
WSGIPythonPath /opt/badgr:/opt/badgr/env/lib/python2.7/site-packages
WSGIScriptAlias / /opt/badgr/code/apps/mainsite/wsgi.py
WSGIPythonHome /opt/badgr/env
感谢任何帮助。
对于 Apache 配置尝试使用:
Alias /static /opt/badgr/code/staticfiles
<Directory /opt/badgr/code/staticfiles>
Require all granted
</Directory>
<Directory /opt/badgr/code/apps/mainsite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGISocketPrefix /var/run/wsgi/wsgi
WSGIRestrictEmbedded On
WSGIDaemonProcess badge python-home=/opt/badgr/env python-path=/opt/badgr/code/apps
WSGIProcessGroup badge
WSGIScriptAlias / /opt/badgr/code/apps/mainsite/wsgi.py
对于 wsgi.py
文件,将其还原为类似于最初创建的文件。
import os
os.environ["DJANGO_SETTINGS_MODULE"] = 'mainsite.settings'
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
WSGIRestrictEmbedded
确保只使用 mod_wsgi 守护进程模式。
使用 python-home
选项将 Python 虚拟环境的位置设置为 WSGIDaemonProcess
。在 python-path
中设置项目的父目录。更改 DJANGO_SETTINGS_MODULE
以再次将项目名称包含在模块路径中。
更新 1
添加到wsgi.py
文件:
import sys
print 'SYS.PATH = %r' % sys.path
检查 Apache 错误日志以查看路径是什么,并验证是否列出了项目所在的父目录。
一段时间以来,我一直在尝试让我的 django 项目安装 badgr(github 上的 badgr-server)与 mod_wsgi 一起工作。我有点不知所措,但我相信我已经取得了一些进步。这是我当前收到的错误消息:
[Thu Nov 17 12:48:48.182772 2016] [core:notice] [pid 24556] AH00094: Command line: '/opt/rh/httpd24/root/usr/sbin/httpd'
[Thu Nov 17 12:48:50.421089 2016] [:error] [pid 24558] [remote 140.225.0.153:60440] mod_wsgi (pid=24558): Target WSGI script '/opt/badgr/code/apps/mainsite/wsgi.py' cannot be loaded as Python module.
[Thu Nov 17 12:48:50.421165 2016] [:error] [pid 24558] [remote 140.225.0.153:60440] mod_wsgi (pid=24558): Exception occurred processing WSGI script '/opt/badgr/code/apps/mainsite/wsgi.py'.
[Thu Nov 17 12:48:50.421200 2016] [:error] [pid 24558] [remote 140.225.0.153:60440] Traceback (most recent call last):
[Thu Nov 17 12:48:50.421228 2016] [:error] [pid 24558] [remote 140.225.0.153:60440] File "/opt/badgr/code/apps/mainsite/wsgi.py", line 27, in <module>
[Thu Nov 17 12:48:50.421348 2016] [:error] [pid 24558] [remote 140.225.0.153:60440] application = get_wsgi_application()
[Thu Nov 17 12:48:50.421374 2016] [:error] [pid 24558] [remote 140.225.0.153:60440] File "/opt/badgr/env/lib/python2.7/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
[Thu Nov 17 12:48:50.421450 2016] [:error] [pid 24558] [remote 140.225.0.153:60440] django.setup()
[Thu Nov 17 12:48:50.421472 2016] [:error] [pid 24558] [remote 140.225.0.153:60440] File "/opt/badgr/env/lib/python2.7/site-packages/django/__init__.py", line 20, in setup
[Thu Nov 17 12:48:50.421558 2016] [:error] [pid 24558] [remote 140.225.0.153:60440] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Thu Nov 17 12:48:50.421581 2016] [:error] [pid 24558] [remote 140.225.0.153:60440] File "/opt/badgr/env/lib/python2.7/site-packages/django/conf/__init__.py", line 46, in __getattr__
[Thu Nov 17 12:48:50.421705 2016] [:error] [pid 24558] [remote 140.225.0.153:60440] self._setup(name)
[Thu Nov 17 12:48:50.421728 2016] [:error] [pid 24558] [remote 140.225.0.153:60440] File "/opt/badgr/env/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
[Thu Nov 17 12:48:50.421764 2016] [:error] [pid 24558] [remote 140.225.0.153:60440] self._wrapped = Settings(settings_module)
[Thu Nov 17 12:48:50.421782 2016] [:error] [pid 24558] [remote 140.225.0.153:60440] File "/opt/badgr/env/lib/python2.7/site-packages/django/conf/__init__.py", line 98, in __init__
[Thu Nov 17 12:48:50.421811 2016] [:error] [pid 24558] [remote 140.225.0.153:60440] % (self.SETTINGS_MODULE, e)
[Thu Nov 17 12:48:50.421841 2016] [:error] [pid 24558] [remote 140.225.0.153:60440] ImportError: Could not import settings 'settings' (Is it on sys.path? Is there an import error in the settings file?): No module named settings`
这是我的 wsgi.py (/opt/badgr/code/apps/mainsite/wsgi.py) 的副本:
"""
WSGI config for badgr project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/1.7/howto/deployment/wsgi/
"""
import sys
import os
import site
activate_this = os.path.expanduser("/opt/badgr/env/bin/activate_this.py")
execfile(activate_this, dict(__file__=activate_this))
path = '/opt/badgr/code/apps/mainsite'
if path not in sys.path:
sys.path.append(path)
os.environ["DJANGO_SETTINGS_MODULE"] = 'settings'
site.addsitedir('/opt/badgr/env/lib/python2.7/site-packages')
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
我很确定我的设置文件是 /opt/badgr/code/apps/mainsite/settings.py(或 .pyc)。但是调用起来好像有点问题
还需要注意的是,我是 运行 Python 2.7 的环境: /opt/badgr/env/lib/python2.7/
最后:这是我在 httpd24 中的 django 配置 (/opt/rh/httpd24/root/etc/httpd/conf.d/django.conf):
Alias /static /opt/badgr/code/staticfiles
<Directory /opt/badgr/code/staticfiles>
Require all granted
</Directory>
<Directory /opt/badgr/code/apps/mainsite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGISocketPrefix /var/run/wsgi/wsgi
WSGIDaemonProcess badgr
WSGIProcessGroup badgr
WSGIPythonPath /opt/badgr:/opt/badgr/env/lib/python2.7/site-packages
WSGIScriptAlias / /opt/badgr/code/apps/mainsite/wsgi.py
WSGIPythonHome /opt/badgr/env
感谢任何帮助。
对于 Apache 配置尝试使用:
Alias /static /opt/badgr/code/staticfiles
<Directory /opt/badgr/code/staticfiles>
Require all granted
</Directory>
<Directory /opt/badgr/code/apps/mainsite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGISocketPrefix /var/run/wsgi/wsgi
WSGIRestrictEmbedded On
WSGIDaemonProcess badge python-home=/opt/badgr/env python-path=/opt/badgr/code/apps
WSGIProcessGroup badge
WSGIScriptAlias / /opt/badgr/code/apps/mainsite/wsgi.py
对于 wsgi.py
文件,将其还原为类似于最初创建的文件。
import os
os.environ["DJANGO_SETTINGS_MODULE"] = 'mainsite.settings'
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
WSGIRestrictEmbedded
确保只使用 mod_wsgi 守护进程模式。
使用 python-home
选项将 Python 虚拟环境的位置设置为 WSGIDaemonProcess
。在 python-path
中设置项目的父目录。更改 DJANGO_SETTINGS_MODULE
以再次将项目名称包含在模块路径中。
更新 1
添加到wsgi.py
文件:
import sys
print 'SYS.PATH = %r' % sys.path
检查 Apache 错误日志以查看路径是什么,并验证是否列出了项目所在的父目录。