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