• Liunx之django项目部署


    一、python web 项目部署

    python django默认启动
    python3 manage.py runserver 0.0.0.0:8000这种方式调用wsgiref单机模块,性能较低,生产环境不用

    线上使用uwsgi工具(由c语言编写的工具,性能强悍)启动django,
    使用方式:

    在激活虚拟环境的前提下,使用uwsgi
    安装配置好virtualenvwrapper工具,或者virtualenv皆可

    在虚拟环境下安装uwsgi

    1、安装uwsgi工具

    pip3 install -i https://pypi.douban.com/simple uwsgi

    2、配置uwsgi.ini

    手动创建uwsgi.ini,放在crm项目下,便于管理
    touch uwsgi.ini 

    配置文件uwsgi.ini的内容如下,这就是一个普通文本,里面定义了功能参数

    [uwsgi]
    # Django-related settings
    # the base directory (full path)
    #项目的绝对路径,定位到nbcrm的第一层
    chdir           = /opt/NBcrm
    # Django's wsgi file
    # 找到项目第二层的wsgi文件(基于上面的路径)
    module          = NBcrm.wsgi
    # the virtualenv (full path)
    # 找到虚拟环境的绝对路径
    home            = /root/Envs/nbcrm
    # process-related settings
    # master
    # 主进程
    master          = true
    # maximum number of worker processes
    # 开启uwsgi的多进程数,根据cpu核数来定义
    processes       = 16
    # the socket (use the full path to be safe
    # 基于socket链接运行crm,只有与nginx结合的时候,才使用socket形式
    socket          = 0.0.0.0:8000
    # 当你没用nginx,调试项目的时候,使用http形式 
    #http     =  0.0.0.0:8000
    # ... with appropriate permissions - may be needed
    # chmod-socket    = 664
    # clear environment on exit
    vacuum          = true
    【指定一个参数,输入日志】
    【如果你使用了supervisor,请注释掉这个参数】
    #守护进程在后台运行,且将日志信息,输出到uwsgi.log日志中
    #daemonize = uwsgi.log
    #守护进程在后台运行
    #daemonize = True

    3、启动django项目

    通过uwsgi.ini配置文件,启动crm

    # 相对路径启动
    uwsgi --ini  uwsgi.ini

    4、收集静态文件

    此时后端项目uwsgi已经启动,但是由于uwsgi默认不解析django的静态文件,需要收集django所有静态文件,然后让nginx去处理静态文件
    并且使用nginx的反向代理特性.

    请求流程:

    用户发起请求, >>>>>>>>>访问192.168.16.142(或者访问域名www.s20crm.com) ,

    请求走到nginx这一层代理,>>>>>>>>>nginx直接转发(uwsgi_pass)给后端django的地址
    django处理完毕 >>>>>>>响应给nginx >>>>>> nginx返回结果给浏览器界面

    1、修改django的settings.py静态文件

    添加如下参数:

    # Static files (CSS, JavaScript, Images)
    # https://docs.djangoproject.com/en/1.11/howto/static-files/
    #自定义的文件夹,统一管理django所有的静态文件
    STATIC_ROOT='/opt/s20static'
    
    STATIC_URL = '/static/'
    STATICFILES_DIRS = [
        os.path.join(BASE_DIR,'statics'),
    ]

    2、执行命令,收集crm的静态文件

    python3  /opt/NBcrm/manage.py collectstatic

    3、配置nginx的location路径匹配,找到crm这些静态文件

    # 在nginx.conf中找到server{}标签,添加如下参数
    # 当我的请求url是 192.168.16.142:80/static/xxxxxxxx 
    location /static {
        alias  /opt/s20static/;
    }

    5、配置nginx

    server {
        listen       80;
        server_name  localhost;
            #最低级路径匹配,当请求url是 www.s20crm.com(192.168.16.142/)
            location / {
               #include翻译就是包含的意思,将外部一个文件的内容,包含进来 
                include uwsgi_params;
                # 找的是用uwsgi启动django的主机,nginx和项目是分离的不在一个主机上
                uwsgi_pass 127.0.0.1:8000;
            }
            #当请求是 192.168.16.142/static/xxxxx
            #解析NBcrm静态文件内容,处理css js
            location /static {
                alias  /opt/s20static/;
            }
        }
        

    6、域名解析

    通过访问www.crm.com会找到真实的ip,对真实的ip进行资源请求

    设置域名:

    本地host文件:

    在本地构造虚拟网址:

    二、supervisor进程管理工具

    使用supervisor进程管理工具,管理你的项目
    其实,supervisor就是在帮你执行命令而已
    使用supervisor管理进程,这个进程不得在后台运行,

    退出虚拟环境,在物理环境下安装supervisor

    1.安装命令
        pip3 install -i https://pypi.douban.com/simple supervisor
    2.创建supervisor的配置文件
        echo_supervisord_conf > /etc/supervisor.conf
    3.编辑配置文件,写入管理nbcrm的任务参数
    [program:s20nbcrm]
    command=/root/Envs/nbcrm/bin/uwsgi --ini uwsgi.ini
    command=/root/Envs/django01/bin/uwsgi --ini /root/Envs/django01/crm_rbac/homework_crm/uwsgi.ini
    stopasgroup=true     ;默认为false,进程被杀死时,是否向这个进程组发送stop信号,包括子进程 
    killasgroup=true     ;默认为false,向进程组发送kill信号,包括子进程 
    
    4.启动supervisor,去管理uwsgi
    
    supervisord -c /etc/supervisor.conf   #指定配置文件,启动这个服务
    
    5.通过supervisorctl管理命令,管理uwsgi
    supervisorctl -c /etc/supervisor.conf 

    管理uwsgi

    # 状态
    status all 
    # 启动
    start s20nbcrm
    # 停止
    stop s20nbcrm
    # 停止所有
    stop all

    注意:

    如果想要django的admin我们需要在物理环境下创建超级用户:

    python3 manage.py createsuperuser

    在虚拟环境下创建的超级用户是隔离的,无法使用

  • 相关阅读:
    Use Gravatar in ASP.NET
    Silverlight ToolkitPivotViewer
    The Future of Silverlight December 2, 2010 at 9:00
    WPF杂记…
    Windows Phone 7开发者站点
    安装 Internet Explorer 9 Beta 的先决条件
    Internet Explorer 9 Beta(多图)
    Expression Blend4 中文
    Silverlight and WPF Virtual books
    Server2008 安装 Zune
  • 原文地址:https://www.cnblogs.com/songzhixue/p/11121541.html
Copyright © 2020-2023  润新知