Django之部署NGINX+uWSGI
参考地址:http://www.cnblogs.com/CongZhang/p/6548529.html
http://www.cnblogs.com/alex3714/p/6538374.html
http://uwsgi.readthedocs.io/en/latest/tutorials/Django_and_nginx.html
------------------------------------------------------------------------------------------
使用NGINX+uWSGI部署django的生产环境,以提供高并发。
访问网站的流程:web请求/响应 <---> nginx服务器 <---> uWSGI服务器 <---> django
uWSGI概念:
uWSGI是一个Web服务器,它实现了WSGI协议、uwsgi、http等协议。Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换。
要注意 WSGI / uwsgi / uWSGI 这三个概念的区分。
1. WSGI是一种Web服务器网关接口。它是一个Web服务器(如nginx,uWSGI等服务器)与web应用(如用Flask框架写的程序)通信的一种规范。
2. uwsgi是一种线路协议而不是通信协议,在此常用于在uWSGI服务器与其他网络服务器的数据通信。
3. 而uWSGI是实现了uwsgi和WSGI两种协议的Web服务器。
4. uwsgi协议是一个uWSGI服务器自有的协议,它用于定义传输信息的类型(type of information),每一个uwsgi packet前4byte为传输信息类型描述,它与WSGI相比是两样东西。
uWSGI的主要特点如下:
1. 超快的性能
2. 低内存占用(实测为apache2的mod_wsgi的一半左右)
3. 多app管理(终于不用冥思苦想下个app用哪个端口比较好了)
4. 详尽的日志功能(可以用来分析app性能和瓶颈)
5. 高度可定制(内存大小限制,服务一定次数后重启等)
-------------------------------------------------------------------------------------------
部署:
总共分为两步,安装uWSGI和编写配置文件,安装NGINX和编写配置文件
我的环境Centos6.8,Django1.11,python3.6.1
1. 首先确保django项目可以使用manage.py正常运行
2. 安装uWSGI
[root@localhost ~]#pip3 install uwsgi
3. 测试uWSGI
# 创建 test.py 文件 def application(env, start_response):start_response('200 OK', [('Content-Type','text/html')])return [b"Hello World"] # python3#return ["Hello World"] # python2# 启动 uWSGI 服务[root@localhost ~]# uwsgi --http :8000 --wsgi-file test.py # 查看启动进程[root@localhost ~]# netstat -lnpt | grep uwsgitcp 0 0 127.0.0.1:26685 0.0.0.0:* LISTEN 22120/uwsgi tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 22120/uwsgi # 在浏览器中访问 http://服务器ip地址:8000 就可以看到 Hello World 字样了
4. 将启动参数写入到配置文件中,然后启动 django 项目
4.1 创建配置文件
[root@localhost ~]# cd /myproject # 进入到 django项目的主目录[root@localhost myproject]# vim myproject-uwsgi.ini # 文件名随意
4.2 写入以下内容
[uwsgi]# 对外提供 http 服务的端口http = :9000#the local unix socket file than commnuincate to Nginx 用于和 nginx 进行数据交互的端口socket = 127.0.0.1:8001# the base directory (full path) django 程序的主目录chdir = /myproject# Django's wsgi file django项目的项目目录中的wsgi文件wsgi-file = /myproject/myproject/wsgi.py# maximum number of worker processesprocesses = 4#thread numbers startched in each worker processthreads = 2#monitor uwsgi status 通过该端口可以监控 uwsgi 的负载情况stats = 127.0.0.1:9191# clear environment on exitvacuum = true# 后台运行,并输出日志daemonize = /var/log/uwsgi.log
4.3 通过 uwsgi 配置文件启动 django 程序(此时访问项目其静态文件加载不了)
[root@localhost myproject]# /usr/local/bin/uwsgi myproject_uwsgi.ini# 在浏览器中 通过访问 http://服务器ip地址:9000 可以看到django项目
5 安装NGINX(百度即可,安装完成后测试是否成功)
6 配置NGINX的配置文件
6.1 在 django 的主目录下创建下面的 nginx 配置文件,然后做软连接到 nginx 的配置文件目录
或者直接在 nginx 配置文件目录中添加该文件也可以
[root@localhost myproject]# vim /usr/local/nginx/conf/myproject-nginx.conf
http {include mime.types;default_type application/octet-stream;server_names_hash_bucket_size 3526;server_names_hash_max_size 4096;# the upstream component nginx needs to connect toupstream django {# server unix:///path/to/your/mysite/mysite.sock; # for a file socketserver 127.0.0.1:8001; # for a web port socket (we'll use this first)}# configuration of the serverserver {# the port your site will be served onlisten 8080; # the domain name it will serve forserver_name 192.168.2.180; # substitute your machine's IP address or FQDNcharset utf-8;# max upload sizeclient_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 /myproject/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 /myproject/uwsgi_params; # the uwsgi_params file you installed}
}
}
events {worker_connections 1024; ## Default: 1024
}
6.2 复制NGINX配置文件下的uwsgi_params文件到django项目的主目录下
[root@localhost myproject]# cp /usr/local/nginx/conf/uwsgi_params /myproject
6.3 启动服务:
[root@localhost myproject]# nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@localhost myproject]# nginx -c /usr/local/nginx/conf/myproject-nginx.conf[root@localhost myproject]# netstat -lnpt | grep nginxtcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 2537/nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1463/nginx
此时访问服务器IP地址:8080即可
-------------------------------------------------------------------------------------------
转载于:https://blog.51cto.com/linqi/1979778
Django之部署NGINX+uWSGI相关推荐
- Centos 6.5部署nginx+uwsgi+django
Centos 6.5部署nginx+uwsgi+django 一.安装python3,系统默认是python2.6 1.安装依赖软件 yum -y install sqlite-devel yum - ...
- Django 部署(Nginx+uwsgi)
使用 uwsgi 来部署 安装 uwsgi sudo pip install uwsgi --upgrade 使用 uwsgi 运行项目 uwsgi --http :8001 --chdir /pat ...
- 阿里云部署Django项目(nginx+uWSGI)-2018.11
前言:部署的前提是你的项目已经在本地完成可以运行了,我的本地环境是ubuntu16.04+python3.5+Django2.1.2+Mysql,在python虚拟环境(virtualenv)下建的项 ...
- Ubuntu16.04下部署 nginx+uwsgi+django1.9.7(虚拟环境pyenv+virtualenv)
由于用的新版本系统,和旧的稍有差别,在网上搜了很多相关资料,搞了三天终于搞好在Ubuntu16.04下的部署,接下来就详细写写步骤以及其中遇到的问题.前提是安装有虚拟环境pyenv+virtualen ...
- nginx+uWSGI + django部署项目
项目部署 nginx+uWSGI + django 1. WSGI WSGI是Web服务器网关接口.它是一个规范,描述了Web服务器(返回静态资源的就是web服务器,Nginx)如何与Web应用程序( ...
- Nginx + uWSGI + Python + Django部署实例
Nginx: Nginx 是一个高性能的 Web 和反向代理服务器, 它具有有很多非常优越的特性: 作为 Web 服务器:相比 Apache,Nginx 使用更少的资源,支持更多的并发连接,体现更高的 ...
- 【转】Nginx+uWSGI 部署 Django 应用
原文来自: http://www.oschina.net/question/54100_30386 http://obmem.info/?p=703 常见的django webapp 部署方式采用FC ...
- 使用Nginx+uWSGI部署Django项目
1.linux安装python3环境 参考链接:https://www.cnblogs.com/zzqit/p/10087680.html 2.安装uwsgi pip3 install uwsgiln ...
- nginx+uwsgi部署Django
本篇文章主要介绍了解决nginx+uwsgi部署Django的所有问题(小结),小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起跟随小编过来看看吧 最近,自己暑假写的小项目也算完毕了,想着投放 ...
最新文章
- DP UVALive 6506 Padovan Sequence
- 命名空间不能直接包含_php命名空间
- 039_External Data Source(转载)
- powerdns mysql_安装PowerDNS(使用MySQL后端)和Poweradmin在Debian Lenny
- B - Friends
- ide中tomcat乱码_idea tomcat 乱码问题的解决及相关设置
- C# .Net中的类型转换(3)
- 【Merry Christmas】圣诞节,给博客添加浪漫的下雪效果!
- Android设备运用Clockworkmod Recovery恢复模式安装定制的Rom
- Asp.net学习资料
- 东莞:“风暴眼”中的世界工厂
- 网易教育线终于『并轨』
- hangfire安装
- 新站长建设网站需要学习知识
- flutter 仿照智行app 首页
- 父页面调用子页面方法, 子页面加载父页面传送的数据
- 攻防世界007 伪造xff_referer
- window11安装JDK1.8【jdk-8u121】
- 湖人 PK 凯尔特人!!!
- Erlang之父Joe Armstrong确认将参加中国软件开发者大会
热门文章
- 计算机视觉--Python实现人体姿态估计
- 语音信号之特性分析图(二)
- python金融量化风险_【手把手教你】Python量化策略风险指标
- 前端ui 后台管理系统 简洁_Github上前端不可不知的可视化后台管理系统(1)
- php实现鼠标悬停显示下拉菜单,Html中鼠标悬停显示二级菜单的两种方法
- 逐渐增高_钩机加长臂的销量持续增高成为热点话题!
- carsim学习笔记3——仿真环境(驾驶员道路环境)
- python3随笔-协方差,标准差,方差
- 【深度学习】使用深度学习阅读和分类扫描文档
- 温州大学《深度学习》课程课件(九、目标检测)