繁体   English   中英

使用带w_mod_wsgi和Apache的Django

[英]Using Django w/ mod_wsgi and Apache

我一直在尝试弄清楚如何在准备好部署时在生产环境上设置Django应用程序。 我正在使用Django v1.11,而我的EC2正在运行Ubuntu 14.04。 我一直试图将本指南作为参考,但是它并不是特定于Ubuntu的,因此在这方面我遇到了一些困难。 我还提到了其他一些资源,但是其中的许多内容似乎已经过时了。

我在本地计算机上设置了主机规则,将www.example.com指向EC2实例的公共IP地址。

我有一个virtualenv设置,它位于/home/django/example.com/ENV 我的Django项目直接位于/home/django/example.com 项目名称是mysite ,并且是使用django-admin startproject mysite生成的,因此它在/home/django/example.com/mysite目录中具有默认的wsgi.py文件。 wsgi.py的内容如下所示:

"""
WSGI config for mysite project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

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

application = get_wsgi_application()

我尝试添加VirtualHost规则,例如:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com

    WSGIScriptAlias / /home/django/example.com/mysite/wsgi.py

    <Directory "/home/django/example.com/mysite">
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>
</VirtualHost>

同样,我尝试添加:

Include /etc/apache2/httpd.conf

/etc/apache/apache2.conf并删除以下内容:

WSGIScriptAlias / /home/django/example.com/mysite/wsgi.py
WSGIPythonHome /home/django/example.com/ENV
WSGIPythonPath /home/django/example.com

<Directory /home/django/example.com/mysite>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>

进入httpd.conf

无论哪种情况,我之后都直接重新启动了Apache服务器。

除了击中“ 500 Internal Server Error”或击中“ ERR_EMPTY_RESPONSE”,我别无他法。

任何人都可以阐明以下内容:a)我要去哪里错了,b)我可以参考最新的说明,或者c)我如何解决这个问题?

经过大量的故障排除并咨询了格雷厄姆评论中提到的资源之后,这是我在VirtualHost中需要的内容:

<VirtualHost *:80>
        ServerName example.com
        ServerAlias www.example.com

        Alias /static /home/django/example.com/static

        <Directory /home/django/example.com/static>
           Require all granted
        </Directory>

        WSGIDaemonProcess mysite python-path=/home/django/example.com:/home/django/ENV/lib/python3.4/site-packages

        WSGIProcessGroup mysite
        WSGIApplicationGroup %{GLOBAL}

        WSGIScriptAlias / /home/django/example.com/mysite/wsgi.py

        <Directory /home/django/example.com/mysite>
            Require all granted
        </Directory>
</VirtualHost>

这是我确定的wsgi.py的内容:

"""
WSGI config for mysite project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/
"""

import os, sys

from django.core.wsgi import get_wsgi_application

path = '/home/django/example.com'
if path not in sys.path:
    sys.path.append(path)

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

并且还值得注意(仅因为对我而言不是立即显而易见的),有必要指定:

STATIC_ROOT = '/home/django/example.com/static'

settings.py中运行python manage.py collectstatic将所有应用程序中的静态文件python manage.py collectstaticSTATIC_ROOT

希望以后对其他人有帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM