• Nginx+uwsgi部署 Diango(生产环境)


    环境:CentOS6.5 + Nginx1.11.5 + Python3.5.2

    1. 安装基础软件包

    yum install -y zlib-devel bzip2-devel 
    pcre-devel openssl-devel ncurses-devel sqlite-devel 
    readline-devel tk-devel
    

     2. 安装Python3.5.2版本

     源码包下载,戳我

    wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tar.xz
    tar xf Python-3.5.2.tar.xz 
    cd Python-3.5.2
    ./configure
    make -j 2
    make altinstall

     PS:Python3.x默认已经安装pip等包管理工具,如果没有需要手动安装`pip`包管理工具

     3. uWSGI的安装与使用

    什么是uwsgi,what?

    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

    # 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

    基本测试

    创建测试文件

    # 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
    

     使用浏览器访问`http://ip:8000`验证

    使用uWSGI运行Django

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

     使用浏览器访问`http://ip:8000`验证

    可以将参数写到一个配置文件中

    [root@localhost Django_test]# vim mysite_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 = /usr/local/Django_test
    # Django's wsgi file
    wsgi-file = Django_test/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 or restart
    vacuum          = true
    

     启动

    uwsgi --ini mysite_uwsgi.ini
    

      使用浏览器访问`http://ip:9000`验证

    3. 安装并配置Nginx

    useradd -M -s /sbin/nologin nginx
    tar zxf nginx-1.11.5.tar.gz 
    cd nginx-1.11.5
    ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_gzip_static_module
    make && make install
    ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
    

     修改nginx配置文件

    #修改主配置文件,引入下面要写的uwsgi的配置文件,也可以在当前配置文件中写
    # vim /usr/local/nginx/conf/nginx.conf
    #在http区段中
    include mysite_uwsgi_nginx.conf;
    

     创建一个uwsgi的配置文件

    # vim /usr/local/nginx/conf/mysite_uwsgi_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
    
        location /static {
            alias /usr/local/Django_test/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     uwsgi_params; # the uwsgi_params file you installed
        }
    }
    

    到此Django项目部署已经基本部署完成,你可以访问我们写的页面,但是访问`http://ip:8000/admin`却发现没有样式,waht?

    这个问题是由于admin的样式文件都在django内部,而不是在我们的项目的静态文件的目录中,到此我们需要把所有的静态文件汇聚到一个目录中

    1. 修改项目目录中的`setting.py`文件,添加 (all_statics可以自己指定目录)

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

     2.run (提示输入yes)

    python manage.py collectstatic
    

     3.现在我们需要去修改nginx的配置文件,将静态文件的目录修改为汇聚后的静态文件的目录

    location /static {
            alias /usr/local/Django_test/all_statics;
        }
    

     现在去启动nginx和uwsgi

    [root@localhost Django_test]# # nginx 
    [root@localhost Django_test]# uwsgi mysite_uwsgi.ini &
    

     现在admin页面总算可以看了

  • 相关阅读:
    Web.config Transformation Syntax for Web Application Project Deployment
    xcode 8.1 (8B62)真机调试配置
    主机与虚拟机互ping
    mac系统下安装mysql步骤
    iphone设备尺寸规格
    linux环境下oracle静默安装
    实现静默安装APK的方法
    mac系统下如何删除银行安全插件
    mac系统下设置eclipse的补全快捷键方法
    给Xcode增加复制行、删除行快捷键的方法
  • 原文地址:https://www.cnblogs.com/zhichaoma/p/8056336.html
Copyright © 2020-2023  润新知