Django + Uwsgi + Nginx 实现生产环境部署的方法


Posted in Python onJune 20, 2018

如何在生产上部署Django?

Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比较常见的一种方式。

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性能非常高

Django + Uwsgi + Nginx 实现生产环境部署的方法

uWSGI的主要特点如下
1.超快的性能
2.低内存占用(实测为apache2的mod_wsgi的一半左右)
3.多app管理(终于不用冥思苦想下个app用哪个端口比较好了-.-)
4.详尽的日志功能(可以用来分析app性能和瓶颈)
5.高度可定制(内存大小限制,服务一定次数后重启等)

总而言之uwgi是个部署用的好东东,正如uWSGI作者所吹嘘的:

If you are searching for a simple wsgi-only server, uWSGI is not for you, but if you are building a real (production-ready) app that need to be rock-solid, fast and easy to distribute/optimize for various load-average, you will pathetically and morbidly fall in love (we hope) with uWSGI.

Uwsgi 安装使用

# Install the latest stable release:

pip install uwsgi

# ... or if you want to install the latest LTS (long term support) release,

pip install https://projects.unbit.it/downloads/uwsgi-lts.tar.gz

基本测试

Create a file called test.py:

# 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 --http :8000 --wsgi-file test.py

用uwsgi 启动django

uwsgi --http :8000 --module mysite.wsgi

可以把参数写到配置文件里

alex@alex-ubuntu:~/uwsgi-test$ more crazye-uwsgi.ini 

[uwsgi]

http = :9000

#the local unix socket file than commnuincate to Nginx

socket = 127.0.0.1:8001

# the base directory (full path)

chdir = /home/alex/CrazyEye 

# Django's wsgi file

wsgi-file = CrazyEye/wsgi.py

# maximum number of worker processes

processes = 4

#thread numbers startched in each worker process

threads = 2

#monitor uwsgi status 

stats = 127.0.0.1:9191

# clear environment on exit

vacuum     = true

启动

/usr/local/bin/uwsgi crazye-uwsgi.ini

Nginx安装使用

sudo apt-get install nginx

sudo /etc/init.d/nginx start  # start nginx

为你的项目生成Nginx配置文件

You will need the uwsgi_params file, which is available in the nginx directory of the uWSGI distribution, or from https://github.com/nginx/nginx/blob/master/conf/uwsgi_params

Copy it into your project directory. In a moment we will tell nginx to refer to it.

Now create a file called mysite_nginx.conf, and put this in it:

# mysite_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 /path/to/your/mysite/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   /path/to/your/mysite/uwsgi_params; # the uwsgi_params file you installed

  }
}

This conf file tells nginx to serve up media and static files from the filesystem, as well as handle requests that require Django's intervention. For a large deployment it is considered good practice to let one server handle static/media files, and another handle Django applications, but for now, this will do just fine.

Symlink to this file from /etc/nginx/sites-enabled so nginx can see it:

sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/

Deploying static files

Before running nginx, you have to collect all Django static files in the static folder. First of all you have to edit mysite/settings.py adding:

STATIC_ROOT = os.path.join(BASE_DIR, "static/")

and then run

python manage.py collectstatic

此时启动Nginx 和Uwsgi,你的django项目就可以实现高并发啦!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python中enumerate的用法实例解析
Aug 18 Python
Python基于DES算法加密解密实例
Jun 03 Python
python 排序算法总结及实例详解
Sep 28 Python
Python实现多线程抓取网页功能实例详解
Jun 08 Python
python实现kNN算法
Dec 20 Python
python实现任意位置文件分割的实例
Dec 14 Python
python字典改变value值方法总结
Jun 21 Python
Django3.0 异步通信初体验(小结)
Dec 04 Python
python实现滑雪游戏
Feb 22 Python
python读取多层嵌套文件夹中的文件实例
Feb 27 Python
详解基于Jupyter notebooks采用sklearn库实现多元回归方程编程
Mar 25 Python
使用Keras中的ImageDataGenerator进行批次读图方式
Jun 17 Python
python实现电脑自动关机
Jun 20 #Python
python3将视频流保存为本地视频文件
Jun 20 #Python
Python操作MySQL数据库的方法
Jun 20 #Python
python实现抖音视频批量下载
Jun 20 #Python
Python 类的特殊成员解析
Jun 20 #Python
python实现音乐下载的统计
Jun 20 #Python
django2用iframe标签完成网页内嵌播放b站视频功能
Jun 20 #Python
You might like
php 无限极分类
2008/03/27 PHP
Zend Studio (eclipse)使用速度优化方法
2011/03/23 PHP
使用php 获取时间今天明天昨天时间戳的详解
2013/06/20 PHP
php+js实现图片的上传、裁剪、预览、提交示例
2013/08/27 PHP
php使用filter过滤器验证邮箱 ipv6地址 url验证
2013/12/25 PHP
phpnow php探针环境检测代码
2014/11/04 PHP
php实现指定字符串中查找子字符串的方法
2015/03/17 PHP
PHP的JSON封装、转变及输出操作示例
2019/09/27 PHP
使用CSS3实现字体颜色渐变的实现
2021/03/09 HTML / CSS
保证JavaScript和Asp、Php等后端程序间传值编码统一
2009/04/17 Javascript
CSS和Javascript简单复习资料
2010/06/29 Javascript
详解Node.js 命令行程序开发教程
2017/06/07 Javascript
Vue.js实现实例搜索应用功能详细代码
2017/08/24 Javascript
Weex开发之地图篇的具体使用
2019/10/16 Javascript
原生JavaScript实现刮刮乐
2020/09/29 Javascript
python计数排序和基数排序算法实例
2014/04/25 Python
在Python中使用matplotlib模块绘制数据图的示例
2015/05/04 Python
Python实现破解猜数游戏算法示例
2017/09/25 Python
Python实现一个Git日志统计分析的小工具
2017/12/14 Python
Python把csv数据写入list和字典类型的变量脚本方法
2018/06/15 Python
浅析Python 3 字符串中的 STR 和 Bytes 有什么区别
2018/10/14 Python
Django使用redis缓存服务器的实现代码示例
2019/04/28 Python
树莓派升级python的具体步骤
2020/07/05 Python
一些Unix笔试题和面试题
2013/01/22 面试题
小学生自我鉴定
2013/10/12 职场文书
微信营销策划方案
2014/02/24 职场文书
经典促销广告词大全
2014/03/19 职场文书
研究生毕业自我鉴定范文
2014/03/27 职场文书
医学求职信
2014/05/28 职场文书
安全标语大全
2014/06/10 职场文书
社区清明节活动总结
2014/07/04 职场文书
干部作风建设心得体会
2014/10/22 职场文书
2015年人事科工作总结
2015/04/28 职场文书
离婚答辩状范文
2015/05/22 职场文书
导游词之无锡唐城
2019/12/12 职场文书
MySQL 那些常见的错误设计规范,你都知道吗
2021/07/16 MySQL