Django+nginx+fastcgi 配置

本文档详细介绍了如何配置Django项目与nginx、fastcgi的整合,包括nginx的安装、flup模块的安装、Django工程创建、nginx配置以及运行步骤。通过配置nginx的upstream实现负载均衡,利用fastcgi处理Django应用的请求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 安装nginx
1.软件准备
nginx:这里选择stable版本
http://nginx.org/en/download.html

wget http://nginx.org/download/nginx-1.0.6.tar.gz

pcre:rewrite所要使用的第三方模块
http://www.pcre.org/

wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.13.zip
openssl:https协议所要使用的模块
http://www.openssl.org/source/
wget http://www.openssl.org/source/openssl-1.0.0e.tar.gz
uploadprogress:由于使用到nginx来做文件上传进度条,所以需要此模块
http://wiki.nginx.org/HttpUploadProgressModule
https://github.com/masterzen/nginx-upload-progress-module/downloads
upload:上传模块
https://github.com/vkholodkov/nginx-upload-module/downloads

2. 将下载的文件解压,为便于管理,都放到nginx目录下,目录列表
nginx-1.0.6.tar.gz
pcre-8.13.zip
openssl-1.0.0e.tar.gz
vkholodkov-nginx-upload-module-2.2.0-0-g2ec4e4f.tar.gz
masterzen-nginx-upload-progress-module-v0.8.3-0-gc7c663f.tar.gz

3. 配置、编译、安装nginx
1>切换到root用户
sudo -s -H
2>配置
./configure --prefix=/opt/nginx-1.0.6 --with-openssl=../openssl-1.0.0e --with-pcre=../pcre-8.13 --add-module=../masterzen-nginx-upload-progress-module-c7c663f --add-module=../vkholodkov-nginx-upload-module-2ec4e4f
部分配置显示如下:
configuring additional modules
adding module in ../masterzen-nginx-upload-progress-module-c7c663f
+ ngx_http_uploadprogress_module was configured
adding module in ../vkholodkov-nginx-upload-module-2ec4e4f
+ ngx_http_upload_module was configured
checking for system md library ... not found
checking for system md5 library ... not found
checking for OpenSSL md5 crypto library ... found
checking for sha1 in system md library ... not found
checking for OpenSSL sha1 crypto library ... found
checking for zlib library ... found
creating objs/Makefile
Configuration summary
+ using PCRE library: ../pcre-8.13
+ using OpenSSL library: ../openssl-1.0.0e
+ md5: using system crypto library
+ sha1: using system crypto library
+ using system zlib library
nginx path prefix: "/opt/nginx-1.0.6"
nginx binary file: "/opt/nginx-1.0.6/sbin/nginx"
nginx configuration prefix: "/opt/nginx-1.0.6/conf"
nginx configuration file: "/opt/nginx-1.0.6/conf/nginx.conf"
nginx pid file: "/opt/nginx-1.0.6/logs/nginx.pid"
nginx error log file: "/opt/nginx-1.0.6/logs/error.log"
nginx http access log file: "/opt/nginx-1.0.6/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
其中:zlib md5 sha1包都使用系统自带的,若系统没有,可下载安装
aptitude search sha1
aptitude search md5
找到合适的包,进行安装即可
3> make
4> make install
5> 到nginx安装目录,查看nginx配置
/opt/nginx-1.0.6/sbin/nginx -V
P.S.
--with-pcre=DIR - 指定 PCRE 库的源代码的路径。
--with-openssl=DIR - Set path to OpenSSL library sources
--with-openssl-opt=OPTIONS - Set additional options for OpenSSL building
--add-module=PATH - Add in a third-party module found in directory PATH
可参考 ./configuare --help查看配置参数
6> 查看nginx安装成功
/opt/nginx-1.0.6/sbin/nginx -t
nginx: the configuration file /opt/nginx-1.0.6/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx-1.0.6/conf/nginx.conf test is successful
由此安装成功!
7> 启动nginx
/opt/nginx-1.0.6/sbin/nginx -c /opt/nginx-1.0.6/conf/nginx.conf


2. 安装flup
下载flup模块,找到setup.py脚本,python setup.py install或python setup.py即可安装。当然要先要安装python,并配置好python环境变量。
或者直接用easy_install安装:
/usr/bin/easy_install flup


3.创建工程
在/wibsite下创建一个django工程mysite,
如:django-admin.py startproject mysite
在mysite里添加一个py文件first.py
from django.http import HttpResponse
def index(request):
return HttpResponse('first django page .....')
修改url.py,在patterns里添加:
(r'^mysite/$','mysite.first.index')


4. 配置nginx
在nginx安装目录下,一般在/usr/local/nginx/conf下,修改nginx.conf文件,关键几点如下:
worker_processes 2;#开2个nginx进程, ps aux可以看到这2个子进程和一个nginx主进程,一般按CPU数配置,我的习惯是2*CPU(核)数
events {
worker_connections 64;#每个进程链接队列长度,可按实际设置
}
http {
... #省略其它配置
#和后端的2个fastcgi 服务通讯,注意fastcgi ,后面用到,weight是权重
upstream fastcgi {
server 10.0.40.83:8051 weight=1;
server 10.0.40.83:8052 weight=1;
}
server {
listen 80;
server_name localhost;
...#省略其它配置
location / {
# 指定 fastcgi 的主机和端口,这里的fastcgi指的是上面的upstream
fastcgi_pass fastcgi;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_pass_header Authorization;
fastcgi_intercept_errors off;
}
...#省略其它配置
}
...#省略其它内容
}

4. 运行
运行nginx(root用户)
运行2个fastcgi服务,开2个cmd窗口,在mysite目录下:
python manage.py runfcgi host=10.0.40.83 port=8051 method=threaded daemonize=false
python manage.py runfcgi host=10.0.40.83 port=8052 method=threaded daemonize=false
http://10.0.40.83/http://127.0.0.1/ 或http://localhost/可以看到输出 结果。


5. 破坏尝试
开2个fastcgi的窗口,终止其中一个访问看看,其实没有任何影响。
再把运行的停止,停止的启动看看,还是能访问。
若2个都终止,发现不能访问。

 

 

====================================================================================

使用uwsgi发布django项目

 

以qos项目为例,应用根目录/home/tools/releases/qos

  1. 项目根目录创建django.xml

    django.xml
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    < uwsgi >
        < socket >127.0.0.1:9001</ socket >
        < workers >1</ workers >
        < max-requests >10000</ max-requests >
        < chdir >/home/tools/releases/qos</ chdir >
        < pythonpath >.</ pythonpath >
        < module >wsgi</ module >
    </ uwsgi >
  2. 创建wsgi.py

    wsgi.py
    ?
    import os, sys
    if not os.path.dirname(__file__) in sys.path[: 1 ]:
        sys.path.insert( 0 , os.path.dirname(__file__))
        os.environ[ 'DJANGO_SETTINGS_MODULE' ] = 'settings'
        from django.core.handlers.wsgi import WSGIHandler
        application = WSGIHandler()
  3. uwsgi 启动脚本

    start_project.sh
    ?
    #!/bin/bash
    uwsgi -M -x /home/tools/releases/qos/django .xml --limit-as 512 -d /tmp/qos .log
  4. nginx.conf 中配置目录映射

    uwsgi configuration in nginx conf
    ?
    location /qos {
                uwsgi_pass 127.0 . 0.1 : 9001 ;
                include uwsgi_params;
                include acl.conf;
            }
  5. 重启nginx进程,发布新的django引用;如果以后代码有更新,直接找到uwsgi主进程,kill -HUP 后,应用直接更新

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值