Python技巧
daphne+nginx+channels+redis 在Ubuntu 24.04布署流程
如何在Ubuntu 24.04上使用uwsgi+django+Nginx来部署python3应用程序
ubuntu24设置uwsgi和nginx开机自启动(设置并开启service)
开启nginx 开机自启动服务
网站建设
在nginx 里 配置跨域访问控制
向Mysql插入Emoji表情时失败了,python代码报以下异常:InternalError: 1366
将MYSQL数据库从一台服务器移植到另一台服务器
MYSQL数据库常用操作命令
Centos7,Centos8防火墙相关的命令
Centos7+nginx下为域名免费安装ssl证书
Centos7.5+nginx布署python3+django WEB服务器
js/jQuery技巧
VPS推荐
域名申请
NameSilo域名注册的流程
构建安卓APP
Android Studio 如何修改 Package Name
Android使用WebView加载网站,无法上传图片的解决方法
Android使用WebView将H5网站打包成apk,如何去掉标题,如何更换应用图标
daphne+nginx+channels+redis 在Ubuntu 24.04布署流程
Python Django 配合 ASGI 服务器(如 Daphne)是实现高并发、异步 Web 应用(如 WebSocket)的标准方式。Django 默认生成 asgi.py 配置文件,支持异步请求处理。ASGI 原生支持 WebSocket:非常适合聊天、实时通知和游戏等实时通信应用
一,创建项目目录,或者从本地调式好的项目打包上传到目标服务器解压后自动生成目录 sudo mkdir -p /home/crash_detector cd /home/crash_detector
二,进入项目目录后创建虚拟环境 python3 -m venv venv 激活虚拟环境 source venv/bin/activate 进入虚拟环境后安装项目所需的包 deactivate (退出虚拟环境)
三, pip install channels daphne channels-redis mysqlclient sudo apt install python3-venv redis-server -y pip install websockets==13.1 aiohttp Django==4.2.5 channels>=4.0.0 channels-redis>=4.1.0 daphne>=4.0.0 aiohttp>=3.9.0 websockets==13.1 mysqlclient==2.2.7 redis>=5.0.0
四,配置 Daphne Service vi /etc/systemd/system/daphne.service 输入下面的内容 [Unit] Description=Daphne Service for Django Channels After=network.target redis-server.service mysql.service [Service] Type=simple User=www-data Group=www-data WorkingDirectory=/home/crash_detector Environment="PATH=/home/crash_detector/venv/bin" ExecStart=/home/crash_detector/venv/bin/daphne -u /run/daphne/daphne.sock crash_detector.asgi:application Restart=on-failure RestartSec=5s [Install] WantedBy=multi-user.target
五,vi /home/crash_detector/crash_detector/asgi.py import os from django.core.asgi import get_asgi_application os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'crash_detector.settings') django_asgi_app = get_asgi_application() from channels.routing import ProtocolTypeRouter, URLRouter from channels.auth import AuthMiddlewareStack from monitor import routing application = ProtocolTypeRouter({ "http": django_asgi_app, "websocket": AuthMiddlewareStack( URLRouter( routing.websocket_urlpatterns ) ), })
六,sudo chown -R www-data:www-data /var/www/crash_detector sudo mkdir -p /run/daphne sudo chown www-data:www-data /run/daphne
七,Start services sudo systemctl daemon-reload sudo systemctl start redis-server sudo systemctl enable redis-server sudo systemctl start daphne # Stop Daphne sudo systemctl stop daphne sudo systemctl enable daphne sudo systemctl restart nginx # Restart all services sudo systemctl restart daphne sudo systemctl restart nginx # 7. Check status sudo systemctl status daphne
八,手动测试是否启动项目 cd /home/crash_detector source venv/bin/activate daphne -u /run/daphne/daphne.sock crash_detector.asgi:application
删除文章
展示文章
友情链接
生活小妙招
赚钱小秘籍
开心一刻