Apache VirtualHost 后面带有 WSGI 的 Django 给出 404

Django with WSGI behind Apache VirtualHost gives 404

我正在尝试 运行 Apache 服务器上的 Django 应用程序,在使用 WSGI 的虚拟主机中。

我完全遵循了 Django 文档,但这里似乎仍然存在一些问题。

据我所知,我已经正确配置了所有内容。

wsgi.py:

import os
import sys

path='/var/www/docs/'
sys.path.append(path)

os.environ["DJANGO_SETTINGS_MODULE"] = "DocsEngine.settings"

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

虚拟主机文件:

Listen 8000

WSGIPythonPath /var/www/docs

<VirtualHost *:8000>
    WSGIScriptAlias / /var/www/docs/DocsEngine/wsgi.py

    ServerName myurl.com
    Alias /resources/ /var/www/docs/resources/

    <Directory /var/www/docs/DocsEngine>
        <Files wsgi.py>
            Require all granted
        </Files> 
    </Directory>

    CustomLog /var/log/apache2/wsgi-access.log combined  
    ErrorLog  /var/log/apache2/wsgi-error.log

</VirtualHost>

导航到 myurl.com:8000/ 时出现 404 页面。

我可以访问我的 /resources 目录中的内容,所以我知道 apache 可以识别其中的一部分。

我已经为此苦苦思索了几个小时,如有任何帮助,我们将不胜感激。

请查看这些步骤: 创建新文件:

sudo nano /etc/apache2/sites-available/domain_name

将这些行写入您的新文件。

<VirtualHost *:8000>
        ServerAdmin yogesh@cisinlabs.com
        ServerName myurl.com
        ServerAlias myurl.com
        WSGIScriptAlias / /var/www/docs/DocsEngine/wsgi.py

        Alias /static/ /home/cis/DjangoLive/cismailer/static/
        <Location "/static/">
            Options -Indexes
        </Location>
</VirtualHost>

然后运行:

sudo a2ensite domain_name

然后打开文件:

sudo nano /etc/apache2/ports.conf

在此文件中更改端口:

NameVirtualHost *:8000
Listen 8000

然后sudo service apache2 restart

希望这对你有用。

如果您使用 virtualenv,您的 wsgi 脚本文件如下所示:

import os
import sys
import site

# Add the site-packages of the chosen virtualenv to work with

site.addsitedir('/path/of/your/env/lib/python2.7/site-packages')

# Add the app's directory to the PYTHONPATH
sys.path.append('/var/www/docs/DocsEngine')
sys.path.append('/var/www/docs/DocsEngine/DocsEngine')


# Activate your virtual env
activate_env=os.path.expanduser("/path/of/your/env/bin/activate_this.py")

execfile(activate_env, dict(__file__=activate_env))

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "DocsEngine.settings")


import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

这里有一些类似的东西,你可以用它来获取更多信息: http://thecodeship.com/deployment/deploy-django-apache-virtualenv-and-mod_wsgi/

https://www.digitalocean.com/community/tutorials/how-to-run-django-with-mod_wsgi-and-apache-with-a-virtualenv-python-environment-on-a-debian-vps

经过大约 14 个小时的 Google-fu 和大量的失败,我终于找到了解决方案。显然他们在 Django 1.7 和 Apache 2.4 中进行了未记录的重大更改。

首先,我需要从 Apache 中禁用 mod_python。显然,当同时启用 WSGI 时,它会导致 silent 问题。我一禁用它,我的日志就开始爆炸,谢天谢地。这是 link 我在向我描述此解决方案的地方找到的:http://marc-abramowitz.com/archives/2012/09/28/some-tips-for-setting-up-apache-and-mod_wsgi/

鉴于禁用 mod_python 时发现的错误,我发现 Django 的 WSGI.py 文件需要以这种方式初始化,与文档中描述的不同:

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

https://www.pythonanywhere.com/forums/topic/1629/#id_post_11030

希望这对以后遇到此问题的可怜人有所帮助。