美文网首页
django项目部署

django项目部署

作者: 土卡拉 | 来源:发表于2019-04-22 17:50 被阅读0次

    一、项目部署基本流程

    - 虚拟环境
    - 数据库
    - 项目上传
    - 项目依赖
    - 数据迁移
    
    - Nginx
    
    
    - uWSGI
    
    - Nginx和uWSGI对接
    

    二、虚拟环境

    # 1、安装
    pip install virtualenv
    pip install virtualenvwrapper
    
    # 2、查看
    type virtualenvwrapper.sh
    
    # 3、配置
    vi ~/.bashrc
        # 文件最后添加:
        export WORKON_HOME=~/.virtualenv
        source /usr/local/bin/virtualenvwrapper.sh
    
    # 4、创建对应目录
    mkdir ~/.virtualenv
    
    # 5、刷新环境
    source ~/.bashrc
    
    # 6、创建虚拟环境
    mkvirtualenv axf -p /usr/bin/python3
    
    # 7、检测
    

    注意: sudo apt install virtualenvwrapper

    三、数据库

    # 1、安装
    apt install mysql-server
    
    # 2、设置开机自启动
    systemctl enable mysql    
    [enable、disable、start、stop、restart、status]
    
    # 3、测试
    mysql -uroot -p
    

    注意: apt update更新源操作

    四、项目上传

    # 方式一: scp命令
    
    # 方式二: Github
    
    # 方式三: pycharm自动工具
        # 服务器创建项目目录
        mkdir /var/www/axf/ -p
        # 配置pycharm
    

    五、项目依赖

    # 生成依赖文件
    pip freeze > reqirement.txt
    
    # 安装依赖
    pip install -r reqirement.txt
    

    六、数据迁移

    # 备份
    mysqldump -uroot -p python1812axf > dir/python1812axf.sql
    
    # 恢复
    mysql> create database python1812axf charset=utf8;
    mysql> use python1812axf;
    mysql> source /var/www/axf/dir/python1812axf.sql
    

    注意: 修改Django的数据库配置

    七、启动项目

    # 启动
    python manager.py runserver 0:8000
    
    # 浏览器查看
    

    八、Nginx

    # 1、获取 key
    wget https://nginx.org/keys/nginx_signing.key
    
    
    # 2、添加key
    sudo apt-key add nginx_signing.key
    
    # 3、添加源
    echo "deb http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" \
        | sudo tee /etc/apt/sources.list.d/nginx.list
    echo "deb http://nginx.org/packages/mainline/ubuntu `lsb_release -cs` nginx" \
        | sudo tee /etc/apt/sources.list.d/nginx.list
    
    # 4、更新源
    sudo apt update
    
    # 5、安装nginx
    apt install nginx
    
    # 6、设置开机自启动
    systemctl enable nginx
    
    # 7、设置启动
    systemctl start nginx
    
    # 8、查看状态
    systemctl status nginx
    
    # 9、测试
    浏览器中访问自己的IP
    

    备注: 能访问,即表示Nginx安装成功

    九、Nginx配置1

    # 配置文件所在位置 /etc/nginx/ngingx.conf
    
    # 拷贝(备份)
    cp nginx.conf nginx.conf.temp
    
    # 编辑文件
    http{
        # include /etc/nginx/conf.d/*.conf;
        server {
            location /static {
                alias /var/www/axf/static;
            }
        }
    }
    
    # 杀死服务
    pkill -9 nginx
    
    # 检查是否杀死服务
    ps -ef | grep nginx
    
    # 指定配置文件启动
    nginx -c nginx.conf
    
    # 检查
    http://39.98.84.248/static/base/img/home.png
    

    十、uWSGI

    要注意 WSGI / uwsgi / uWSGI 这三个概念的区分。
        WSGI是一种通信协议。
        uwsgi是一种线路协议而不是通信协议,在此常用于在uWSGI服务器与其他网络服务器的数据通信。
        而uWSGI是实现了uwsgi和WSGI两种协议的Web服务器。
    uwsgi协议是一个uWSGI服务器自有的协议,它用于定义传输信息的类型(type of information),每一个uwsgi packet前4byte为传输信息类型描述,它与WSGI相比是两样东西。
    
    # 安装
    pip install uwsgi
    
    ########## 配置
    [uwsgi
    # 直接作为web服务器使用
    http=0.0.0.0:8020
    # 配置工程目录
    chdir=/var/www/axf
    # wsgi文件
    # /var/www/axf/Python181AXF/wsgi.py
    wsgi-file=Python181AXF/wsgi.py
    
    # 进程数量
    processes=1
    # 线程
    theads=2
    # 使能
    enable-theads=True
    # PID文件
    pidfile=uwsgi.pid
    # 日志文件
    daemonize=uwsgi.log
    

    备注: 必须是安装在对应的虚拟环境中!
    报错: 因为python3开发环境不完整有部分包是欠缺的 apt install python3-dev

    # 启动
    uwsgi --ini uwsgi.ini
    
    # 停止
    pkill -9 uwsgi
    
    # 检查是否杀死服务
    ps -ef | grep uwsgi
    
    # 检查是否启动Django项目成功 (没有样式的)
    http://39.98.84.248:8020/axf/
    

    十一、Nginx和uWSGI对接

    ############ uWSGI配置更改
    # 直接作为web服务器使用
    # http=0.0.0.0:8020
    # 对接nginx
    socket=0.0.0.0:8030
    
    ############ nginx.cof配置
    http {
        server {
    
        location / {
                include /etc/nginx/uwsgi_params;
                uwsgi_pass 0.0.0.0:8030;
            }
        
    
        }
    }
    

    相关文章

      网友评论

          本文标题:django项目部署

          本文链接:https://www.haomeiwen.com/subject/beyugqtx.html