【Django】 gunicorn部署纪要

使用Gunicorn 来部署Django应用, 没有一步一步写怎么操作,简单记录下重要的点,方面以后查阅。 主要的方式还是Nginx反向代理到Gunicorn, Gunicorn wsgi来启动Django。

特点

  • Gunicorn是基于prefork模式的Python wsgi应用服务器,支持 Unix like的系统

  • 采用epoll (Linux下) 非阻塞网络I/O 模型

  • 多种Worker类型可以选择 同步的,基于事件的(gevent tornado等),基于多线程的

  • 高性能,比之uwsgi不相上下

  • 配置使用非常简单

  • 支持 Python 2.x >= 2.6 or Python 3.x >= 3.2

操作

Gunicron 安装很简单 pip install gunicorn或者用easy_install ,源码方式都可以,基本是纯Python代码,安装一般比较顺利,最好配合 virtualenv一起使用。

django最简单部署(wsgi只的是django项目中的wsgi.py文件)

gunicorn wsgi:application 
#8个worker
gunicorn -w 8 wsgi:application
#指定端口号
gunicorn -w 8 -b 0.0.0.0:8888 wsgi:application
#unix socket
gunicorn -w 8 --bind unix:/xx/mysock.sock wsgi:application
#使用gevent做异步(默认worker是同步的)
gunicorn -w 8 --bind 0.0.0.0:8000 -k 'gevent' wsgi:application 
#选项挺多,看文档或者使用 --help都可以查看
--log-level=DEBUG  
--timeout=100

参考脚本

以下只是些参考样例,并不是实际部署配置等。

  • how-to-deploy-python-wsgi-apps-using-gunicorn-http-server-behind-nginx

部署脚本 参考一

#!/bin/bash
NAME="djangotut" # Name of the application
DJANGODIR=/xxx/django_project # Django project directory
SOCKFILE=/xxx/gunicorn.sock # we will communicte using this unix socket
USER=osboxes # the user to run as
GROUP=osboxes # the group to run as
NUM_WORKERS=8 # how many worker processes should Gunicorn spawn
MAX_REQUESTS=100000 # reload the application server for each request
DJANGO_SETTINGS_MODULE=django_project.settings # which settings file should Django use
DJANGO_WSGI_MODULE=django_project.wsgi # WSGI module name
echo “Starting $NAME as `whoami`”
# Activate the virtual environment
cd $DJANGODIR
source ~/.virtualenvs/django-tutorial-env/bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH
# Create the run directory if it doesn’t exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR
# Start your Django Unicorn
# Programs meant to be run under supervisor should not daemonize themselves (do not use –daemon)
exec ~/.virtualenvs/django-tutorial-env/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
–name $NAME \
–workers $NUM_WORKERS \
–max-requests $MAX_REQUESTS \
–user=$USER –group=$GROUP \
–bind=0.0.0.0:3000 \
–log-level=error \
–log-file=-

自己用的一个简单的shell脚本,参考二

#!/bin/sh
#file: gun.sh
#start and stop gunicorn+django app
P=8000
worker=1
host="0.0.0.0"
case "$@" in
    start)
        gunicorn -b $host:$P -w $worker -k 'gevent' wsgi:application -D
        ;;
    stop)
        kill -9 `ps aux|grep gunicorn|grep $P|awk '{print $2}'|xargs`
        ;;
    restart)
        kill -9 `ps aux|grep gunicorn|grep $P|awk '{print $2}'|xargs`
        sleep 1
        gunicorn -b $host:$P -w $worker -k 'gevent' wsgi:application -D
        ;;
    reload)
        ps aux |grep gunicorn |grep $P | awk '{print $2}'|xargs kill -HUP
        ;;
    status)
    pids=$(ps aux|grep gunicorn|grep $P)
        echo "$pids"
    ;;
    *)
        echo 'unknown arguments args(start|stop|restart|status|reload)'
        exit 1
        ;;
esac

nginx 配置样例

server {
    listen 80;
    server_name 0.0.0.0;
    client_max_body_size 4G;
    location /static/ {
        alias /xxx/static/;
    }
    location /media/ {
        alias /xxx/media/;
    }
    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        # Try to serve static files from nginx, no point in making an
        # *application* server like Unicorn/Rainbows! serve static files.
        if (!-f $request_filename) {
            proxy_pass http://app_server;
            break;
        }
    }
}

优化

  • 采用 meinheld 来代替默认的那些worker,这也是一个基于事件的async worker,但是比gevent等更快。

pip install -U meinheld

gunicorn --workers=2 --worker-class="egg:meinheld#gunicorn_worker" wsgi

你可能感兴趣的:(Django,nginx)