如何使用Apache 和mod_wsgi 部署Django

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
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下面的任何请求。

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,并确保每个站点在其自己的守护进程中运行。

使用虚拟机

If you install your project’s Python dependencies inside a virtualenv, you’ll need to add the path to this virtualenv’s directory to your Python path as well. 如果你的python项目在一个依赖虚拟机的python环境中,你需要将路径添加虚拟机的site-packages目录到你的python路径To do this, add an additional path to your WSGIPythonPath directive, with multiple paths separated by a colon (:) if using a UNIX-like system, or a semicolon (;) if using Windows.如果目录路径的任何部分包含空格字符,则必须引用WSGIPythonPath的完整参数字符串:

WSGIPythonPath /path/to/mysite.com:/path/to/your/venv/lib/python3.X/site-packages

请确保为您的virtualenv指定正确的路径,并将正确的Python版本替换为python3.Xpython3.4)。

采用mod_wsgi守护进程模式

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

WSGIDaemonProcess example.com python-path=/path/to/mysite.com:/path/to/venv/lib/python2.7/site-packages
WSGIProcessGroup example.com

如果您要在子目录(本示例中为http://example.com/mysite)中投放您的项目,可以将WSGIScriptAlias添加到上面的配置中:

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

See the official mod_wsgi documentation for details on setting up daemon mode.

Serving files

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

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

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

This example sets up Django at the site root, but explicitly serves robots.txt, favicon.ico, any CSS file, 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>

If you are using a version of Apache older than 2.4, replace Require all granted with Allow from all and also add the line Order deny,allow above it.

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

If you get a UnicodeEncodeError

如果您正在利用Django的国际化功能(请参阅Internationalization and localization),并且您打算允许用户上传文件,则必须确保用于启动Apache的环境配置为接受非-ASCII文件名。如果未正确配置环境,则在调用类似于os.path中的函数时,将触发UnicodeEncodeError异常,该函数包含非ASCII字符。

为了避免这些问题,用于启动Apache的环境应包含类似于以下内容的设置:

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

请查阅您的操作系统的文档以获取适当的语法和位置来放置这些配置项; /etc/apache2/envvars是Unix平台上的常见位置。将这些语句添加到环境后,重新启动Apache。