【问题标题】:Deploying Django with Virtualenv and Apache使用 Virtualenv 和 Apache 部署 Django
【发布时间】:2014-10-11 22:28:14
【问题描述】:

我想部署一个使用 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 在 /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 Internal Server 错误。我在 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 路径,但一切似乎都很好。但是导入仍然失败。有人知道如何解决这个问题吗?

【问题讨论】:

  • 查看我发布的问题和得到的答案。这可能是您正在寻找的:stackoverflow.com/questions/24739643/…
  • 但是你有/有不同的方法。我尝试使用 WSGIDaemonProcess,这是根据the official django documentation 推荐的方法。这种方法没有解决方案。它在我的本地机器上运行。
  • 我不认为将你的 virtualenv 放在 /root 中是个好主意,因为你的服务器进程将无法访问它,除非它们以超级用户权限运行。
  • 我只有一个用户,root。所以我的进程也将在 root 下运行

标签: python django apache mod-wsgi wsgi


【解决方案1】:

两个潜在的错误

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 文件是模块“设置”,而不是“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()

【讨论】:

  • 建议示例的主要内容主要由 virtualenv bin 文件夹中的 activate_this.py 实现。有许多使用 activate_this.py 讨论的问题/答案。例如,请参阅link
猜你喜欢
  • 2014-10-26
  • 1970-01-01
  • 2012-08-22
  • 2011-01-25
  • 2013-08-03
  • 2014-07-15
  • 2011-03-28
  • 2011-05-04
  • 2013-12-24
相关资源
最近更新 更多