如何使用WSGI部署Django

如何使用WSGI部署Django,第1张

如何使用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 pathin 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 sectionthis 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日

首先要安装 flup ,

这是 Python 处理 FastCGI 的库。

FactCGI 采用 c/s 模型,独立的运行一个进程。在需要处理请求时,web 服务器(apache, httpd,..)直接和 FactCGI 进程进行通信即可。

web 服务器可通过两种办法和 FastCGI server 连接:

1. Unix domain socket(或 win32 的“命名管道")

2. TCP socket

通常 TCP socket 更简单,因为权限问题比较好配置。

如何启动 FactCGI 服务器:

到项目目录中,执行:

./manage.py runfcgi [options]

如果要看帮助:

./manage.py runfcgi help

在选项中,需要指定一个 socket 或 host 和 port 的组合,这样的话,当你启动 web server 时,就可以通过这些信息来连接到 FactCGI 服务器。

例子

在 TCP 端口中运行一个线程内服务器:

./manage.py runfcgi method=threaded host=127.0.0.1 port=3033

在 Unix domain socket 上运行一个 preforked 服务器:

./manage.py runfcgi method=prefork socket=/home/user/mysite.sock pidfile=django.pid

不作为后台进程执行(便于调试):

./manage.py runfcgi daemonize=false socket=/tmp/mysite.sock

如何停止后台的 FastCGI 进程:

1. 如果指定了 pidfile 属性,则可以这样:

kill `cat $PIDFILE`

其中 $PIDFILE 是你指定的 pidfile 选项。

要重启 Unix 上的后台 FactCGI 进程,可执行下列 shell 脚本:

#!/bin/bash

# Replace these three settings.

PROJDIR="/home/user/myproject"

PIDFILE="$PROJDIR/mysite.pid"

SOCKET="$PROJDIR/mysite.sock"

cd $PROJDIR

if [ -f $PIDFILE ]then

kill `cat -- $PIDFILE`

rm -f -- $PIDFILE

fi

exec /usr/bin/env - \

PYTHONPATH="../python:.." \

./manage.py runfcgi socket=$SOCKET pidfile=$PIDFILE

Apache 的设定:

1. 需要安装了 mod_fastcgi

2. 编辑 httpd.conf:

(1) 用 FastCGIExternalServer 指向 FastCGI 服务器的位置

可以用 socket 也可以用主机+端口的方式指定。例子:

# Connect to FastCGI via a socket / named pipe.

FastCGIExternalServer /home/user/public_html/mysite.fcgi -socket /home/user/mysite.sock

# Connect to FastCGI via a TCP host/port.

FastCGIExternalServer /home/user/public_html/mysite.fcgi -host 127.0.0.1:3033

不管上述那种情况,/home/user/public_html/mysite.fcgi 这个文件是不需要存在的。其作用指示作为一个 URL 指示 Web 服务器哪些请求需要被 FastCGI 来处理。

(2) 用 mod_rewrite 来分派需要处理的 URL 到 FastCGI.

例子:

<VirtualHost 12.34.56.78>

ServerName example.com

DocumentRoot /home/user/public_html

Alias /media /home/user/python/django/contrib/admin/media

RewriteEngine On

RewriteRule ^/(media.*)$ /$1 [QSA,L]

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^/(.*)$ /mysite.fcgi/$1 [QSA,L]

</VirtualHost>

这里指示的重写后的 url 就是上面 FastCGIExternalServer 中那个。

上述配置将不是 /media/ 开头的,或者指向不存在的文件的请求,转给了 FastCGI.

lighttpd 配置

lighttpd 天然的支持 FastCGI.

首先要确认你的 mod_fastcgi 在模块列表中,并且在 mod_rewrite 和 mod_access 后,在 mod_accesslog 前。

为了服务 admin media, 也许你还需要 mod_alias.

在 lighttpd 的配置文件中加如下一段:

server.document-root = "/home/user/public_html"

fastcgi.server = (

"/mysite.fcgi" =>(

"main" =>(

# Use host / port instead of socket for TCP fastcgi

# "host" =>"127.0.0.1",

# "port" =>3033,

"socket" =>"/home/user/mysite.sock",

"check-local" =>"disable",

)

),

)

alias.url = (

"/media/" =>"/home/user/django/contrib/admin/media/",

)

url.rewrite-once = (

"^(/media.*)$" =>"$1",

"^/favicon\.ico$" =>"/media/favicon.ico",

"^(/.*)$" =>"/mysite.fcgi$1",

)

还可以在一个 lighttp 服务器上跑多个 Django. 只要为每一个应用指定一个独立的 FastCGI host.

如何在共享的 web hosting 上运行 Django / Apache:

编辑 .htaccess:

AddHandler fastcgi-script .fcgi

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.*)$ mysite.fcgi/$1 [QSA,L]

再写一个 mysite.fcgi 的可执行的脚本:

#!/usr/bin/python

import sys, os

# Add a custom Python path.

sys.path.insert(0, "/home/user/python")

# Switch to the directory of your project. (Optional.)

# os.chdir("/home/user/myproject")

# Set the DJANGO_SETTINGS_MODULE environment variable.

os.environ['DJANGO_SETTINGS_MODULE'] = "myproject.settings"

from django.core.servers.fastcgi import runfastcgi

runfastcgi(method="threaded", daemonize="false")

在更新代码后,可以通过重新上传 mysite.fcgi 文件来指示 Apache 重启 Django 程序。

如果有 shell 权限,可以直接用 touch 命令改变时间戳:

touch mysite.fcgi


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11446182.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存