目标WSGI脚本'../wsgi.py'无法作为Python模块

时间:2016-09-21 15:27:02

标签: django apache virtualenv mod-wsgi wsgi

我正在部署django项目并获得500错误(请参阅服务器日志错误)。

我在哪里做错了?

一些注意事项:

  • CENTOS
  • mod_wsgi已安装
  • 相同的python(2.7)和django(1.9.6)版本开发和部署environement
  • 在deploy environement中使用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 个答案:

答案 0 :(得分:1)

正如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构建的版本,可以从系统软件包(如果可用)安装,也可以从源代码构建,如果没有可用于Python 2.7的mod_wsgi的系统软件包。

我还建议你检查关于使用mod_wsgi的Django文档,并确保你使用守护进程模式。

请注意,Django文档仍然没有遵循所有最佳实践。您应该使用site-packages来引用虚拟环境,而不是在python-path中明确添加python-home。参见:

答案 1 :(得分:1)

您没有使用正确的python版本,但您可以使用WSGIPythonHome指令指定在apache conf中使用哪个版本。

添加

WSGIPythonHome /path/to/your/virtualenv

到你的Apache配置,

通过这种方式,您可以使用virtualenv中的解释器。

编辑:

由于您可能希望专门为您的VirtualHost定义python home(WSGIPythonHome范围内不能使用VirtualHost),您可以使用WSGIDaemonProcess指令:

<VirtualHost *:80>
  ServerName example.com
  [...]

  WSGIDaemonProcess example.com python-home=/path/to/venv python-path=<python-path>
  WSGIProcessGroup example.com

</VirtualHost>