微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

django+uwsgi+daphne+supervisor生产环境部署

一、前言

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

二、软件安装

  之前已经写过一一篇关于django+Nginx+uwsgi的部署方式地址:,这里就不多说了,以下介绍daphne、supervisor、以及Nginx代理websocket的安装配置。

1.部署daphne

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

sgi entrypoint. Configures Django and then runs the application defined in the Asgi_APPLICATION setting.

<span style="color: #0000ff;">import<span style="color: #000000;"> os
<span style="color: #0000ff;">import<span style="color: #000000;"> django
<span style="color: #0000ff;">from channels.routing <span style="color: #0000ff;">import<span style="color: #000000;"> get_default_application

os.environ.setdefault(<span style="color: #800000;">"<span style="color: #800000;">DJANGO_SETTINGS_MODULE<span style="color: #800000;">",<span style="color: #800000;">"<span style="color: #800000;">myproject.settings<span style="color: #800000;">"<span style="color: #000000;">)
django.setup()
application = get_default_application()

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

daphne -p 8001 devops.asgi:application

2.安装supervisor

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

安装步骤:

yum install python-easy_install supervisor 或者 yum install -y epel--<span style="color: #008000;">#<span style="color: #008000;">手动安装:
wget https://pypi.python.org/packages/source/s/supervisor/supervisor-3.1.3<span style="color: #000000;">.tar.gz
tar zxf supervisor
-3.1.3<span style="color: #000000;">.tar.gz
cd supervisor
python setup.py install

<span style="color: #008000;">#<span style="color: #008000;">pip安装:
pip install supervisor

生成配置文件

echo_supervisord_conf > /etc/supervisord.conf

3.使用supervisor管理daphne进程

编辑/etc/supervisord.conf加入配置

daphne] directory=/opt/app/devops command=daphne -b 127.0.0.1 -p 8001 --proxy-headers devops.asgi:application autostart===/tmp/websocket.log redirect_stderr=true

启动supervisor

supervisord -c /etc/supervisord.conf

启动或者停止daphne

daphne supervisorctl stop daphne

三、代理webscoket

修改Nginx配置文件

127.0.0.1:8001<span style="color: #008000;">#<span style="color: #008000;">#####location配置
<span style="color: #000000;">
location
/ws/<span style="color: #000000;">deploy {
proxy_pass http://<span style="color: #000000;">wsbackend;
proxy_http_version 1.1<span style="color: #000000;">;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection <span style="color: #800000;">"<span style="color: #800000;">upgrade<span style="color: #800000;">"<span style="color: #000000;">;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-<span style="color: #000000;">IP $remote_addr;
proxy_set_header X-Forwarded-<span style="color: #000000;">For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-<span style="color: #000000;">Host $server_name;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐