我想部署一个用Django创建的网站。生产环境是租用的虚拟服务器。
我想用Django部署应用程序。因此,我根据文档更改了所有设置(特别是,创建了一个可以提供所有收集的静态文件的文件夹)并在我的本地开发机器上进行了尝试。
由于网站现已准备好,我将整个项目推送到虚拟服务器。我在我的开发机器和虚拟主机上都使用Ubuntu 14.04 LTS。虽然我使用apache在本地计算机上测试了该项目,但在部署阶段遇到了一些困难。该项目名为 kleyboldt 。我的virtualenv存储在 / root 目录中,项目位于 / var / www 下。以下是重要文件:
/etc/apache2/sites-available/mks.conf
WSGIDaemonProcess mathias-kleyboldt-stiftung.de python-path=/var/www/kleyboldt_homepage$
WSGIProcessGroup mathias-kleyboldt-stiftung.de
<VirtualHost *:80>
DocumentRoot /var/html/kleyboldt_homepage
WSGIScriptAlias / /var/www/kleyboldt.wsgi
ServerName mathias-kleyboldt-stiftung.de
ServerAlias www.mathias-kleyboldt-stiftung.de
<LocationMatch "\.(jpg|css|gif|pdf|ico)$">
SetHandler None
</LocationMatch>
Alias /media/ /var/www/kleyboldt_homepage/static/media/
Alias /static/ /var/www/kleyboldt_homepage/static/static-only/
<Directory /var/www/kleyboldt_homepage/>
Require all granted
Order allow,deny
Allow from all
</Directory>
<Directory /var/www/kleyboldt_homepage/static/static-only>
Require all granted
Order allow,deny
Allow from all
</Directory>
ErrorLog /var/www/kleyboldt_homepage/apache_error.log
LogLevel debug
</VirtualHost>
/var/www/kleyboldt.wsgi
import os
import sys
sys.path.append('/var/www/kleyboldt_homepage')
os.environ['DJANGO_SETTINGS_MODULE'] = 'kleyboldt_homepage.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
/ var / www / kleyboldt_homepage 下的项目结构:
root@somewhere:/var/www/kleyboldt_homepage# ls
apache_error.log homepage index.html manage.py static
db.sqlite3 homepage.log kleyboldt_homepage site.txt
为了管理这个项目的依赖项,我使用virtualenvwrapper在名为kleyboldt-homepage的 / root / virtualenvs 下创建一个env:
root@somewhere:~/virtualenvs/kleyboldt-homepage/lib/python2.7/site-packages# ls
crispy_forms markdown2.pyc
django markdown_deux
Django-1.6.5.dist-info _markerlib
django_crispy_forms-1.4.0-py2.7.egg-info pagedown
django_grappelli-2.5.3-py2.7.egg-info pip
django_markdown_deux-1.0.4-py2.7.egg-info pip-1.5.4.dist-info
django_pagedown-0.1.0-py2.7.egg-info pkg_resources.py
easy_install.py pkg_resources.pyc
easy_install.pyc setuptools
grappelli setuptools-2.2.dist-info
markdown2-2.2.1-py2.7.egg-info south
markdown2.py South-1.0-py2.7.egg-info
重新加载apache2服务器并刷新页面后,我收到500内部服务器错误。我在apache conf文件中指定的调试文件中查找了它。
/var/www/kleyboldt_homepage/apache_error.log
[Mon Aug 18 17:04:50.226000 2014] [authz_core:debug] [pid 966:tid 139697743423232] mod_authz_core.c(802): [client 92.224.193.119:56235] AH01626: authorization result of Require all granted: granted
[Mon Aug 18 17:04:50.226104 2014] [authz_core:debug] [pid 966:tid 139697743423232] mod_authz_core.c(802): [client 92.224.193.119:56235] AH01626: authorization result of <RequireAny>: granted
[Mon Aug 18 17:04:50.226227 2014] [authz_core:debug] [pid 966:tid 139697743423232] mod_authz_core.c(802): [client 92.224.193.119:56235] AH01626: authorization result of Require all granted: granted
[Mon Aug 18 17:04:50.226239 2014] [authz_core:debug] [pid 966:tid 139697743423232] mod_authz_core.c(802): [client 92.224.193.119:56235] AH01626: authorization result of <RequireAny>: granted
[Mon Aug 18 17:04:50.241584 2014] [:info] [pid 965:tid 139697924556544] [remote 92.224.193.119:14076] mod_wsgi (pid=965, process='mathias-kleyboldt-stiftung.de', application='mathias-kleyboldt-stiftung.de|'): Loading WSGI script '/var/www/kleyboldt.wsgi'.
[Mon Aug 18 17:04:50.242108 2014] [:error] [pid 965:tid 139697924556544] [remote 92.224.193.119:14076] mod_wsgi (pid=965): Target WSGI script '/var/www/kleyboldt.wsgi' cannot be loaded as Python module.
[Mon Aug 18 17:04:50.242118 2014] [:error] [pid 965:tid 139697924556544] [remote 92.224.193.119:14076] mod_wsgi (pid=965): Exception occurred processing WSGI script '/var/www/kleyboldt.wsgi'.
[Mon Aug 18 17:04:50.242137 2014] [:error] [pid 965:tid 139697924556544] [remote 92.224.193.119:14076] Traceback (most recent call last):
[Mon Aug 18 17:04:50.242161 2014] [:error] [pid 965:tid 139697924556544] [remote 92.224.193.119:14076] File "/var/www/kleyboldt.wsgi", line 7, in <module>
[Mon Aug 18 17:04:50.242215 2014] [:error] [pid 965:tid 139697924556544] [remote 92.224.193.119:14076] import django.core.handlers.wsgi
[Mon Aug 18 17:04:50.242233 2014] [:error] [pid 965:tid 139697924556544] [remote 92.224.193.119:14076] ImportError: No module named django.core.handlers.wsgi
导入 django.core.handlers.wsgi 似乎失败了。我检查了 WSGIDaemonProcess 后面指定的python路径,但一切似乎都没问题。但进口仍然失败。有人知道如何解决这个问题吗?
答案 0 :(得分:2)
两个潜在的错误
Django设置文件必须是Python模块
根据您提供的输入,在您的情况下,它不是Python模块,您的文件夹结构是错误的
sys.path.append('/var/www/kleyboldt_homepage')
os.environ['DJANGO_SETTINGS_MODULE'] = 'kleyboldt_homepage.settings'
上述意味着文件夹/ var / www / kleyboldt_homepage中的.py文件转到顶级Python命名空间。例如。 settings.py文件是模块“settings”,而不是'kleyboldt_homepage.settings'。
Virtualenv路径必须位于sys.path
以下是django.wsgi
示例。请将此作为指导示例,而不是针对您的部署的经过测试的解决方案:
# Must be in the project root or production deployment does not work
import os
import sys
from os.path import abspath, dirname, join
# This is /srv/django/yoursite
PROJECT_PATH=abspath(join(dirname(__file__), "."))
import site
import os
# Assume virtualenv is in relative subdirectory "venv" to the project root
vepath = PROJECT_PATH+'/venv/lib/python2.7/site-packages'
prev_sys_path = list(sys.path)
# add the site-packages of our virtualenv as a site dir
site.addsitedir(vepath)
# reorder sys.path so new directories from the addsitedir show up first
new_sys_path = [p for p in sys.path if p not in prev_sys_path]
for item in new_sys_path:
sys.path.remove(item)
sys.path[:0] = new_sys_path
# import from down here to pull in possible virtualenv django install
from django.core.handlers.wsgi import WSGIHandler
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
application = WSGIHandler()