[英]Using Django w/ mod_wsgi and Apache
I've been trying to work out how I'll go about setting up a Django application on production, when it's ready for deployment. 我一直在尝试弄清楚如何在准备好部署时在生产环境上设置Django应用程序。 I'm using Django v1.11, and my EC2 is running Ubuntu 14.04. 我正在使用Django v1.11,而我的EC2正在运行Ubuntu 14.04。 I have been attempting to refer to this guide as reference, however it is not specific to Ubuntu, so I've been experiencing a bit of difficulty in this regard. 我一直试图将本指南作为参考,但是它并不是特定于Ubuntu的,因此在这方面我遇到了一些困难。 I've referred to several other resources, but much of what is out there seems to be outdated. 我还提到了其他一些资源,但是其中的许多内容似乎已经过时了。
I have a host rule set up on my local machine, pointing www.example.com
to my EC2 instance's public IP address. 我在本地计算机上设置了主机规则,将www.example.com
指向EC2实例的公共IP地址。
I have a virtualenv set up which lives in /home/django/example.com/ENV
. 我有一个virtualenv设置,它位于/home/django/example.com/ENV
。 My Django project lives in /home/django/example.com
directly. 我的Django项目直接位于/home/django/example.com
。 The project name is mysite
, and was generated using django-admin startproject mysite
, thus it has the default wsgi.py
file inside the /home/django/example.com/mysite
directory. 项目名称是mysite
,并且是使用django-admin startproject mysite
生成的,因此它在/home/django/example.com/mysite
目录中具有默认的wsgi.py
文件。 The contents of wsgi.py
look like: 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()
I've tried adding VirtualHost rules such as the following: 我尝试添加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>
Similarly, I've tried adding: 同样,我尝试添加:
Include /etc/apache2/httpd.conf
to /etc/apache/apache2.conf
and chucking the following: 到/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>
into httpd.conf
. 进入httpd.conf
。
In either case, I've restarted the Apache server directly afterwards. 无论哪种情况,我之后都直接重新启动了Apache服务器。
I'm not getting any further than hitting "500 Internal Server Error" or hitting an "ERR_EMPTY_RESPONSE". 除了击中“ 500 Internal Server Error”或击中“ ERR_EMPTY_RESPONSE”,我别无他法。
Anyone able to shed some light around a) where I'm going wrong, b) where I can refer to for up-to-date instructions, or c) how I can troubleshoot this? 任何人都可以阐明以下内容:a)我要去哪里错了,b)我可以参考最新的说明,或者c)我如何解决这个问题?
After a lot of troubleshooting, and consulting with the resource mentioned in Graham's comment, here's what I established was required in my VirtualHost: 经过大量的故障排除并咨询了格雷厄姆评论中提到的资源之后,这是我在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>
and here are the contents of wsgi.py
that I settled on: 这是我确定的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")
And it is also worth noting (simply because it wasn't immediately obvious to me) that it is necessary to specify: 并且还值得注意(仅因为对我而言不是立即显而易见的),有必要指定:
STATIC_ROOT = '/home/django/example.com/static'
in settings.py
, and to run python manage.py collectstatic
to collect static files from all applications into said STATIC_ROOT
. 在settings.py
中运行python manage.py collectstatic
将所有应用程序中的静态文件python manage.py collectstatic
到STATIC_ROOT
。
I hope this helps somebody else, in future! 希望以后对其他人有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.