Django+Uwsgi+Nginx如何实现生产环境部署

如何在生产上部署Django?

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

uwsgi介绍

uWSGI是一个Web服务器,它实现了WSGI协议、uwsgi、http等协议。Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换。

要注意 WSGI / uwsgi / uWSGI 这三个概念的区分。

  • WSGI是一种Web服务器网关接口。它是一个Web服务器(如nginx,uWSGI等服务器)与web应用(如用Flask框架写的程序)通信的一种规范。
  • uwsgi是一种线路协议而不是通信协议,在此常用于在uWSGI服务器与其他网络服务器的数据通信。
  • 而uWSGI是实现了uwsgi和WSGI两种协议的Web服务器。
  • uwsgi协议是一个uWSGI服务器自有的协议,它用于定义传输信息的类型(type of information),每一个uwsgi packet前4byte为传输信息类型描述,它与WSGI相比是两样东西。

uwsgi性能非常高

uWSGI的主要特点如下

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

总而言之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项目就可以实现高并发啦!

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

(0)

相关推荐

  • Django uwsgi Nginx 的生产环境部署详解

    配置生产环境 #setting.py 文件中 DEBUG = False # 生产环境 # 允许访问的域名,域名前加一个点表示允许访问该域名下的子域名,比如 www.zmrenwu.com. # test.zmrenwu.com 等二级域名同样允许访问.如果不加前面的点则只允许访问 zmrenwu.com ALLOWED_HOSTS = ["127.0.0.1",".blogzjl.site"] 创建Python虚拟环境 安装 virtualenv sudo pip

  • 详解Django+uwsgi+Nginx上线最佳实战

    什么是uwsgi? uWSGI是一个Web服务器,它实现了WSGI协议.uwsgi.http等协议.Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换.WSGI是一种Web服务器网关接口.它是一个Web服务器(如nginx,uWSGI等服务器)与web应用(如用Flask框架写的程序)通信的一种规范. WSGI是一种通信协议. uwsgi是一种线路协议而不是通信协议,在此常用于在uWSGI服务器与其他网络服务器的数据通信.uwsgi协议是一个uWSGI服务器自有的协议,

  • Centos部署django服务nginx+uwsgi的方法

    1.安装python3 yum -y install wget gcc make zlib-devel readline-devel bzip2-devel ncurses-devel sqlite-devel gdbm-devel xz-devel tk-devel openssl-devel wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tar.xz xz -d Python-3.6.1.tar.xz tar -xvf P

  • nginx+uwsgi启动Django项目的详细步骤

    当我们在用django开发的web项目时,开发测试过程中用到的是django自带的测试服务器,由于其安全及稳定等性能方面的局限性,django官方并不建议将测试服务器用在实际生产. nginx+uwsgi+django是我们常用的django部署方式.nginx作为最前端的服务器,他负责接收所有的客户端请求,对于请求的静态文件,由nginx服务器自己完成,因为它具有很好处理静态文件的能力,性能进行过优化,支持高并发量:uWSGI服务器作为支持服务器,是用来服务nginx的,nginx将请求的动态

  • Ubuntu系统搭建django+nginx+uwsgi的教程详解

    1. 在开发机上的准备工作 1.确认项目没有bug. 2.用pip freeze > requirements.txt将当前环境的包导出到requirements.txt文件中,方便在部署的时候安装. 3.将项目上传到服务器上的/srv目录下.这里以git的形式为例,打开终端,依次输入如下命令 •git init •git remote add origin xxx.git •git add . •git commit -m 'first commit' •git pull origin mas

  • 详解Django+Uwsgi+Nginx的生产环境部署

    使用runserver可以使我们的django项目很便捷的在本地运行起来,但这只能在局域网内访问,如果在生产环境部署django,就要多考虑一些问题了.比如静态文件处理,安全,效率等等,本篇文章总结归纳了一下基于uwsgi+Nginx下django项目生产环境的部署 准备条件: 1.确保有一个能够用runserver正常启动的django项目 2.项目已上传到linux 3.linux上已部署好python环境,且已安装好项目所需的模块 安装uwsgi uwsgi是python的一个模块,安装u

  • 解决nginx+uwsgi部署Django的所有问题(小结)

    最近,自己暑假写的小项目也算完毕了,想着投放到自己云服务器上,本来以为只要打开端口运行python3 manager runserver 0.0.0.0:80就搞定了,最后才知道这只适用于Django的开发模式,只支持单用户访问,既然如此,那么就得需要web服务器进行部署了.我便使用了nginx nginx? 为什么是nginx? 首先我觉得它小,很轻量级,用着简便,没有apache那么庞杂,并且网上都推荐nginx部署Django. 安装 这里直接略过,说一点Linux用户推荐大家源码安装,因

  • 详解Django+Uwsgi+Nginx 实现生产环境部署

    uwsgi介绍 uWSGI是一个Web服务器,它实现了WSGI协议.uwsgi.http等协议.Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换. 要注意 WSGI / uwsgi / uWSGI 这三个概念的区分. WSGI是一种Web服务器网关接口.它是一个Web服务器(如nginx,uWSGI等服务器)与web应用(如用Flask框架写的程序)通信的一种规范. uwsgi是一种线路协议而不是通信协议,在此常用于在uWSGI服务器与其他网络服务器的数据通信. 而u

  • Django+Uwsgi+Nginx如何实现生产环境部署

    如何在生产上部署Django? Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比较常见的一种方式. uwsgi介绍 uWSGI是一个Web服务器,它实现了WSGI协议.uwsgi.http等协议.Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换. 要注意 WSGI / uwsgi / uWSGI 这三个概念的区分. WSGI是一种Web服务器网关接口.它是一个Web服务器(如nginx,uWSGI等服务器)与web应用(如用Flask框架写的

  • 详解Ubuntu环境下部署Django+uwsgi+nginx总结

    前言 这是我在搭建Django项目时候的过程,拿来总结记录,以备不时之需. 项目采用nginx+uwsgi的搭配方式. 项目依赖包采用 requirements.txt 文件管理的方式. 本地准备工作 确认项目能够运行起来,没有 bug 将当前环境的包导出 pip freeze > requirements.txt 将项目上传到服务器上的 /srv 目录下.这里以 git 的形式为例, 打开终端, 依次输入如下命令: $ git init $ git remote add origin xxx.

  • 聊聊Django+uwsgi+nginx服务器部署问题

    目录 准备工作 安装anaconda: 安装需要的组件 创建django程序(本地) 使用文件传输工具将django程序传输到服务器 部署工作 配置uwsgi 配置nginx 配置ssl证书以提供https访问 准备工作 推荐使用anaconda进行python环境的管理,因python环境容易出现各种版本冲突问题 安装anaconda: wget https://repo.anaconda.com/archive/Anaconda3-2020.11-Linux-x86_64.sh bash A

  • 使用Docker搭建Django,Nginx,R,Python部署环境的方法

    本文介绍了使用Docker搭建Django,Nginx,R,Python部署环境的方法,分享给大家,具体如下: 基本环境: Ubuntu 16.10 docker 17.06.0-ce 压缩自己的项目文件 1.这里需要注意的是,在压缩的时候,也需要把自己的需要的Python包写在requirement.txt,这样搭建环境的时候才会知道你需要什么包,才能一起安装,我的项目的requirement.txt 内容如下. Django==1.10.5 rpy2==2.8.5 PyMySQL==0.7.

  • 基于Vue生产环境部署详解

    前面的话 开发时,Vue 会提供很多警告来帮助解决常见的错误与陷阱.生产时,这些警告语句却没有用,反而会增加载荷量.再次,有些警告检查有小的运行时开销,生产环境模式下是可以避免的.本文将详细介绍Vue生产环境部署 生产环境 如果用 Vue 完整独立版本 (直接用 <script> 元素引入 Vue),生产时应该用精简版本 (vue.min.js) 如果用 Webpack 或 Browserify 类似的打包工具时,生产状态会在 Vue 源码中由 process.env.NODE_ENV 决定,

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

    如何在生产上部署Django? Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比较常见的一种方式. uwsgi介绍 uWSGI是一个Web服务器,它实现了WSGI协议.uwsgi.http等协议.Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换. 要注意 WSGI / uwsgi / uWSGI 这三个概念的区分. 1.WSGI是一种Web服务器网关接口.它是一个Web服务器(如nginx,uWSGI等服务器)与web应用(如用Flask框架

随机推荐