首页 > 解决方案 > 我在使用 mod_wsgi 与 Apache 设置 Django 时遇到问题

问题描述

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

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

我正在使用 python 3.6、Django2.1、apache2、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 *: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>

标签: djangopython-3.xapachemod-wsgi

解决方案


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

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

推荐阅读