使用Virtualenv和Apache部署Django

3 投票
1 回答
4465 浏览
提问于 2025-04-18 17:43

我想在一个用Django创建的网站上进行部署。我的生产环境是一个租来的虚拟服务器。

为了部署这个应用程序,我根据文档修改了所有设置(特别是,创建了一个文件夹来存放所有收集的静态文件),然后在我的本地开发机器上进行了测试。

现在网站已经准备好了,我把整个项目推送到了虚拟服务器上。我在开发机器和虚拟主机上都使用的是Ubuntu 14.04 LTS。虽然我在本地机器上用Apache测试过这个项目,但在部署阶段遇到了一些困难。这个项目叫做kleyboldt。我的虚拟环境存放在/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在/root/virtualenvs下创建了一个名为kleyboldt-homepage的环境:

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配置文件中指定的调试文件。

/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路径,一切看起来都正常。但导入仍然失败。有没有人知道怎么解决这个问题?

1 个回答

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'。

虚拟环境的路径必须在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()

撰写回答