提高Django高并发性的部署方案(Python)
方案: nginx + uWSGI 提高 Django的并发性
1. uWSGI :
uWSGI是一个web服务器,实现了WSGI协议、uwsgi协议、http协议等。
uWSGI的主要特点是:
超快的性能
低内存占用
多app管理
详尽的日志功能(可以用来分析app的性能和瓶颈)
高度可定制(内存大小限制,服务一定次数后重启等)
uWSGI服务器自己实现了基于uwsgi协议的server部分,我们只需要在uwsgi的配置文件中指定application的地址,uWSGI就 能直接和应用框架中的WSGI application通信。
2. nginx :
Nginx 是一个高性能的负载均衡HTTP和反向代理服务器,Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件代理服务器。
特点是占有内存少,并发能力强。
结构与扩展:一个主进程和多个工作进程。工作进程是单线程的,且不需要特殊授权即可运行;
3. nginx和uWSGI的关系:
nginx相当于是服务器,负责接收请求
uwsgi是服务器和服务端应用程序的通信协议,规定了怎么把请求转发给应用程序和返回
2个基本概念:
服务器(接收请求),应用程序(处理请求并返回)
通信过程:
客户端发送一个http请求,被nginx服务器接收,nginx服务器将请求转发给uwsgi,uwsgi将请求转发给实现uwsgi
协议的应用程序(flask,gunicorn等等)
4. uWSGI的安装:
4.1: [root@crazy-acong ~]# pip3 install uwsgi
4.2: 创建django项目并配置uWSGI配置文件:
cd 进入到 django 的主目录
vi test-uwsgi.ini # 添加以下配置文件,按需修改即可:
[uwsgi]
# 对外提供 http 服务的端口
http = :8888
#用于和 nginx 进行数据交互的端口
socket = 127.0.0.1:8899
# django 程序的主目录。
chdir = /project/django
# Django's wsgi file
wsgi-file = /project/django_test/django_test/wsgi.py
# 最大的工作进程数
processes = 4
#在每个进程中的最大线程数
threads = 2
# 通过该端口可以监控 uwsgi 的负载情况
stats = 127.0.0.1:9999
# 清理环境出口
vacuum = true
# 后台运行,并输出日志
daemonize = /var/log/uwsgi.log
4.3 通过 uwsgi 配置文件启动 django 程序
uwsgi test-uwsgi.ini # 在浏览器中 通过访问 http://ip:9000 可以看到发布的 django 程序
4.4. 查看uwsgi的启动进程状态
netstat -lnpt | grep uwsgi
4.4 . 安装nginx :
apt-get install nginx
5、配置 nginx 的配置文件
在 django 的主目录下创建下面的 nginx 配置文件,然后做软连接到 nginx 的配置文件目录,或者直接在 nginx 配置文件目录中添加该文件也可以
5.1 创建 nginx 配置文件
[root@crazy-acong django_test]# cat /data/django_test/django-nginx.conf
# the upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name .example.com; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /path/to/your/mysite/media; # your Django project's media files - amend as required
}
location /static {
alias /data/django_test/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /data/django_test/uwsgi_params; # the uwsgi_params file you installed
}
}
5.2 重启nginx 服务
[root@crazy-acong django_test]# nginx -t
nginx: the configuration file /data/application/nginx-1.10.3/conf/nginx.conf syntax is ok
nginx: configuration file /data/application/nginx-1.10.3/conf/nginx.conf test is successful
[root@crazy-acong django_test]# nginx -s reload
[root@crazy-acong django_test]# netstat -lnpt | grep 8000
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 43492/nginx
这个时候就可以通过 http://ip:8000 访问 django 程序了,不过目前还存在一个问题,访问 http://ip:8000/admin 发现静态文件貌似没读取到,需要通过下面的方法解决静态文件的问题
6、解决 django 多 app 静态文件的问题
# 在 django 程序的 settings.py 文件中添加以下内容
STATIC_ROOT = os.path.join(BASE_DIR, "static_all")
# 然后通过执行该命令,将静态文件整合到一个目录中
[root@crazy-acong django_test]# python3 manage.py collectstatic
[root@crazy-acong django_test]# ll
total 40
drwxr-xr-x 3 nginx games 4096 Mar 14 14:42 app01
-rw-r--r-- 1 root root 3072 Mar 14 14:51 db.sqlite3
-rw-r--r-- 1 root root 1026 Mar 14 15:18 django-nginx.conf
drwxr-xr-x 3 nginx games 4096 Mar 14 15:45 django_test
-rwxr-xr-x 1 nginx games 809 Mar 14 14:37 manage.py
drwxr-xr-x 2 nginx games 4096 Mar 14 14:42 static
drwxr-xr-x 3 root root 4096 Mar 14 15:47 static_all # 此时会发现多了一个该目录,所有 app 的静态文件都整合到这一个目录中了
drwxr-xr-x 2 nginx games 4096 Mar 14 14:40 templates
-rw-r--r-- 1 root root 565 Mar 14 15:40 test-uwsgi.ini
-rw-r--r-- 1 root root 664 Mar 14 15:28 uwsgi_params
然后需要修改 nginx 配置文件中 指向 django 静态目录的配置文件
[root@crazy-acong django_test]# cat /data/django_test/django-nginx.conf
# the upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name .example.com; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /path/to/your/mysite/media; # your Django project's media files - amend as required
}
location /static {
# 需要修改的地方在这里
alias /data/django_test/static_all; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /data/django_test/uwsgi_params; # the uwsgi_params file you installed
}
}
最后重启 nginx 服务即可
[root@crazy-acong django_test]# nginx -t
nginx: the configuration file /data/application/nginx-1.10.3/conf/nginx.conf syntax is ok
nginx: configuration file /data/application/nginx-1.10.3/conf/nginx.conf test is successful
[root@crazy-acong django_test]# nginx -s reload
---------------------
作者:LIJZ_Python
来源:CSDN
原文:https://blog.csdn.net/yinhangxitong36/article/details/79821851
版权声明:本文为博主原创文章,转载请附上博文链接!
提高Django高并发性的部署方案(Python)的更多相关文章
- 【转】探索 ConcurrentHashMap 高并发性的实现机制
原文链接:https://www.ibm.com/developerworks/cn/java/java-lo-concurrenthashmap/ <探索 ConcurrentHashMap ...
- 探索 ConcurrentHashMap 高并发性的实现机制--转
ConcurrentHashMap 是 Java concurrent 包的重要成员.本文将结合 Java 内存模型,来分析 ConcurrentHashMap 的 JDK 源代码.通过本文,读者将了 ...
- Java高并发的常见应对方案
Java高并发的常见应对方案 一.关于并发我们说的高并发是什么? 在互联网时代,高并发,通常是指,在某个时间点,有很多个访问同时到来. 高并发,通常关心的系统指标与业务指标? QPS:每秒钟查询量,广 ...
- 高并发Flask服务部署
高并发Flask服务部署 AI模型持久化 OOP: 利用面向对象思想,实现算法在内存上的实例化及持久化.即一次模型加载,多次请求调用. class ocr_infer_class(threading. ...
- 大数据高并发系统架构实战方案(LVS负载均衡、Nginx、共享存储、海量数据、队列缓存)
课程简介: 随着互联网的发展,高并发.大数据量的网站要求越来越高.而这些高要求都是基础的技术和细节组合而成的.本课程就从实际案例出发给大家原景重现高并发架构常用技术点及详细演练. 通过该课程的学习,普 ...
- 高并发WEB网站优化方案
一.什么是高并发在互联网时代,所讲的并发.高并发,通常是指并发访问,也就是在某个时间点,有多少个访问同时到来.比如,百度首页同时有1000个人访问,那么也就是并发为1000.通常一个系统的日PV在千万 ...
- 读写分离提高 SQL Server 并发性
转自:http://www.canway.net/Lists/CanwayOriginalArticels/DispForm.aspx?ID=476 在一些大型的网站或者应用中,单台的SQL Serv ...
- Django高并发负载均衡
1 什么是负载均衡? 当一台服务器的性能达到极限时,我们可以使用服务器集群来提高网站的整体性能.那么,在服务器集群中,需要有一台服务器充当调度者的角色,用户的所有请求都会首先由它接收,调度者再根据每台 ...
- Django 高并发负载均衡
1 什么是负载均衡? 当一台服务器的性能达到极限时,我们可以使用服务器集群来提高网站的整体性能.那么,在服务器集群中,需要有一台服务器充当调度者的角色,用户的所有请求都会首先由它接收,调度者再根据每台 ...
随机推荐
- zookeeper安装(集群)
Dubbo 建议使用Zookeeper 作为服务的注册中心.Zookeeper 集群中只要有过半的节点是正常的情况下,那么整个集群对外就是可用的.正是基于这个特性,要将ZK 集群的节点数量要为奇数(2 ...
- PostMan打不开怎么解决
如题: 解决办法: 1.找到以下两个路径直接删除文件,注安装路径不同有可能不同 C:\Users\Administrator\AppData\Roaming\Postman C:\Users\Admi ...
- sqlyog创建数据库表关系图
作为一个后台前端,数据库,需求分析,运维,PPT全包的码农来说.uml建模不存在的,对不起我没有时间,就用sqlyog拉几个你看看吧.看的懂的一眼就看清了,看不懂的整再好也是白瞎. 第一步:选择增强工 ...
- .NET获取当前程序所在电脑的CPU和内存使用率
using System; using System.Diagnostics; using System.Text; using System.Runtime.InteropServices; nam ...
- HDU 1021(斐波那契数与因子3 **)
题意是说在给定的一种满足每一项等于前两项之和的数列中,判断第 n 项的数字是否为 3 的倍数. 斐波那契数在到第四十多位的时候就会超出 int 存储范围,但是题目问的是是否为 3 的倍数,也就是模 3 ...
- hdu 6385
题意是在一个矩形中任给N个点,求这N个点到矩形某边的最短距离和. 一开始想到直接贪心,求出每个点到矩形一边的最短距离,但题中说到线段间不能交叉,这里好像是比较麻烦,但题目中同时说了点与点之间的横纵坐标 ...
- freemarker迭代list、map等常规操作,将数据放到模板中
转自:https://blog.csdn.net/wickedvalley/article/details/65937189 一.controller开始准备模型.数据1.po类 package co ...
- 细说tomcat之应用监控
官网:http://tomcat.apache.org/tomcat-7.0-doc/monitoring.html Java应用程序的监控通过JMX实现,详见:https://docs.oracle ...
- POI导出Excel 错误THE MAXIMUM COLUMN WIDTH FOR AN INDIVIDUAL CELL IS 255 CHARACTERS
int orgColWidth = (arrColWidth[column.Ordinal] + 1); if (liekuan > 255) liekuan = 255; //设置列宽 she ...
- Redis基础知识 之——发布/订阅
一.说明: 订阅,取消订阅和发布实现了发布/订阅消息范式(引自wikipedia),发送者(发布者)不是计划发送消息给特定的接收者(订阅者).而是发布的消息分到不同的频道,不需要知道什么样的订阅者订阅 ...