冰恋 发表于 2018-11-16 10:41:24

Django之部署NGINX+uWSGI-LinQi

  参考地址: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
    #pip3 install uwsgi  3. 测试uWSGI
    # 创建 test.py 文件  
    def application(env, start_response):
  
      start_response('200 OK', [('Content-Type','text/html')])
  
      return # python3
  
      #return ["Hello World"] # python2
  
    # 启动 uWSGI 服务
  
    # uwsgi --http :8000 --wsgi-file test.py
  
    # 查看启动进程
  
    # netstat -lnpt | grep uwsgi
  
    tcp      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 创建配置文件
    # cd /myproject   # 进入到 django项目的主目录  
    # vim myproject-uwsgi.ini # 文件名随意
  4.2 写入以下内容
      
    # 对外提供 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 filedjango项目的项目目录中的wsgi文件
  
    wsgi-file = /myproject/myproject/wsgi.py
  
    # maximum number of worker processes
  
    processes = 4
  
    #thread numbers startched in each worker process
  
    threads = 2
  

  
    #monitor uwsgi status通过该端口可以监控 uwsgi 的负载情况
  
    stats = 127.0.0.1:9191
  
    # clear environment on exit
  
    vacuum          = true
  
    # 后台运行,并输出日志
  
    daemonize = /var/log/uwsgi.log
  4.3 通过 uwsgi 配置文件启动 django 程序(此时访问项目其静态文件加载不了)
    # /usr/local/bin/uwsgi myproject_uwsgi.ini  
    # 在浏览器中 通过访问 http://服务器ip地址:9000 可以看到django项目
  5 安装NGINX(百度即可,安装完成后测试是否成功)
  6 配置NGINX的配置文件
  6.1 在 django 的主目录下创建下面的 nginx 配置文件,然后做软连接到 nginx 的配置文件目录
  或者直接在 nginx 配置文件目录中添加该文件也可以
    # 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 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      8080;
  
      # the domain name it will serve for
  
      server_name 192.168.2.180; # 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 /myproject/static; # your Django project's static files - amend as required
  
    }
  

  
    # Finally, send all non-media requests to the Django server.
  
    location / {
  
      uwsgi_passdjango;
  
      include   /myproject/uwsgi_params; # the uwsgi_params file you installed
  
    }
  
}
  
}
  
events {
  
worker_connections1024;## Default: 1024
  
}
  6.2 复制NGINX配置文件下的uwsgi_params文件到django项目的主目录下
    # cp /usr/local/nginx/conf/uwsgi_params /myproject  6.3 启动服务:
    # nginx -t  
    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  
    # nginx -c /usr/local/nginx/conf/myproject-nginx.conf
  
    # netstat -lnpt | grep nginx
  
    tcp      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即可
  -------------------------------------------------------------------------------------------


页: [1]
查看完整版本: Django之部署NGINX+uWSGI-LinQi