如何使用Django与Apache和mod_wsgi

Apachemod_wsgi 部署Django项目是一个第三方的,测试的方法来得到Django生产环境。

mod_wsgi是一个Apache模块,可以托管任何Python WSGI应用程序,包括Django。 Django将与支持mod_wsgi的任何版本的Apache一起工作。

官方mod_wsgi文档是有关如何使用mod_wsgi的所有细节的来源。 您可能需要先从安装和配置文档开始。

Basic configuration

一旦您安装并激活了mod_wsgi,请编辑Apache服务器的httpd.conf文件并添加以下内容。 如果你的Apache版本低于2.4, 请将 Require all granted 替换成 Allow from all 并在上一行添加 Order deny,allow .

WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
WSGIPythonHome /path/to/venv
WSGIPythonPath /path/to/mysite.com

<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>

WSGIScriptAlias行中的第一个位是您要在其上(/指示根URL)的服务应用程序的基本URL路径,第二个位置是“ WSGI文件“ - 见下面 - 在您的系统上,通常在您的项目包(在本例中mysite)。 这告诉Apache使用该文件中定义的WSGI应用程序来提供给定URL下面的任何请求。

如果您在virtualenv中安装项目的Python依赖项,请使用WSGIPythonHome添加到virtualenv的路径。 有关详细信息,请参阅mod_wsgi virtualenv指南

The WSGIPythonPath line ensures that your project package is available for import on the Python path; in other words, that import mysite works.

The <Directory> piece just ensures that Apache can access your wsgi.py file.

接下来,我们需要确保这个wsgi.py与WSGI应用程序对象存在。 从Django版本1.4起,startproject将为您创建一个;否则,您需要创建它。 请参阅WSGI overview documentation以获取您应该放入此文件的默认内容,以及您可以添加到其中的其他内容。

警告

如果多个Django站点在单个mod_wsgi进程中运行,则所有这些站点将使用首先运行的设置。 这可以通过改变:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{{ project_name }}.settings")

wsgi.py中:

os.environ["DJANGO_SETTINGS_MODULE"] = "{{ project_name }}.settings"

或者通过using mod_wsgi daemon mode,并确保每个站点在其自己的守护进程中运行。

修正UnicodeEncodeError进行文件上传

如果在使用包含非ASCII字符的文件名的文件上传时,您会得到一个UnicodeEncodeError,请确保Apache配置为接受非ASCII文件名:

export LANG='en_US.UTF-8'
export LC_ALL='en_US.UTF-8'

放置此配置的常见位置是/etc/apache2/envvars

有关详细信息,请参阅Unicode参考指南的Files部分。

使用mod_wsgi守护进程模式

“Daemon模式”是运行mod_wsgi的推荐模式(在非Windows平台上)。 需要通过WSGIDaemonProcessWSGIProcessGroup指令来实现Django实例运行在守护进程组中 如果使用守护程序模式,则上述配置所需的进一步更改是您不能使用WSGIPythonPath;而应使用python-pathWSGIDaemonProcess选项,例如:

WSGIDaemonProcess example.com python-home=/path/to/venv python-path=/path/to/mysite.com
WSGIProcessGroup example.com

如果要在子目录(本例中为https://example.com/mysite)中提供项目,可以将WSGIScriptAlias添加到上述配置中:

WSGIScriptAlias /mysite /path/to/mysite.com/mysite/wsgi.py process-group=example.com

有关设置守护进程模式的详细信息,请参阅官方mod_wsgi文档。

Serving files

Django不提供文件本身;它将该作业留给您选择的任何Web服务器。

我们建议使用单独的Web服务器(即不运行Django的服务器)来提供媒体。 这里有一些很好的选择:

但是,如果您无法选择在与Django相同的Apache VirtualHost上提供媒体文件,则可以将Apache设置为将某些网址用作静态媒体,而将其他网址用于Django的mod_wsgi接口。

This example sets up Django at the site root, but serves robots.txt, favicon.ico, and anything in the /static/ and /media/ URL space as a static file. 所有其他网址将使用mod_wsgi:

Alias /robots.txt /path/to/mysite.com/static/robots.txt
Alias /favicon.ico /path/to/mysite.com/static/favicon.ico

Alias /media/ /path/to/mysite.com/media/
Alias /static/ /path/to/mysite.com/static/

<Directory /path/to/mysite.com/static>
Require all granted
</Directory>

<Directory /path/to/mysite.com/media>
Require all granted
</Directory>

WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py

<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>

如果你的Apache版本低于2.4, 请将 Require all granted 替换成 Allow from all 并在上一行添加 Order deny,allow .

Serving the admin files

django.contrib.staticfiles位于INSTALLED_APPS中时,Django开发服务器会自动提供管理应用程序(以及任何其他已安装的应用程序)的静态文件。 但是,当您使用任何其他服务器布局时不是这样。 您负责设置Apache,或您使用的任何Web服务器,以提供管理文件。

管理文件位于Django发行版的django/contrib/admin/static/admin中。

We strongly recommend using django.contrib.staticfiles to handle the admin files (along with a Web server as outlined in the previous section; this means using the collectstatic management command to collect the static files in STATIC_ROOT, and then configuring your Web server to serve STATIC_ROOT at STATIC_URL), but here are three other approaches:

  1. 在文档根目录中创建一个指向管理静态文件的符号链接(这可能需要Apache配置中的+FollowSymLinks)。
  2. 使用如上所示的Alias指令,将适当的网址(可能是STATIC_URL + admin/)别名到管理文件的实际位置。
  3. 复制admin静态文件,使它们存在于Apache文档根目录下。

Authenticating against Django’s user database from Apache

Django提供了一个处理程序,允许Apache直接对Django的身份验证后端进行身份验证。 请参阅mod_wsgi authentication documentation