设为首页 收藏本站
查看: 975|回复: 0

[经验分享] Django之部署NGINX+uWSGI-LinQi

[复制链接]

尚未签到

发表于 2018-11-16 10:41:24 | 显示全部楼层 |阅读模式
  参考地址: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 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 创建配置文件
    [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 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 程序(此时访问项目其静态文件加载不了)
    [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 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_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 -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
  
    [root@localhost myproject]# nginx -c /usr/local/nginx/conf/myproject-nginx.conf
  
    [root@localhost myproject]# 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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-635720-1-1.html 上篇帖子: CentOS 6下编译安装Nginx 下篇帖子: Centos7.0安装Nginx1.8.1-ITATPRO出身
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表