Django个人博客搭建教程---channel的应用与阿里云部署

一、前言

pip3 install channel

二、应用

直接看官方教程,这里就不啰嗦了

三、部署

这里给出我的方案

安装supervisord

sudo apt install supervisor

在项目settings.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", "MyBlog.settings")
django.setup()
application = get_default_application()

 修改配置文件,路径如下

/etc/supervisor/supervisord.conf
; supervisor config file

[unix_http_server]
file=/var/run/supervisor.sock   ; (the path to the socket file)
chmod=0700                       ; sockef file mode (default 0700)

[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor            ; ('AUTO' child log dir, default $TEMP)

; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL  for a unix socket

; The [include] section can just contain the "files" setting.  This
; setting can list multiple files (separated by whitespace or
; newlines).  It can also contain wildcards.  The filenames are
; interpreted as relative to this file.  Included files *cannot*
; include files themselves.

[program:daphne]
directory=/home/MyBlog
command=daphne -b 127.0.0.1 -p 8001 --proxy-headers MyBlog.asgi:application #启动命令
autostart=true
autorestart=true
stdout_logfile=/tmp/websocket.log  #日志
redirect_stderr=true

[include]
files = /etc/supervisor/conf.d/*.conf

这里主要加入了

[program:daphne]
directory=/home/MyBlog # 你的项目路径,不是app路径
command=daphne -b 127.0.0.1 -p 8001 --proxy-headers MyBlog.asgi:application #启动命令
autostart=true
autorestart=true
stdout_logfile=/tmp/websocket.log  #日志
redirect_stderr=true

注意,记得检查是否存在日志文件,没有的话touch一个

查看进程命令

ps -ef | grep supervisord

启动命令 

supervisord -c /etc/supervisor/supervisord.conf

如果报错:

Unlinking stale socket /var/run/supervisor.sock

用如下命令:

find / -name supervisor.sock
/run/supervisor.sock

然后

unlink /run/supervisor.sock

再次启动

supervisord -c /etc/supervisor/supervisord.conf

 

然后修改nginx.conf文件


upstream channels-backend {
    server localhost:8001;
  }
# 在http中加入这个

# 在server中加入这个
# websockets
    location /ws/chat/ {
        proxy_pass http://channels-backend;
        proxy_http_version 1.1;
        proxy_redirect off;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
    }
http {

  upstream channels-backend {
    server localhost:8001;
  }

  server {
      listen 80;
      server_name  _;
      return 301 https://www.guanacossj.com$request_uri;
   }

	server {
    listen       443 default_server;
    listen       [::]:443 default_server;
    server_name  _;
	  ssl on;
	  ssl_certificate   /etc/nginx/cert/1831344_www.guanacossj.com.pem;     # 路径/pem文件
	  ssl_certificate_key  /etc/nginx/cert/1831344_www.guanacossj.com.key;  # 路径/key文件
	  ssl_session_timeout 5m;
	  ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
	  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
	  ssl_prefer_server_ciphers on;
    charset     utf-8;
    client_max_body_size 1000M;   # adjust to taste
    include /etc/nginx/default.d/*.conf;
    location /static {
    alias /home/MyBlog/static; # ָÏdjangoµÄtaticĿ¼
    }
    location /static/rest_framework/ {
    alias /usr/local/lib/python3.6/dist-packages/rest_framework/static/rest_framework/
    ;}

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  127.0.0.1:8000;
        #uwsgi_pass  django;
        include     uwsgi_params; 	# the uwsgi_params file you installed;
        proxy_redirect off;
	      proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
	      uwsgi_send_timeout 600;
        uwsgi_connect_timeout 600;  
        uwsgi_read_timeout 600; 
    }
    # websockets
    location /ws/chat/ {
        proxy_pass http://channels-backend;
        proxy_http_version 1.1;
        proxy_redirect off;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
    }


}
...
}

注意查看你的html文件中的websocket请求地址

官方教程给的是

var chatSocket = new WebSocket('wss://' + 'www.guanacossj.com' +'/ws/chat/' + roomName + '/');

如果你用的https,像我一样,那就是wss开头

然后后面一定要是域名不能用localhost

注意这里的

/ws/chat/

和nginx中的location那里的一定要一致。

<think>嗯,用户想从零开始搭建一个带视觉分析功能的平台,需要实时处理图片并在平台上展示结果。首先,我得理清楚整个流程。用户可能完全没有技术背景,所以得用简单易懂的语言来解释步骤,同时确保关键点都覆盖到。 首先,平台架构是基础。可能需要分成几个模块:前端、后端、存储、视觉处理。前端负责用户上传图片和展示结果,后端处理请求,存储管理图片数据,视觉模块负责分析。这时候要考虑到用户是否了解这些技术组件,可能需要解释每个部分的作用。 然后是技术选型。前端用React或Vue比较常见,后端可能需要Node.js或者Python的框架,比如Flask或Django。存储的话,云存储服务比如AWS S3或者阿里云OSS可能合适。视觉处理部分,OpenCV和TensorFlow/PyTorch是常用的工具。用户可能对如何集成这些技术不太清楚,需要详细说明。 实时分析的关键点在于处理速度。可能需要消息队列如RabbitMQ或Kafka来异步处理任务,或者用WebSocket实现实时通信。用户可能不知道这些技术的区别,得简单解释一下各自的适用场景。 数据存储方面,关系型数据库如MySQL或PostgreSQL适合结构化数据,非结构化的图片路径存在数据库,实际图片存在对象存储。用户可能分不清数据库和存储的区别,需要明确说明。 部署部分,Docker容器化方便部署,Kubernetes适合扩展。云服务的选择也很重要,AWS、Azure、阿里云这些提供商各有优势。用户可能关心成本和易用性,可能需要对比一下。 安全性和扩展性也不能忽视。HTTPS、身份验证、权限控制都是必要的。用户可能没意识到安全的重要性,得强调一下。扩展性方面,微服务架构和负载均衡能帮助平台应对增长的用户量。 最后,开发流程需要分步骤,从基础架构到各个模块的实现,再到测试优化。用户可能需要一个循序渐进的指南,确保每一步都可行。 可能用户的实际需求是快速搭建一个可用的平台,所以需要推荐最简单有效的工具和框架,避免复杂配置。同时,实时分析可能需要GPU加速,但如果是零基础,可能先从CPU处理开始,后续再优化。需要平衡技术难度和实现速度,确保用户能逐步实施。</think>我将从零基础视角分步骤讲解平台构建流程,重点说明技术架构和实现逻辑: 一、平台基础架构设计(分4层) 1. 用户交互层:Web前端 - 技术推荐:React/Vue.js + HTML5上传组件 - 关键功能:图片上传、结果可视化展示、实时状态反馈 2. 服务处理层:后端系统 $$ \begin{cases} \text{REST API服务} & \text{(Node.js/Spring Boot/Django)}\\ \text{消息队列} & \text{(RabbitMQ/Kafka)}\\ \text{任务调度} & \text{Celery/APScheduler} \end{cases} $$ 3. 视觉处理层: - 核心算法:OpenCV/PyTorch/TensorFlow - 处理流程: ```mermaid graph LR A[图片解码] --> B[预处理] B --> C[AI模型推理] C --> D[结果解析] ``` 4. 数据存储层: - 图片存储:MinIO/阿里云OSS - 结构化数据:MySQL/PostgreSQL - 分析结果:Redis/MongoDB 二、关键技术实现点 1. 实时图片传输方案: ```python # FastAPI示例代码 @app.post("/upload") async def upload(file: UploadFile): contents = await file.read() image = cv2.imdecode(np.frombuffer(contents, np.uint8), cv2.IMREAD_COLOR) # 推送到消息队列 channel.basic_publish(exchange='', routing_key='img_queue', body=image) return {"status": "processing"} ``` 2. 异步处理架构: ![处理流程图](https://via.placeholder.com/600x200?text=客户端->API网关->消息队列->AI处理器->数据库) 3. 性能优化策略: - 使用GPU加速推理(CUDA) - 图片压缩传输(WebP格式) - 分布式处理架构 三、部署实施步骤 1. 基础设施准备: - 服务器:推荐云服务(AWS EC2/阿里云ECS) - 容器化:Docker + Kubernetes - 监控:Prometheus + Grafana 2. 系统集成测试: ```bash # 压力测试示例 locust -f stress_test.py --host http://your-api-endpoint ``` 3. 安全防护措施: - HTTPS强制加密 - JWT身份验证 - 图片内容过滤(NSFW检测) 四、成本估算参考表 | 项目 | 初级方案 | 生产级方案 | |-----------|------------|-----------| | 服务器 | 2核4G $20/月 | 8核32G $200/月 | | 存储 | 100GB $5/月 | 1TB $50/月 | | AI推理服务 | CPU $0 | GPU $300/月 | 五、学习路径建议(按优先级排序) 1. 先掌握Web开发基础(HTML+JS+任意后端语言) 2. 学习基本的图像处理技术(OpenCV基础) 3. 了解云服务部署(Docker+任意云平台) 4. 进阶学习分布式系统设计 注:实际开发中建议采用现成的视觉云服务(如AWS Rekognition/阿里云视觉智能)进行快速验证,待业务成熟后再自建完整系统。初期可优先使用开源模型库(如MMDetection,HuggingFace)降低开发难度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值