zoukankan      html  css  js  c++  java
  • Django Linux+Nginx+uWSGI环境下部署

    本文将介绍如何在Linux系统上部署Django web项目,本次部署基于下面的架构:

    Linux(CentOS7)+ Python3.5 + Django1.11 + Nginx + uWSGI

    application 对象

    用 WSGI 部署的关键是一个叫做application 的可调用对象,应用服务器用它与你的代码交互,是所有请求的接口和入口。

    application 一般位于一个 Python 模块中,以名为 application 的对象的形式出现,且对服务器可见。

    我们使用startproject 命令创建项目时会自动创建 /wsgi.py文件,其中就包含了 application 对象,直接使用即可。Django 开发服务器和生产环境的 WSGI 部署都使用它。

    WSGI 服务器从其配置文件中获取 application 对象的路径。Django 的开发服务器( runserver ),从配置项 WSGI_APPLICATION 中获取。默认值是 .wsgi.application,指向 /wsgi.py 中的 application 。

    [root@kube mylab]# python
    Python 3.7.4 (default, Sep 20 2019, 15:41:39) 
    [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import sys 
    >>> print(sys.path)
    ['', '/usr/local/lib/python37.zip', '/usr/local/lib/python3.7', '/usr/local/lib/python3.7/lib-dynload', '/usr/local/lib/python3.7/site-packages']
    >>> 

    一、安装uWSGI

    Django的主要部署平台是WSGI,它也是Python的标准web服务器和应用。

    uWSGI是实现了WSGI协议的WSGI服务器。

    uWSGI 是一个快速的、自我驱动的、对开发者和系统管理员友好的应用容器服务器,完全由 C 编写。

    uWSGI的官网地址:https://uwsgi-docs.readthedocs.io/en/latest/index.html

    根据血和泪的经验教训,请确保安装的是最新版本的uwsgi,否则可能出现各种坑。

    所以不建议使用:pip3 install uwsgi(不一定是最新版)

    不建议使用:pip install https://projects.unbit.it/downloads/uwsgi-lts.tar.gz(也不一定是最新版)

    而是建议到https://uwsgi-docs.readthedocs.io/en/latest/Download.html页面,下载Stable/LTS版本的源文件。

    为什么要最新版?因为现在的官方教程和相关技术文章全是以新版编写的,很多参数名,用法有较大改变。用旧版,你可能连跑都跑不起来。

    tar -zxvf uwsgi 
    
    # 进入解压目录
    python setup.py install

    安装完毕后,尝试运行一下uwsgi:

    root@kube mylab]# uwsgi
    *** Starting uWSGI 2.0.19.1 (64bit) on [Fri Jan 15 15:14:13 2021] ***
    compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-39) on 13 January 2021 07:53:36
    os: Linux-3.10.0-1062.9.1.el7.x86_64 #1 SMP Fri Dec 6 15:49:49 UTC 2019
    nodename: kube.master
    machine: x86_64
    clock source: unix
    pcre jit disabled
    detected number of CPU cores: 4
    current working directory: /website/mylab
    detected binary path: /usr/local/bin/uwsgi
    uWSGI running as root, you can use --uid/--gid/--chroot options
    *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
    *** WARNING: you are running uWSGI without its master process manager ***
    your processes number limit is 15065
    your memory page size is 4096 bytes
    detected max file descriptor number: 1024
    lock engine: pthread robust mutexes
    thunder lock: disabled (you can enable it with --thunder-lock)
    uWSGI running as root, you can use --uid/--gid/--chroot options
    *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
    uWSGI running as root, you can use --uid/--gid/--chroot options
    *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
    The -s/--socket option is missing and stdin is not a socket.
    [root@kube mylab]# 
    uwsgi --python -version
    测试:
    
    [root@localhost /]# uwsgi --http :8888 --module myapp.wsgi    // 配置文件中的module,随便写个其他端口
    如最后出现下面内容,则运行成功:
    
    spawned uWSGI worker 1 (and the only) (pid: 5715, cores: 1)      // Ctrl+C结束

    二、配置uwsgi

    [root@kube mylab]# pwd
    /website/mylab
    [root@kube mylab]# cat uwsgi.ini   #文件位置在 项目根目录中
    [uwsgi]
    #项目根目录
    chdir = /website/mylab   
    //  指定wsgi模块下的application对象,就是调用 mylab.wsgi 文件中的 application 对象
    module =mylab.wsgi:application     
    //对本机8000端口提供服务
    socket = 127.0.0.1:20000         
    //主进程
    master = true                   
    
    
    # 以上4个是核心配置项
    
    #vhost = true          //多站模式
    #no-site = true        //多站模式时不设置入口模块和文件
    #workers = 2           //子进程数
    #reload-mercy = 10
    #vacuum = true         //退出、重启时清理文件
    #max-requests = 1000
    #limit-as = 512
    #buffer-size = 30000
    #pidfile = /var/run/uwsgi9090.pid    //pid文件,用于下脚本启动、停止该进程
    
    
    #日志文件
    daemonize = /var/log/uwsgi/mylab.log  
    #不记录正常信息,只记录错误信息
    x#disable-logging = true  
    [root@kube mylab]# 

    三、安装配置Nginx 

    安装nginx 请参考 https://www.cnblogs.com/zy09/p/10221281.html

    配置nginx  

    [root@kube uwsgi-2.0.19.1]# cat /etc/nginx/nginx.conf
    # For more information on configuration, see:
    #   * Official English Documentation: http://nginx.org/en/docs/
    #   * Official Russian Documentation: http://nginx.org/ru/docs/
    
    user nginx;
    worker_processes auto;
    error_log /var/log/nginx/error.log;
    pid /run/nginx.pid;
    
    # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
    include /usr/share/nginx/modules/*.conf;
    
    events {
        worker_connections 1024;
    }
    
    http {
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
    
        access_log  /var/log/nginx/access.log  main;
    
        sendfile            on;
        tcp_nopush          on;
        tcp_nodelay         on;
        keepalive_timeout   65;
        types_hash_max_size 2048;
    
        include             /etc/nginx/mime.types;
        default_type        application/octet-stream;
    
        # Load modular configuration files from the /etc/nginx/conf.d directory.
        # See http://nginx.org/en/docs/ngx_core_module.html#include
        # for more information.
        include /etc/nginx/conf.d/*.conf;
    
    #定义Django server
    
    server {
            #如果要使用  https 请参考nginx 添加证书的博客
            listen 8090;   #区别于 uwsgi  端口,不然会造成端口冲突
            listen [::]:8090;    #监听 ipv6 端口
    
            server_name 10.2.61.21;    #当访问此名称时 ,nginx 会进行server_name 进行匹配
    
            location / {      #匹配  / 目录
                include uwsgi_params;    #导入 uwsgi_params 文件
                uwsgi_pass 127.0.0.1:20000;   #使用 uwsgi_pass 参数将数据传递给 uwagi 应用服务器
            
            
            }
            location /static {         #匹配static 静态文件的 ,进行别名处理
            
            alias /website/mylab/myapp/static;
            
            }
    
    
    }
    
    
    
    
    
    
    
    #    server {
    #        listen       80 default_server;
    #        listen       [::]:80 default_server;
    #        server_name  _;
    #        root         /usr/share/nginx/html;
    #
            # Load configuration files for the default server block.
    #        include /etc/nginx/default.d/*.conf;
    #
    #        location / {
    #        }
    #
    #        error_page 404 /404.html;
    #            location = /40x.html {
    #        }
    #
    #        error_page 500 502 503 504 /50x.html;
    #            location = /50x.html {
    #        }
    #    }
    
    # Settings for a TLS enabled server.
    #
    #    server {
    #        listen       443 ssl http2 default_server;
    #        listen       [::]:443 ssl http2 default_server;
    #        server_name  _;
    #        root         /usr/share/nginx/html;
    #
    #        ssl_certificate "/etc/pki/nginx/server.crt";
    #        ssl_certificate_key "/etc/pki/nginx/private/server.key";
    #        ssl_session_cache shared:SSL:1m;
    #        ssl_session_timeout  10m;
    #        ssl_ciphers HIGH:!aNULL:!MD5;
    #        ssl_prefer_server_ciphers on;
    #
    #        # Load configuration files for the default server block.
    #        include /etc/nginx/default.d/*.conf;
    #
    #        location / {
    #        }
    #
    #        error_page 404 /404.html;
    #            location = /40x.html {
    #        }
    #
    #        error_page 500 502 503 504 /50x.html;
    #            location = /50x.html {
    #        }
    #    }
    
    }
    
    [root@kube uwsgi-2.0.19.1]# 
    nginx -s reload
    
    # 其他指令
    启动服务:nginx
    退出服务:nginx -s quit
    强制关闭服务:nginx -s stop
    重启服务:nginx -s reload
    验证配置文件:nginx -t
    使用配置文件:nginx -c "配置文件路径"
    使用帮助:nginx -h

    四、部署 代码并进行测试

    参考  使用 PyCharm 在centos 部署代码  https://www.cnblogs.com/zy09/p/14282349.html

    部署完成后启动 uwsgi ,nginx 

    cd 到 uwsgi.ini 放置的目录执行启动命令
    启动:uwsgi --ini uwsgi.ini
    启动 ningx
    启动服务:nginx

      一般初步部署时会有很多问题,主要在uwsgi 的log 文件汇总和 nginx log 文件中查看,

    问题总结:

    1.uwsgi.ini 配置文件中 使用 // 进行注释的内容不加载,因此不要使用 // 直接在配置文件中注释

    2.部署环境的python 模块安装不全,通过下面命令找到 部署环境 的  '/usr/local/lib/python3.7/site-packages' ,将本机的包可以直接上传。

    [root@kube mylab]# python
    Python 3.7.4 (default, Sep 20 2019, 15:41:39) 
    [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import sys 
    >>> print(sys.path)
    ['', '/usr/local/lib/python37.zip', '/usr/local/lib/python3.7', '/usr/local/lib/python3.7/lib-dynload', '/usr/local/lib/python3.7/site-packages']
    >>> 

    3. nginx 的配置文件加载的问题,请分析 nginx 日志,一般就这些。主要就是uwsgi 的log , import 模块加载之类的信息都会有。

    4.关于DEBUG 的配置参数

    #在生产环境不要使用DEBUG 功能,
    #注:当DEBUG为False时,必须设置ALLOWED_HOSTS的值。
    #当DEBUG为True和ALLOWED_HOSTS为空时,默认相当于配置['.localhost''127.0.0.1''[:: 1]']
    DEBUG = True
    #DEBUG = False
    ALLOWED_HOSTS = ['www.website.com','10.2.61.21']
  • 相关阅读:
    oracle各个版本的exp/imp兼容性
    AJAX开发 下载和安装ATF的步骤
    页面省略显示——web
    Oracle数据库中分区表的操作方法
    Hashtable的使用(2)
    RMAN之实战RMAN备份
    oracle_索引使用简介
    oracle中rollback的使用方法
    Hashtable的使用
    startup 出现的监听错误或者未初始化服务句柄【数据库归档问题】
  • 原文地址:https://www.cnblogs.com/zy09/p/14282243.html
Copyright © 2011-2022 走看看