【问题标题】:Django - Apache2 Internal Server ErrorDjango - Apache2 内部服务器错误
【发布时间】:2018-11-23 23:02:01
【问题描述】:

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

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

settings.py 中的 allowed_hosts 如下所示:

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

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

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

当我尝试访问 localhost 时出现此错误:

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

这里是apache2error.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

非常感谢。

【问题讨论】:

  • 尝试运行 sudo chmod -R 777 /home/toto/Desktop/dash_test 并重新加载 apache
  • 谢谢我确实运行了命令,但我仍然有同样的错误
  • 您是否在编辑配置后重新加载了 Apache?
  • 是的,我做了 /etc/init.d/apache2 restart

标签: django python-2.7 ubuntu apache2 mod-wsgi


【解决方案1】:

代替:

<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 文件的实际路径是什么。

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

【讨论】:

  • wsgi.py文件的实际路径为:/home/toto/Desktop/dash_test/dash_test/wsgi.py
  • 我确实更改了 WSGIScriptAlias 中的路径,但我仍然有同样的错误
  • 我编辑了我的问题,因为重新加载服务器后我收到 500 内部服务器错误
  • 什么是/home/toto/Desktop/dash_test/developer。因为您已将python-home 设置为该值,所以预计那是一个 Python 虚拟环境,因此必须在其中安装 Django。那真的是Python虚拟环境的根目录吗?在您的虚拟环境中运行python,看看import sys; sys.prefix 为您提供了什么。它提供的那个目录应该是python-home 设置的目录。详情在modwsgi.readthedocs.io/en/develop/user-guides/…
  • 在虚拟环境中运行python,如果你运行import django; django.__file__,你会得到什么?
【解决方案2】:

我使用 Django 3.x 和 Apache 2.4 将我的项目上传到本地主机,我确实要测试我的项目和 Apache,但我遇到了同样的问题,当我检查我的 Apache error.log 时它是:“ModuleNotFoundError:没有名为 'django' 的模块"

配置文件似乎没问题,当我检查它时:“sudo apache2ctl configtest”

但我使用这两个命令来解决问题

  sudo a2dissite 000-default
  sudo a2ensite 000-default

和 sudo systemctl 重新加载 apache2

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-23
    • 2016-08-31
    • 2011-08-08
    • 2015-09-30
    • 2019-11-25
    • 2014-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多