我花了很多时间在google上阅读文档和stackoverflow答案,但是在apache / mod_wsgi守护程序模式的帮助下,无法在virtualenv中部署django。
这是我的apache conf文件:
<VirtualHost *:80>
ServerAdmin admin@example.com
ServerName example.com
LogLevel info
WSGIDaemonProcess example.com python-path=/var/www/testproject:/root/.virtualenvs/env1/lib/python2.7/site-packages
WSGIProcessGroup example.com
WSGIScriptAlias / /var/www/testproject/testproject/wsgi.py
<Directory /var/www/testproject/testproject>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
ErrorLog /var/www/testproject/log/error.log
CustomLog /var/www/testproject/log/access.log combined
</VirtualHost>
这是我的wsgi.py fil。 (我还没碰过它):
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testproject.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
这是我的日志文件:
[Thu Jan 22 18:45:12.289458 2015] [:info] [pid 585:tid 139777547847552] mod_wsgi (pid=585): Cleanup interpreter ''.
[Thu Jan 22 18:45:12.293845 2015] [:info] [pid 585:tid 139777547847552] mod_wsgi (pid=585): Terminating Python.
[Thu Jan 22 18:45:12.296594 2015] [:info] [pid 585:tid 139777547847552] mod_wsgi (pid=585): Python has shutdown.
[Thu Jan 22 18:45:13.364017 2015] [:info] [pid 982:tid 139775604578176] mod_wsgi (pid=982): Attach interpreter ''.
[Thu Jan 22 18:45:13.365960 2015] [:info] [pid 982:tid 139775604578176] mod_wsgi (pid=982): Adding '/var/www/testproject' to path.
[Thu Jan 22 18:45:13.366464 2015] [:info] [pid 982:tid 139775604578176] mod_wsgi (pid=982): Adding '/root/.virtualenvs/env1/lib/python2.7/site-packages' to path.
[Thu Jan 22 18:45:35.255450 2015] [:info] [pid 982:tid 139775484114688] mod_wsgi (pid=982): Create interpreter 'example.com|'.
[Thu Jan 22 18:45:35.257781 2015] [:info] [pid 982:tid 139775484114688] mod_wsgi (pid=982): Adding '/var/www/testproject' to path.
[Thu Jan 22 18:45:35.258663 2015] [:info] [pid 982:tid 139775484114688] mod_wsgi (pid=982): Adding '/root/.virtualenvs/env1/lib/python2.7/site-packages' to path.
[Thu Jan 22 18:45:35.259379 2015] [:info] [pid 982:tid 139775484114688] [remote 206.190.152.176:8971] mod_wsgi (pid=982, process='env1', application='example.com|'): Loading WSGI script '/var/www/testproject/testproject/wsgi.py'.
[Thu Jan 22 18:45:35.259995 2015] [:error] [pid 982:tid 139775484114688] [remote 206.190.152.176:8971] mod_wsgi (pid=982): Target WSGI script '/var/www/testproject/testproject/wsgi.py' cannot be loaded as Python module.
[Thu Jan 22 18:45:35.260417 2015] [:error] [pid 982:tid 139775484114688] [remote 206.190.152.176:8971] mod_wsgi (pid=982): Exception occurred processing WSGI script '/var/www/testproject/testproject/wsgi.py'.
[Thu Jan 22 18:45:35.260669 2015] [:error] [pid 982:tid 139775484114688] [remote 206.190.152.176:8971] Traceback (most recent call last):
[Thu Jan 22 18:45:35.260888 2015] [:error] [pid 982:tid 139775484114688] [remote 206.190.152.176:8971] File "/var/www/testproject/testproject/wsgi.py", line 13, in <module>
[Thu Jan 22 18:45:35.261212 2015] [:error] [pid 982:tid 139775484114688] [remote 206.190.152.176:8971] from django.core.wsgi import get_wsgi_application
[Thu Jan 22 18:45:35.261463 2015] [:error] [pid 982:tid 139775484114688] [remote 206.190.152.176:8971] ImportError: No module named django.core.wsgi
[Thu Jan 22 18:45:35.693482 2015] [:info] [pid 982:tid 139775500916480] [remote 206.190.152.176:37373] mod_wsgi (pid=982, process='env1', application='example.com|'): Loading WSGI script '/var/www/testproject/testproject/wsgi.py'.
[Thu Jan 22 18:45:35.694122 2015] [:error] [pid 982:tid 139775500916480] [remote 206.190.152.176:37373] mod_wsgi (pid=982): Target WSGI script '/var/www/testproject/testproject/wsgi.py' cannot be loaded as Python module.
[Thu Jan 22 18:45:35.694140 2015] [:error] [pid 982:tid 139775500916480] [remote 206.190.152.176:37373] mod_wsgi (pid=982): Exception occurred processing WSGI script '/var/www/testproject/testproject/wsgi.py'.
[Thu Jan 22 18:45:35.694180 2015] [:error] [pid 982:tid 139775500916480] [remote 206.190.152.176:37373] Traceback (most recent call last):
[Thu Jan 22 18:45:35.694220 2015] [:error] [pid 982:tid 139775500916480] [remote 206.190.152.176:37373] File "/var/www/testproject/testproject/wsgi.py", line 13, in <module>
[Thu Jan 22 18:45:35.694267 2015] [:error] [pid 982:tid 139775500916480] [remote 206.190.152.176:37373] from django.core.wsgi import get_wsgi_application
[Thu Jan 22 18:45:35.694296 2015] [:error] [pid 982:tid 139775500916480] [remote 206.190.152.176:37373] ImportError: No module named django.core.wsgi
example.com
是我的域名
env1
是我的virtualenv
在我的日志文件中,我可以看到virtualenv已被添加到路径中:
'/root/.virtualenvs/env1/lib/python2.7/site-packages' to path.
但我仍然收到错误:
ImportError: No module named django.core.wsgi
全局安装django解决了这个问题,但我不是我想要的。
我在Ubuntu 12.10
,Django 1.7
,Python 2.7
参考: