使用带 mod_wsgi 和 Apache 的 Django
Using Django w/ mod_wsgi and Apache
我一直在努力弄清楚如何在准备好部署时在生产环境中设置 Django 应用程序。我使用的是 Django v1.11,我的 EC2 是 运行 Ubuntu 14.04。我一直试图将 this guide 作为参考,但它并不特定于 Ubuntu,所以我在这方面遇到了一些困难。我参考了其他几个资源,但其中的大部分内容似乎都已过时。
我在本地计算机上设置了主机规则,将 www.example.com
指向我的 EC2 实例的 public IP 地址。
我在 /home/django/example.com/ENV
中设置了一个 virtualenv。我的 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 内部服务器错误”或点击 "ERR_EMPTY_RESPONSE"。
任何人都可以阐明 a) 我哪里出错了,b) 我可以参考最新说明的地方,或者 c) 我如何解决这个问题?
经过大量故障排除并参考了 Graham 评论中提到的资源,这是我在 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'
in settings.py
,然后 运行 python manage.py collectstatic
从所有应用程序中收集静态文件到 STATIC_ROOT
.
我希望这对以后的其他人有所帮助!
我一直在努力弄清楚如何在准备好部署时在生产环境中设置 Django 应用程序。我使用的是 Django v1.11,我的 EC2 是 运行 Ubuntu 14.04。我一直试图将 this guide 作为参考,但它并不特定于 Ubuntu,所以我在这方面遇到了一些困难。我参考了其他几个资源,但其中的大部分内容似乎都已过时。
我在本地计算机上设置了主机规则,将 www.example.com
指向我的 EC2 实例的 public IP 地址。
我在 /home/django/example.com/ENV
中设置了一个 virtualenv。我的 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 内部服务器错误”或点击 "ERR_EMPTY_RESPONSE"。
任何人都可以阐明 a) 我哪里出错了,b) 我可以参考最新说明的地方,或者 c) 我如何解决这个问题?
经过大量故障排除并参考了 Graham 评论中提到的资源,这是我在 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'
in settings.py
,然后 运行 python manage.py collectstatic
从所有应用程序中收集静态文件到 STATIC_ROOT
.
我希望这对以后的其他人有所帮助!