Django:Django+channels+uwsgi+nginx+daphne部署

感谢:(1)python3, supervisor配置:https://www.jianshu.com/p/ba6327f198ce

一、前提

项目使用了websocket来进行消息推送,而uwsgi并不能处理websocket请求,所以需要asgi服务器来处理websocket请求,官方推荐的asgi服务器是daphne。

二、websocket编写

先前有写,可参考:https://blog.csdn.net/weixin_38676276/article/details/114277902

三、生产环境部署

1、先部署Django+uwsgi+nginx

上一篇有写,可直接参考:https://blog.csdn.net/weixin_38676276/article/details/114284642

2、部署daphne

安装channels是会自动安装daphne

如已经编写好websocket,可执行: daphne -p 8001 项目名称.asgi:application (-p为端口号)测试是否正常运行

如图运行成功:

3、修改nginx配置文件

  # 8081端口号需要和daphne -b 127.0.0.1 -p 8081 项目名称.asgi:application一致
            proxy_pass http://127.0.0.1:8081;


user  root;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
	
	server {
        # 设置网站运行端口
        listen       81;
        server_name  localhost;
   
        location / {
            include  uwsgi_params;
            uwsgi_pass  127.0.0.1:32592;
            index  index.html index.htm;
            client_max_body_size 35m;
			proxy_set_header REMOTE_ADDR $remote_addr;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
	# 静态文件目录
        location /static/ {
            alias /root/logs/static/;
            index index.html index.htm;
        }
		#  /ws/xx 连接请求均走这边
		location /ws {
            # 8081端口号需要和daphne -b 127.0.0.1 -p 8081 项目名称.asgi:application一致
            # 必须使用127.0.0.1或ip地址,不要使用localhost
			proxy_pass http://127.0.0.1:8081;
			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;
		}
    }



    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

4、运行

检测nginx配置文件有没有错误:nginx -t

重启nginx:nginx -s reload

进入项目目录,启动uwsgi:uwsgi --ini uwsgi.ini(需先kill uwsgi进程,或者直接重启uwsgi:uwsgi --reload uwsgi.pid) 

启动 daphne服务器:daphne -b 127.0.0.1 -p 8081 项目名称.asgi:application

至此,ws通讯成功。

 

四、 使用supervisor管理daphne

感谢:https://www.jianshu.com/p/d6394ca19d92

           https://www.cnblogs.com/wdliu/p/10032180.html

将服务端的终端关闭,再访问时发现websocket出现无法访问的现象。这是因为daphne不是常驻线程的,这时候我们得需要常驻线程,我们可以借助supervisor来管理他。

1、安装supervisor

pip3 install supervisor

2、建立软连接

ln -s /usr/local/python3/bin/supervisord  /usr/bin/supervisord

3、生成配置文件

# 这里我是在项目目录执行,生成在了项目目录下,可以自行换成etc目录
echo_supervisord_conf > supervisord.conf


 4、对配置文件增加配置

感谢:https://blog.csdn.net/qq_41854273/article/details/89333269

[program:daphne]
user=root
directory=/home/arrow/bmxf  ;项目目录
command=daphne -b 127.0.0.1 -p 8081 --proxy-headers 项目名称.asgi:application  ;启动命令
autostart=true
autorestart=true
stdout_logfile=/tmp/websocket.log   ;日志所在目录
logfile_maxbytes=500MB        ;日志文件大小,超出会rotate,默认 50MB,如果设成0,表示不限制大小
logfile_backups=10           ;日志文件保留备份数量默认10,设为0表示不备份 
redirect_stderr=true

5、启用

# 启动supervisor
supervisord -c supervisord.conf


#启动或者停止daphne
#supervisorctl start daphne
#supervisorctl stop daphne			# 停止daphne

PS:如果提示daphne:command not found 则可能需要进入虚拟环境启动。(daphne安装在了虚拟环境)

测试ws通讯成功。 

关闭终端,程序正常访问。

 

五、制作脚本启动

# nginx -s reload
NAME="uwsgi"
echo "killing all by uwsgi"
echo $NAME
ID=`ps -ef | grep "$NAME" | grep -v "$0" | grep -v "grep" | awk '{print $2}'`
echo $ID
for id in $ID
do
kill -9 $id
echo "killed $id"
done
echo "启动UWSGI"
#uwsgi -i uwsgi.ini
uwsgi --ini uwsgi.ini
#uwsgi --reload uwsgi.pid
#workon pay_python_ven
# 启动dephne 开启websocket连接
echo "启动dephne 开启websocket连接"
# ps -ef | grep daphne
#daphne -p 8081 -b 127.0.0.1 pay_python.asgi:application
# 这里的端口号,需要和daphne启动的端口号相同
NAME="8081"  
STR="8081端口使用情况"
echo "killing all by 8081"
echo $STR
ID=`ps -ef | grep "$NAME" | grep -v "$0" | grep -v "grep" | awk '{print $2}'`
echo $ID
for id in $ID
do
kill -9 $id
echo "killed $id"
done

NAME="supervisord"
echo "killing all by supervisord"
echo $NAME
ID=`ps -ef | grep "$NAME" | grep -v "$0" | grep -v "grep" | awk '{print $2}'`
echo $ID
for id in $ID
do
kill -9 $id
echo "killed $id"
done
unlink /tmp/supervisor.sock
supervisord -c /root/pay_python/supervisord.conf
#supervisorctl start daphne
#supervisorctl stop daphne			# 停止daphne
sleep 1

 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值