无法将目标 WSGI 脚本“../wsgi.py”加载为 Python 模块
Target WSGI script '../wsgi.py' cannot be loaded as Python module
我正在部署一个 Django 项目并收到 500 错误(请参阅服务器日志错误)。
我哪里做错了?
一些注意事项:
- centOS
- mod_wsgi 已安装
- 开发和部署环境中的 python (2.7) 和 django (1.9.6) 版本相同
- 在部署环境中使用 virtualenv
服务器日志错误
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93] SyntaxError: invalid syntax
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93] mod_wsgi (pid=6570): Target WSGI script '/new_esmart/esmart2/esmart2/wsgi.py' cannot be loaded as Python module., referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93] mod_wsgi (pid=6570): Exception occurred processing WSGI script '/new_esmart/esmart2/esmart2/wsgi.py'., referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93] Traceback (most recent call last):, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93] File "/new_esmart/esmart2/esmart2/wsgi.py", line 13, in <module>, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93] import django.core.handlers.wsgi, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93] File "/new_esmart/esmart_env/lib/python2.7/site-packages/django/__init__.py", line 1, in <module>, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93] from django.utils.version import get_version, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93] File "/new_esmart/esmart_env/lib/python2.7/site-packages/django/utils/version.py", line 7, in <module>, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93] from django.utils.lru_cache import lru_cache, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93] File "/new_esmart/esmart_env/lib/python2.7/site-packages/django/utils/lru_cache.py", line 28, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93] fasttypes = {int, str, frozenset, type(None)},, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93] ^, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93] SyntaxError: invalid syntax, referer: http://192.168.30.17/logistics/alarms/
wsgi.py
# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('/new_esmart/esmart_env/lib/python2.7/site-packages')
# Add the app's directory to the PYTHONPATH
sys.path.append('/new_esmart/esmart2')
sys.path.append('/new_esmart/esmart2/esmart2')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "esmart2.settings")
# Activate your virtual env
activate_env = os.path.expanduser("/new_esmart/esmart_env/bin/activate_this.py")
execfile(activate_env, dict(__file__=activate_env))
application = django.core.handlers.wsgi.WSGIHandler()
httpd.conf
<VirtualHost *:80>
ServerAdmin blahblah@blah.it
DocumentRoot /new_esmart/esmart2
ServerName logistica.org
ServerAlias www.logistica.org
WSGIScriptAlias / /new_esmart/esmart2/esmart2/wsgi.py
ErrorLog logs/logistica.org-error_log
CustomLog logs/logistica.org-access_log common
</VirtualHost>
WSGIPythonPath /new_esmart/esmart2:/new_esmart/esmart_env/lib/python2.7/site-packages
/etc/httpd/conf.d/wsgi.conf
LoadModule wsgi_module modules/mod_wsgi.so
正如 Daniel 所指出的,该错误表明您的 mod_wsgi 是针对 Python 2.6 编译的。为了使用 Python 2.7,您需要安装 mod_wsgi 并针对 Python 2.7 进行编译。您不能通过简单地引用您的 Python 2.7 虚拟环境来尝试强制它使用 Python 2.7,它不是这样工作的。
您可以验证 Python 版本 mod_wsgi 是使用以下描述的测试应用程序编译的:
您将需要卸载 mod_wsgi 模块并安装为 Python 2.7 构建的版本,如果可用,则从系统包安装,或者如果没有可用于 [ 的系统包,则从源代码构建=38=] 对于 Python 2.7.
我还建议您查看有关使用 mod_wsgi 的 Django 文档,并确保按照说明使用守护进程模式。
请注意,Django 文档仍未遵循所有最佳实践。与其在 python-path
中显式添加 site-packages
,不如使用 python-home
来引用虚拟环境。参见:
您没有使用正确的 python 版本,但您可以使用 WSGIPythonHome
指令在您的 apache conf 中指定要使用的版本。
添加
WSGIPythonHome /path/to/your/virtualenv
到您的 Apache 配置,
这样你就可以使用 virtualenv 中的解释器了。
编辑:
由于您可能想专门为您的 VirtualHost 定义 python 主页(WSGIPythonHome
不能在 VirtualHost
范围内使用),您可以使用 WSGIDaemonProcess
指令:
<VirtualHost *:80>
ServerName example.com
[...]
WSGIDaemonProcess example.com python-home=/path/to/venv python-path=<python-path>
WSGIProcessGroup example.com
</VirtualHost>
我正在部署一个 Django 项目并收到 500 错误(请参阅服务器日志错误)。
我哪里做错了?
一些注意事项:
- centOS
- mod_wsgi 已安装
- 开发和部署环境中的 python (2.7) 和 django (1.9.6) 版本相同
- 在部署环境中使用 virtualenv
服务器日志错误
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93] SyntaxError: invalid syntax
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93] mod_wsgi (pid=6570): Target WSGI script '/new_esmart/esmart2/esmart2/wsgi.py' cannot be loaded as Python module., referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93] mod_wsgi (pid=6570): Exception occurred processing WSGI script '/new_esmart/esmart2/esmart2/wsgi.py'., referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93] Traceback (most recent call last):, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93] File "/new_esmart/esmart2/esmart2/wsgi.py", line 13, in <module>, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93] import django.core.handlers.wsgi, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93] File "/new_esmart/esmart_env/lib/python2.7/site-packages/django/__init__.py", line 1, in <module>, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93] from django.utils.version import get_version, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93] File "/new_esmart/esmart_env/lib/python2.7/site-packages/django/utils/version.py", line 7, in <module>, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93] from django.utils.lru_cache import lru_cache, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93] File "/new_esmart/esmart_env/lib/python2.7/site-packages/django/utils/lru_cache.py", line 28, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93] fasttypes = {int, str, frozenset, type(None)},, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93] ^, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93] SyntaxError: invalid syntax, referer: http://192.168.30.17/logistics/alarms/
wsgi.py
# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('/new_esmart/esmart_env/lib/python2.7/site-packages')
# Add the app's directory to the PYTHONPATH
sys.path.append('/new_esmart/esmart2')
sys.path.append('/new_esmart/esmart2/esmart2')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "esmart2.settings")
# Activate your virtual env
activate_env = os.path.expanduser("/new_esmart/esmart_env/bin/activate_this.py")
execfile(activate_env, dict(__file__=activate_env))
application = django.core.handlers.wsgi.WSGIHandler()
httpd.conf
<VirtualHost *:80>
ServerAdmin blahblah@blah.it
DocumentRoot /new_esmart/esmart2
ServerName logistica.org
ServerAlias www.logistica.org
WSGIScriptAlias / /new_esmart/esmart2/esmart2/wsgi.py
ErrorLog logs/logistica.org-error_log
CustomLog logs/logistica.org-access_log common
</VirtualHost>
WSGIPythonPath /new_esmart/esmart2:/new_esmart/esmart_env/lib/python2.7/site-packages
/etc/httpd/conf.d/wsgi.conf
LoadModule wsgi_module modules/mod_wsgi.so
正如 Daniel 所指出的,该错误表明您的 mod_wsgi 是针对 Python 2.6 编译的。为了使用 Python 2.7,您需要安装 mod_wsgi 并针对 Python 2.7 进行编译。您不能通过简单地引用您的 Python 2.7 虚拟环境来尝试强制它使用 Python 2.7,它不是这样工作的。
您可以验证 Python 版本 mod_wsgi 是使用以下描述的测试应用程序编译的:
您将需要卸载 mod_wsgi 模块并安装为 Python 2.7 构建的版本,如果可用,则从系统包安装,或者如果没有可用于 [ 的系统包,则从源代码构建=38=] 对于 Python 2.7.
我还建议您查看有关使用 mod_wsgi 的 Django 文档,并确保按照说明使用守护进程模式。
请注意,Django 文档仍未遵循所有最佳实践。与其在 python-path
中显式添加 site-packages
,不如使用 python-home
来引用虚拟环境。参见:
您没有使用正确的 python 版本,但您可以使用 WSGIPythonHome
指令在您的 apache conf 中指定要使用的版本。
添加
WSGIPythonHome /path/to/your/virtualenv
到您的 Apache 配置,
这样你就可以使用 virtualenv 中的解释器了。
编辑:
由于您可能想专门为您的 VirtualHost 定义 python 主页(WSGIPythonHome
不能在 VirtualHost
范围内使用),您可以使用 WSGIDaemonProcess
指令:
<VirtualHost *:80>
ServerName example.com
[...]
WSGIDaemonProcess example.com python-home=/path/to/venv python-path=<python-path>
WSGIProcessGroup example.com
</VirtualHost>