【问题标题】:I've issue in setting up Django with Apache using mod_wsgi我在使用 mod_wsgi 与 Apache 设置 Django 时遇到问题
【发布时间】:2019-01-26 11:04:36
【问题描述】:

我已经尝试了这么多,感觉我被卡住了,没有得到任何地方 Apache 给出的错误是

ModuleNotFoundError: 没有名为“wearpretty.settings”的模块

我正在使用 蟒蛇3.6, Django2.1, 阿帕奇2, libapache2-mod-wsgi-py3

以下是本文使用的文件。

wsgi.py

​​>
import os

from django.core.wsgi import get_wsgi_application

os.environ["DJANGO_SETTINGS_MODULE"] = "wearpretty.settings"
application = get_wsgi_application()

virtualhost.conf

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName lhwearpretty.com
    ServerAlias www.lhwearpretty.com
    DocumentRoot /var/www/myprojects/wearpretty/wearpretty/wearpretty

    <Directory /var/www/myprojects/wearpretty/wearpretty/wearpretty>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    WSGIDaemonProcess lhwearpretty.com python-home=/var/www/myprojects/wearpretty/venv python-path=/var/www/myprojects/wearpretty
    WSGIProcessGroup lhwearpretty.com
    WSGIScriptAlias / /var/www/myprojects/wearpretty/wearpretty/wearpretty/wsgi.py

    Alias /static/ /var/www/myprojects/wearpretty/wearpretty/static/ 

    <Directory /var/www/myprojects/wearpretty/wearpretty/static> 
        Require all granted 
    </Directory>
</VirtualHost>

【问题讨论】:

    标签: django python-3.x apache mod-wsgi


    【解决方案1】:

    您的 DocumentRoot 应该是一个文件夹(减去 1 /wearpretty)并且 WSGIDaemonProcess 中的 python-path var 应该是一个文件夹(加 1 /wearpretty) :它们必须指向你的 Django 项目根目录。

    <VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerName lhwearpretty.com
        ServerAlias www.lhwearpretty.com
        DocumentRoot /var/www/myprojects/wearpretty/wearpretty
    
        <Directory /var/www/myprojects/wearpretty/wearpretty/wearpretty>
            <Files wsgi.py>
                Require all granted
            </Files>
        </Directory>
    
        WSGIDaemonProcess lhwearpretty.com python-home=/var/www/myprojects/wearpretty/venv python-path=/var/www/myprojects/wearpretty/wearpretty
        WSGIProcessGroup lhwearpretty.com
        WSGIScriptAlias / /var/www/myprojects/wearpretty/wearpretty/wearpretty/wsgi.py
    
        Alias /static /var/www/myprojects/wearpretty/wearpretty/static/ 
    
        <Directory /var/www/myprojects/wearpretty/wearpretty/static> 
            Require all granted 
        </Directory>
    </VirtualHost>
    

    另外,我建议添加此目录节点:

    <Directory /var/www/myprojects/wearpretty/wearpretty>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
    

    【讨论】:

    • 我也试过了,几乎每条路径我都试过,从 /var/www/myprojects/wearpretty/ 到 /var/www/myprojects/wearpretty/wearpretty/wearpretty
    • 但幸运的是我知道我的项目发生了什么。
    • 太棒了。去吧!
    • 您错过了其他事件,例如 Directory 指令,您可以在其中告诉 wsgi.py 应该在哪里查看。很难使用相同名称的深度文件夹。欢呼
    【解决方案2】:

    当我自己开始时,我发现我的目录命名存在一些问题。 任何第一次开始的人都应该知道您不能使用您的站点目录名称作为您的项目名称。 所以我改了。

    旧结构

    Site Dir = /var/www/myprojects/wearpretty
    Project Dir = /var/www/myprojects/wearpretty/wearpretty
    venv = /var/www/myprojects/wearpretty/venv
    

    新结构

    Site Dir = /var/www/myprojects/wearpretty
    Project Dir = /var/www/myprojects/wearpretty/mysite
    venv = /var/www/myprojects/wearpretty/venv
    

    Virtualhost.conf

    <VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerName lhwearpretty.com
        ServerAlias www.lhwearpretty.com
        DocumentRoot /var/www/myprojects/wearpretty/mysite
    
    
        WSGIDaemonProcess lhwearpretty.com python-home=/var/www/myprojects/wearpretty/wearprettyenv python-path=/var/www/myprojects/wearpretty/mysite
        WSGIProcessGroup lhwearpretty.com
        WSGIScriptAlias / /var/www/myprojects/wearpretty/mysite/mysite/wsgi.py
    
    
        <Directory /var/www/myprojects/wearpretty/mysite>
            <Files wsgi.py>
                Require all granted
            </Files>
        </Directory>
    
        Alias /static/ /var/www/myprojects/wearpretty/mysite/static/ 
    
        <Directory /var/www/myprojects/wearpretty/mysite/static> 
            Require all granted 
        </Directory>
    </VirtualHost>
    

    希望这对其他人也有帮助。

    【讨论】:

    • 实际上,您在很多地方的多个文件夹深度使用相同的名称,您错过了它们。上面的@DKH 尝试并接近解决方案,但错过了其他事件,例如Directory 指令,您可以在其中告诉wsgi.py 应该在哪里查看。更改名称只是清除了命名混乱。欢呼;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-11
    • 1970-01-01
    • 2011-02-13
    • 2011-04-01
    • 1970-01-01
    • 2023-03-03
    相关资源
    最近更新 更多