如何使用WSGI部署Django

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用WSGI部署Django相关的知识,希望对你有一定的参考价值。

参考技术A 如何使用Apache 和mod_wsgi 部署Django¶
用Apache 和 mod_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, 请将 Requireall 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 importmysite 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.X。python3.4)。
采用mod_wsgi守护进程模式¶
“Daemon模式”是运行mod_wsgi的推荐模式(在非Windows平台上)。需要通过WSGIDaemonProcess和WSGIProcessGroup指令来实现Django实例运行在守护进程组中如果使用守护程序模式,则上述配置所需的进一步更改是您不能使用WSGIPythonPath;而应使用WSGIDaemonProcess的python-path选项,例如:
WSGIDaemonProcess example.com python-path=/path/to/mysite.com:/path/to/venv/lib/python2.7/site-packages
WSGIProcessGroup example.com

如果您要在子目录(本示例中为)中投放您的项目,可以将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的服务器)来提供媒体。这里有一些很好的选择:
nginx
Apache的精简版本
但是,如果您无法选择在与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 Orderdeny,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:
在文档根目录中创建一个指向管理静态文件的符号链接(这可能需要Apache配置中的+FollowSymLinks)。
使用如上所示的Alias指令,将适当的网址(可能是STATIC_URL + admin/)别名到管理文件的实际位置。
复制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。
目录
如何使用Django与Apache和mod_wsgi
基本配置
使用virtualenv
使用mod_wsgi守护程序模式
提供文件
提供管理文件
对来自Apache的Django用户数据库进行身份验证
如果您得到UnicodeEncodeError
浏览
上一页:如何使用WSGI部署
下一步:对来自Apache的Django用户数据库进行身份验证
你在这里:
Django 1.8.2.dev20150513143415 documentation
部署Django
如何使用Django与Apache和mod_wsgi
如何使用WSGI进行部署
“操作指南”
这一页
显示源
快速搜索
输入搜索字词或模块,类或函数名称。
最后更新:
2015年5月13日

使用 DotCloud 使用 apache + mod_wsgi + postgresql + nginx + memchache 部署 Django 应用程序

【中文标题】使用 DotCloud 使用 apache + mod_wsgi + postgresql + nginx + memchache 部署 Django 应用程序【英文标题】:Deploy Django app with apache + mod_wsgi + postgresql + nginx + memchache using DotCloud 【发布时间】:2011-09-24 20:41:45 【问题描述】:

我是 django 开发和整体 Web 应用程序开发的新手,但我是一名程序员。

我想知道在 DotCloud 上部署 django 应用程序的步骤是什么,它将在带有 mod_wsgi 的 apache 服务器上运行。 Nginx 将用于提供媒体文件,而 Postgresql 将用于数据库。这将与 memchache 一起使用。

本教程:http://dev.lethain.com/the-django-and-ubuntu-intrepid-almanac/ 基本上可以满足我的需求,但我想弄清楚如何使用 DotCloud 实现这一目标。

我认为 Dotcloud 文档不够完整,无法提供完成此任务所需的信息,因此希望有人能提供信息。

感谢您的帮助与合作。我很感激。

【问题讨论】:

【参考方案1】:

幸运的是,有人在how to deploy a django application on dotcloud 上为您完成了艰苦的工作。

【讨论】:

感谢您的链接。实际上我已经遇到过这个问题,它有助于重申 dotcloud 文档中提到的关键步骤。但是,它不符合我的需要。据我所知,他只是使用 nginx 和 mysql 来部署 django 应用程序。尽管从本教程中仍然可以学到一些东西。谢谢:) @amirrustam。相信你会发现DotCloud使用的是nginx/uWSGI,所以你不能使用Apache/mod_wsgi。因此,引用的文档将是这样做的方式。为什么你特别认为你需要使用 Apache? 哇,这真的解决了一些问题。我想使用 apache,因为它是 django 文档中的部署建议之一,而且我从 php 时代就一直使用它。 reddit 上有人推荐 nginx+uWSGI 也是不错的选择。我很高兴你告诉我这个,因为我昨晚正在阅读 nginx+uWSGI。【参考方案2】:

如果你想在 python 中使用 postgreSQL,你必须安装 psycopg 模块,查看文档here

【讨论】:

感谢您的提示。我不知道这件事。我希望从这个问题线程中收集尽可能多的信息,让一个实际的生产应用程序运行起来,然后发布一个可靠的文档,以便其他人可以受益。

以上是关于如何使用WSGI部署Django的主要内容,如果未能解决你的问题,请参考以下文章

如何部署Apache2+mod_wsgi+flask?

如何在 Heroku 上使用 Channels 和 Celery 部署 Django?

django和wsgi的问题。

无法使用 mod-wsgi 在 Apache 上部署 django

使用 mod_wsgi 部署 django3 项目

如何使用wsgi在centos上运行django