Django - Apache2 内部服务器错误

3
我在/home/toto/Desktop下创建了一个名为dash_test的django项目,并希望使用Apache2和mod_wsgi来服务我的Django应用程序。
我使用以下命令安装了Apache Web服务器、mod_wsgi和pip:sudo apt-get install python-pip apache2 libapache2-mod-wsgi,并在项目目录中创建了一个名为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>

当我尝试访问本地主机时,出现以下错误:
服务器内部错误 服务器遇到了内部错误或配置错误,并且无法完成您的请求。 请联系服务器管理员 [未提供地址],告知他们出现此错误的时间以及在此错误之前执行的操作。 有关此错误的更多信息可能会在服务器错误日志中提供。 Apache/2.4.18 (Ubuntu) 服务器位于本地主机端口80。
下面是apache2错误日志的内容:
[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。 - Ykh
谢谢,我已经运行了该命令,但是我仍然遇到相同的错误。 - A. Arfaoui
你编辑配置文件后重新加载了Apache吗? - Daniel Roseman
是的,我执行了 /etc/init.d/apache2 restart。 - A. Arfaoui
2个回答

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

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


我已经更改了WSGIScriptAlias中的路径,但仍然出现相同的错误。 - A. Arfaoui
我修改了我的问题,因为在重新加载服务器后,我遇到了500内部服务器错误。 - A. Arfaoui
1
“/home/toto/Desktop/dash_test/developer”是什么?因为您已将“python-home”设置为该目录,所以预期它是Python虚拟环境,因此Django必须安装在其中。那真的是Python虚拟环境的根目录吗?从您的虚拟环境中运行“python”,并查看“import sys; sys.prefix”给出的内容。它给出的目录应该是“python-home”设置的目录。详情请参见http://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html。 - Graham Dumpleton
1
当你在虚拟环境中运行“python”时,如果你输入“import django; django.__file__”,你会得到什么? - Graham Dumpleton
你说得对,我忘了在它上面安装Django。现在我已经安装了,并且看起来安装成功了。非常感谢你的帮助,我不知道它是如何逃过我的注意力的。现在我的应用页面可以在本地主机上运行了。再次感谢。 - A. Arfaoui
显示剩余2条评论

0

我使用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 reload apache2


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接