【问题标题】:apache cannot import setings with django projectapache 无法使用 django 项目导入设置
【发布时间】:2014-05-16 13:04:14
【问题描述】:

我想将 django 与 apache 一起使用。我的目录树如下

/home/avlahop/development/django/rhombus2
├── accounts
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── models.py
│   ├── tests.py
│   ├── urls.py
│   ├── urls.pyc
│   ├── views.py
│   └── views.pyc
├── admin
│   ├── css
│   ├── img
│   │   ├── gis
│   └── js
├── customer
│   ├── migrations
│   ├── models.py
│   ├── models.pyc
│   ├── views.py
│   └── views.pyc
├── __init__.py
├── __init__.pyc
├── manage.py
├── media
├── mycal
├── mypil
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── models.py
│   ├── tests.py
│   ├── urls.py
│   ├── urls.pyc
│   ├── views.py
│   └── views.pyc
├── rhombus
│   ├── api.py
│   ├── api.pyc
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── rhombus.db
│   ├── settings.py
│   ├── settings.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
├── scripts
│   ├── create_data_files.py
│   ├── create_data_files.pyc
│   ├── find_server.py
│   ├── __init__.py
│   ├── __init__.pyc
│   └── populatedb.py
├── settings
├── static
├── tastypie
├── templates

wsgi.py

import os, sys

root = os.path.join(os.path.dirname(__file__),'..')
sys.path.insert(0, root)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "rhombus.settings")



# This application object is used by any WSGI server configured to use this
# file. This includes Django's development server, if the WSGI_APPLICATION
# setting points here.
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

# Apply WSGI middleware here.
# from helloworld.wsgi import HelloWorldApplication
# application = HelloWorldApplication(application)

我的 site.conf 文件有以下代码(并且已启用)

WSGIPythonPath /home/avlahop/development/django/rhombus2
<VirtualHost *:80>
    ServerAdmin webmast@rhombus.com
    ServerName myrhombus.com
    ServerAlias www.myrhombus.com
    DocumentRoot /home/avlahop/development/django/rhombus2
    WSGIScriptAlias / /home/avlahop/development/django/rhombus2/rhombus/wsgi.py
    <Directory /home/avlahop/development/django/rhombus2/rhombus>
        <Files wsgi.py>
            Require all granted
        </Files>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>

/home/avlahop/development/django/rhombus2 下的所有内容(包括 root)都被 chmod 为 755,这是其他人的 rx。但我收到以下错误

ImportError: Could not import settings 'rhombus.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named rhombus.settings

这里出了什么问题。我在另一个站点(另一个 conf 文件中的另一个虚拟主机)有同样的事情,但我被拒绝了权限。我复制粘贴了上面的代码,并更改了路径。所以 2 个站点相同的代码,不同的错误。我真的很沮丧。我不想去 php....php 太容易部署了...

【问题讨论】:

    标签: python django apache mod-wsgi


    【解决方案1】:

    Django documentation 中所述,您应该指定python 路径如下:

    WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
    WSGIPythonPath /path/to/mysite.com
    

    作为替代方案,您可能希望将WSGIDaemonProcess 指令与python_path argument 一起使用(这在Django docs 中也有说明)

    WSGIDaemonProcess [other args] python-path=/path/to/mysite.com
    

    在您的情况下,/path/to/mysite.com 应该是 /home/avlahop/development/django/rhombus2

    另外,请注意,将源代码放在 DocumentRoot 下被认为是一种不好的做法,因为 (1) 不需要运行站点,并且 (2) 在某些情况下允许攻击者访问源代码。如果我没记错的话,只有静态文件应该位于 DocumentRoot 下。

    【讨论】:

    • 但我有。查看我的 site.conf 的第一行。我应该在 DocumentRoot 里面放什么?只有模板和静态?这是否意味着我需要更改我的设置文件中的模板文件夹?大概吧。这不是很灵活。如果我有许多具有不同 WSGIPythonPath 的虚拟主机怎么办。我不能把它们放在一个conf中。对吗?
    • 尝试在文件系统树上上下移动WSGIPythonPath。模板也应该在 DocumentRoot 之外。如果我没记错的话,模板文件夹应该有相对路径,所以除非你指定绝对路径,否则你不必更改它。如果你有很多虚拟主机,你应该为每个虚拟主机配置 mod_wsgi。
    • 此外,如果由于某些错误而无法导入设置文件,则会出现相同的错误。尝试切换到该 virtualenv,运行交互式 python 控制台并导入您的设置文件 - 可能会有一些错误
    • 我试过......同样的错误......我可以在一个 conf 文件中设置多少个 WSGIPythonPath?我猜一个。因此,为不同站点设置一个 conf 的唯一方法是将它们的 wsgi.py 文件放在一个与 django 代码分开的目录中。我不在 virtualenv 中,普通的 python。如果我运行 ./manage.py shell,我可以正常导入 rhombus.settings。它适用于 django 的测试网络服务器
    • 即使在普通的交互式 python shell 中,我也可以导入 rhombus.settings。 /home/avlahop/development/django/rhombus2/mycal 在 PYTHONPATH 中(我不知道为什么)。
    猜你喜欢
    • 1970-01-01
    • 2013-05-16
    • 1970-01-01
    • 2014-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多