django+uwsgi+daphne+supervisor生产环境部署(socket连接)

一、前言

  在上一篇文章中项目中使用了webscoket进行实时通讯,但是生产环境又使用了django+nginx+uwsgi的部署方式,我们都知道uwsgi并不能处理websocket请求,所以需要asgi服务器来处理websocket请求,官方推荐的asgi服务器是daphne,下面将介绍详细的部署步骤。

1.部署daphne

项目配置文件目录(wsgi.py同级)下创创建文件asgi.py,加入应用:

"""
ASGI entrypoint. Configures Django and then runs the application
defined in the ASGI_APPLICATION setting.
"""

import os
import django
from channels.routing import get_default_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
django.setup()
application = get_default_application()

启动daphne 测试是否正常运行(成功以后退出)

2.安装supervisor

  supervisor是由python实现的一个进程管理工具,可以确保所管理的进程一直运行,当进程一点中断supervisord会自动进行重启。

安装步骤:

#yum安装:
yum install python-setuptools
easy_install supervisor
或者
yum install -y epel-release
yum install -y supervisor  

#手动安装:
wget https://pypi.python.org/packages/source/s/supervisor/supervisor-3.1.3.tar.gz
tar zxf supervisor-3.1.3.tar.gz
cd supervisor
python setup.py install

#pip安装,推荐安装:
pip3 install supervisor
# 在/tmp/pycharm_project_298 目录
daphne -p 8001 OPs.asgi:application

生成配置文件

echo_supervisord_conf > /etc/supervisord.conf

开机自启动

vim /etc/rc.local
/usr/local/bin/supervisord

chmod +x /etc/rc.local

3.使用supervisor管理daphne进程

编辑/etc/supervisord.conf加入配置
[program:daphne]
directory=/root/ops  #项目目录
command=/usr/local/python3/bin/daphne -b 127.0.0.1 -p 8001  Ops.asgi:application #启动命令
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile = /tmp/out.log
stdout_logfile_maxbytes = 1MB
stderr_logfile= /tmp/err.log

[program:Ops]
command=/usr/local/python3/bin/uwsgi --ini  /root/Ops/Ops/uwsgi.ini  #这里是结合virtualenv的命令 和supervisor的精髓!!!!
;command= /opt/py3/bin/uwsgi --uwsgi 0.0.0.0:8010 --chdir /tmp/pycharm_project_298 --home=/opt/py3 --module Ops.wsgi
;--home指的是虚拟环境目录  --module找到 mysite/wsgi.py

;例子3 守护jumpserver的cocod进程
[program:cocoweb]
environment=PATH="/opt/py3/bin" # 指定py3的virtualenv虚拟环境
command=/web/coco/cocod restart -d
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile = /tmp/out1.log
stdout_logfile_maxbytes = 1MB
stderr_logfile= /tmp/err1.log
#uwsgi支持ini、xml等多种配置方式,本文以 ini 为例, 在/etc/目录下新建uwsgi_nginx.ini,添加如下配置:
# uwsgi.ini 这个在项目录和wsgi.py自同一级别,手动创建

[uwsgi]
# http = :9000   不使用NGINX的时候用这个访问

#the local unix socket file than commnuincate to Nginx
socket = 127.0.0.1:8010

# the base directory (full path)    #项目目录
chdir = /root/Ops

# Django's wsgi file
module = Ops.wsgi
# 或指定文件 wsgi-file = mysite/wsgi.py

# the virtualenv (full path)    # 指定虚拟环境目录
#home            = /opt/py3

# process-related settings
# master
master          = true

# maximum number of worker processes
processes       = 1
#thread numbers startched in each worker process
threads = 2

#monitor uwsgi status  # 没有用到
stats = 127.0.0.1: 9192

# ... with appropriate permissions - may be needed
chmod-socket    = 664

# clear environment on exit
vacuum          = true

# 后台运行 注意:superviscorct管理uwsgi.ini文件时候后台运行必须注释
#daemonize      = /var/log/Ops.log

# 设置nginx的timeout
uwsgi_reqad_rimeout = 600
harakiri = 1200
uwsgi.ini

supervisord -c /etc/supervisord.conf 启动或者停止daphne supervisorctl start daphne supervisorctl start ops

配置代理webscoket

修改nginx配置文件

upstream wsbackend {
         server 127.0.0.1:8001;
}

server {
    listen         8081;
    server_name    10.10.3.192;
    charset UTF-8;
    access_log      /var/log/nginx/ops_access.log;
    error_log       /var/log/nginx/ops_error.log;
    client_max_body_size 75M;
    #index index.php index.html index.htm;
    #root /alidata/video;    # 存放视频目录
    autoindex on; #开启nginx目录浏览功能
    autoindex_exact_size off; #文件大小从KB开始显示
    autoindex_localtime on; #显示文件修改时间为服务器本地时间

    location / {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:8010;     # uwsgi.ini socket 地址
        uwsgi_read_timeout 1200;
        uwsgi_send_timeout 300;

    }

    location /static {
        expires 30d;
        autoindex on;
        add_header Cache-Control private;
        alias /opt/static/;
     }

    location /ws {
        proxy_pass http://wsbackend;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Host $server_name;

     }

}

 

posted @ 2019-04-25 16:41  王竹笙  阅读(172)  评论(0编辑  收藏  举报