首页 > 解决方案 > Django - Apache2 内部服务器错误

问题描述

/home/toto/Desktop我在命名下创建了一个 django 项目dash_test,我想用 Apache2 和 mod_wsgi 为我的 Django 应用程序提供服务。

我使用此命令安装了 Apache Web 服务器、mod_wsgi 和 pip sudo apt-get install python-pip apache2 libapache2-mod-wsgi。我在项目目录中创建了一个名为developer.

中的 allowed_hostssettings.py如下所示:

ALLOWED_HOSTS = ['xxx.xxx.xxx.xxx', 'localhost', '127.0.0.1']`

我添加了 static_url 和 static_root:

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')

我确实跑了一个collectstatic

apache通过编辑配置000-default.conf

 <VirtualHost *:80>
    Alias /static /home/toto/Desktop/dash_test/static
    <Directory /home/toto/Desktop/dash_test/static>
        Require all granted
    </Directory>

    <Directory /home/toto/Desktop/dash_test/dash_test>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    WSGIDaemonProcess dash_test python-home=/home/toto/Desktop/dash_test/developer python-path=/home/toto/Desktop/dash_test
    WSGIProcessGroup dash_test
    WSGIScriptAlias / /home/toto/Desktop/dash_test/dash_test/wsgi.py
 </VirtualHost>

当我尝试访问本地主机时,出现此错误:

内部服务器错误 服务器遇到内部错误或配置错误,无法完成您的请求。请通过 [no address given] 联系服务器管理员,告知他们此错误发生的时间,以及您在此错误之前执行的操作。服务器错误日志中可能提供有关此错误的更多信息。Apache/2.4.18 (Ubuntu) 服务器在 localhost 端口 80

这是 apache2 的内容error.log

[Thu Jun 14 13:25:48.134132 2018] [wsgi:error] [pid 21611] [remote 127.0.0.1:15106] mod_wsgi (pid=21611): Target WSGI script '/home/toto/Desktop/dash_test/dash_test/wsgi.py' cannot be loaded as Python module.
[Thu Jun 14 13:25:48.134152 2018] [wsgi:error] [pid 21611] [remote 127.0.0.1:15106] mod_wsgi (pid=21611): Exception occurred processing WSGI script '/home/toto/Desktop/dash_test/dash_test/wsgi.py'.
[Thu Jun 14 13:25:48.134167 2018] [wsgi:error] [pid 21611] [remote 127.0.0.1:15106] Traceback (most recent call last):
[Thu Jun 14 13:25:48.134182 2018] [wsgi:error] [pid 21611] [remote 127.0.0.1:15106]   File "/home/toto/Desktop/dash_test/dash_test/wsgi.py", line 11, in <module>
[Thu Jun 14 13:25:48.134203 2018] [wsgi:error] [pid 21611] [remote 127.0.0.1:15106]     from django.core.wsgi import get_wsgi_application
[Thu Jun 14 13:25:48.134216 2018] [wsgi:error] [pid 21611] [remote 127.0.0.1:15106] ImportError: No module named django.core.wsgi

我正在使用 MySQL 和 phpMyAdmin 而不是 db.sqlite3

非常感谢。

标签: djangopython-2.7ubuntuapache2mod-wsgi

解决方案


代替:

<Directory /home/toto/Desktop/dash_test/dash_test>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>

利用:

<Directory /home/toto/Desktop/dash_test>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>

您使用了错误的目录。

那或者你有:

WSGIScriptAlias / /home/toto/Desktop/dash_test/wsgi.py

错误的,它的意思是:

WSGIScriptAlias / /home/toto/Desktop/dash_test/dash_test/wsgi.py

如果这是一个 Django 应用程序,它的位置wsgi.py实际上看起来不太正确。

文件的实际路径是什么wsgi.py

上面的目录参数应该是它所在的目录。


推荐阅读