【发布时间】:2016-09-21 15:27:01
【问题描述】:
我正在部署一个 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
【问题讨论】:
-
虽然您已将 2.7 的库目录添加到 Python 路径,但您实际上并未使用 2.7 版本的 Python。这里有很多问题(其中大部分由 mod_wsgi 的作者回答)告诉您如何将其指向不同的版本。
-
与您当前的问题无关,但在较新的 Django 版本中,您需要 use
get_wsgi_application()而不是WSGIHandler()。 -
@DanielRoseman 在我的服务器上我在 env 和 virtualenv 中都有相同的 Python 版本(2.7.8)
-
我不知道在这种情况下“env”是什么,但没关系;您的 mod_wsgi 显然是针对 Python 2.6 运行的。
-
因为错误中突出显示的 set literal syntax 在 Python 2.6 中无效,但在 2.7 中有效。
标签: django apache virtualenv mod-wsgi wsgi