Django项目部署流程——最基本的部署流程

第一阶段:基本准备

.gitignore文件

在项目中有一些文件时不需要上传到仓库的。(例如一些在本机测试的文件,local_settings文件等)

参考:https://github.com/github/gitignore/blob/main/Python.gitignore

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
#  Usually these files are written by a python script from a template
#  before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
#   For a library or package, you might want to ignore these files since the code is
#   intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
#   According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
#   However, in case of collaboration, if having platform-specific dependencies or dependencies
#   having no cross-platform support, pipenv may install dependencies that don't work, or not
#   install all needed dependencies.
#Pipfile.lock

# poetry
#   Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
#   This is especially recommended for binary packages to ensure reproducibility, and is more
#   commonly ignored for libraries.
#   https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
#   Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
#   pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
#   in version control.
#   https://pdm.fming.dev/latest/usage/project/#working-with-version-control
.pdm.toml
.pdm-python
.pdm-build/

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
#  JetBrains specific template is maintained in a separate JetBrains.gitignore that can
#  be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
#  and can be added to the global gitignore or merged into this file.  For a more nuclear
#  option (not recommended) you can uncomment the following to ignore the entire idea folder.
.idea/

# database migrations
# 除了初始化的这个都不提交
*/migrations/*.py
!*/migrations/__init__.py

将项目上传到gitee

  • 通过git init创建本地仓库

在这里插入图片描述

  • 创建远程仓库

  • 配置本地仓库的远程仓库

git remote add origin https://gitee.com/duan-peitong/django_order_system.git
  • 将代码提交到本地仓库
git add .
git commit -m '提交说明...'
  • 上传到远程仓库
git push origin master

在这里插入图片描述

服务器准备

  • 购买服务器

    在这里插入图片描述
    在这里插入图片描述

  • finalshell远程连接阿里云服务器

    在这里插入图片描述

服务器拉取gitee代码

  • 服务器下载git

    yum install git -y
    

    在这里插入图片描述

  • 将代码拉到服务器

    git clone https://gitee.com/duan-peitong/django_order_system.git
    

    在这里插入图片描述


第二阶段:服务器配置

mysql环境

  • 安装数据库

    yum install mariadb-server -y     (数据库服务端)
    yum install mariadb -y            (数据库客户端)
    

    在这里插入图片描述

  • 启动mariadb服务

    systemctl start mariadb  (启动数据库服务)
    systemctl enable mariadb  (开机自启数据库服务)
    systemctl restart mariadb  (重启数据库服务)
    systemctl stop mariadb  (停用数据库服务)
    
  • 登录数据库

    mysql -u root -p    (root无密码登录即可)
    

    在这里插入图片描述

  • 修改root用户的登录密码

    ALTER USER 'root'@'localhost' IDENTIFIED BY '';
    
  • 创建新用户

    '127.0.0.1'表示了用户可以从本机连接。

    如果希望用户能从任何主机连接,可以使用'%',但是一般数据库用户是不允许在任何主机连接的。

    通过%创建的用户,并不意味着可以在任何电脑通过mysql -u dpt -h '39.102.208.92' -p密码的方式连接,因为这只代码数据库层面允许,而服务器的3306端口是否对外开放也不一定,只有服务器该端口开放时才能连接。

    端口的开放可以通过安全组设置,22(ssh),80(http),443(https),3306(数据库)

    在这里插入图片描述

    CREATE USER 'dpt'@'127.0.0.1' IDENTIFIED BY '047981pt.';   (仅本机) 
    CREATE USER 'DPT'@'%' IDENTIFIED BY '047981pt.';           (所有)
    
    CREATE USER '用户名'@'连接地址' IDENTIFIED BY '密码';
    

    在这里插入图片描述

  • 创建数据库

    CREATE DATABASE django_order_system DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
    

    在这里插入图片描述

  • 授权用户数据库

    GRANT ALL PRIVILEGES ON django_order_system.* TO 'dpt'@'127.0.0.1';  (仅本机)
    flush privileges;
    
    GRANT ALL PRIVILEGES ON django_order_system.* TO 'DPT'@'%';  (所有)
    flush privileges;
    

    在这里插入图片描述

  • 在做完这些配置之后,之后要在服务器中运行项目,要连接的数据库信息就是:dpt047981pt.django_order_system

redis环境

  • 下载redis

    yum install redis -y
    
  • 配置redis

    vim /etc/redis.conf
    

    requirepass

    在这里插入图片描述

    bind

    在这里插入图片描述

  • 启动redis

    systemctl start mariadb 
    systemctl enable mariadb
    

python3环境

  • 安装gcc,用于后续安装Python时编译源码。

    yum install gcc -y
    
  • 安装Python3相关依赖

    yum install zlib zlib-devel -y
    yum install bzip2 bzip2-devel  -y
    yum install ncurses ncurses-devel  -y
    yum install readline readline-devel  -y
    yum install openssl openssl-devel  -y
    yum install xz lzma xz-devel  -y
    yum install sqlite sqlite-devel  -y
    yum install gdbm gdbm-devel  -y
    yum install tk tk-devel  -y
    yum install mysql-devel -y
    yum install python-devel -y
    yum install libffi-devel -y
    
  • 下载Python源码,https://www.python.org/ftp/python/

    cd /data/
    wget https://www.python.org/ftp/python/3.9.5/Python-3.9.5.tgz
    

    注意:如果没有wget,则先安装 yum install wget

    在这里插入图片描述

  • 编译安装

    • 解压

      tar -xvf Python-3.9.5.tgz
      
    • 进入目录并编译安装

      cd Python-3.9.5
      ./configure
      make all
      make install
      
    • 测试

      python3 --version
      
      默认会配置到/usr/local/bin/中
      /usr/local/bin/python3
      /usr/local/bin/pip3
      /usr/local/bin/pip3.9
      
    • 配置豆瓣源(腾讯云服务器,默认腾讯源)

      pip3.9 config set global.index-url https://mirrors.aliyun.com/pypi/simple/
      https://mirrors.aliyun.com/pypi/simple/
      

虚拟环境

  • 创建虚拟环境

    pip3.9 install virtualenv
    
  • 创建虚拟环境目录并创建虚拟环境

    mkdir /envs
    virtualenv /envs/nb --python=python3.9
    

    在这里插入图片描述

  • 安装项目依赖的pip包

    source /envs/nb/bin/activate          (激活虚拟环境)
    cd /data/www/nb/
    pip install -r requirements.txt       (安装项目依赖)
    

    在这里插入图片描述

在服务器配置local_settings文件

local_settings文件用来配置本地测试和服务器部署时不同的设置

import os
from pathlib import Path

BASE_DIR = Path(__file__).resolve().parent.parent.parent

DEBUG = False

ALLOWED_HOSTS = ['*']

STATIC_ROOT = os.path.join(BASE_DIR,"allstatic")

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
		'NAME': 'django_order_system',
        'USER': 'DPT',
        'PASSWORD': '047981pt.',
        'HOST': '127.0.0.1',
        'PORT': 3306,
    }
}

CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
            "CONNECTION_POOL_KWARGS": {"max_connections": 100},
            "PASSWORD": "047981pt.",
        }
    }
}

收集静态资源

django处理静态资源性能差,就需要通过nginx处理,就要将所有的静态资源收集起来

根据上面配置的STATIC_ROOT将静态文件收集起来

后续静态资源有变动时,需要重新收集

python manage.py  collectstatic	

在这里插入图片描述

第三阶段:运行项目

Nginx: HTTP服务器,反向代理,静态资源转发,负载均衡,SSL终端,缓存,高并发处理。

UWSGI: Python应用程序服务器,WSGI兼容,多进程管理,快速应用部署,多种协议支持。

在部署过程中,Nginx通常负责处理外部的HTTP请求,并将动态请求转发给UWSGI,而UWSGI则负责运行Django应用并处理这些请求。同时,Nginx还可以处理静态文件的请求,直接返回给客户端,而不需要将请求发送到UWSGI,这样可以提高响应速度并减轻后端的压力。

在这里插入图片描述

UWSGI

  • 安装UWSGI(在虚拟环境下)

    source /envs/django_order_system/bin/activate
    pip install uwsgi
    
  • 此时就可以直接通过uwsgi直接运行了(不使用nginx时,uwsgi就直接监听80端口了)

    uwsgi --http :80 --chdir data/www/order_system/django_order_system/ --wsgi-file django_order_system/wsgi.py --master --processes 1 --static-map /static=data/www/order_system/allstatic
    

    必须从root目录下运行,因为root目录下有data,因为根目录设置的data/…

    在这里插入图片描述

  • 还可以通过文件的形式,将命令行中的信息写到ini结尾的文件中

    django_order_system_uwsgi.ini

    [uwsgi]
    http = 0.0.0.0:80
    chdir = data/www/order_system/django_order_system/
    wsgi-file = django_order_system/wsgi.py
    processes = 1
    static-map = /static=data/www/order_system/allstatic
    
    virtualenv = /envs/django_order_system/
    
    uwsgi --http :80 --chdir data/www/order_system/django_order_system/ --wsgi-file django_order_system/wsgi.py --master --processes 1 --static-map /static=data/www/order_system/allstatic
    

    在这里插入图片描述

    此时运行就可以通过该配置文件

    uwsgi --ini data/www/order_system/django_order_system/django_order_system_uwsgi.ini
    

    后台跑:

    nohup uwsgi --ini data/www/order_system/django_order_system/django_order_system_uwsgi.ini &
    

nginx

  • 安装nginx

    yum install nginx -y
    
  • 配置文件(默认地址:/etc/nginx/nginx.conf

    (django_order_system) [root@iZ2zeas0eqklkqrh05ocdvZ nginx]# cat nginx.conf
    
    user root;
    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 4096;
    
        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;
        
        upstream django{
        	server 127.0.0.1:8001;
        }
    
        server {
            listen       80;
            listen       [::]:80;
            server_name  _;
    
            # Load configuration files for the default server block.
            include /etc/nginx/default.d/*.conf;
    
            # 静态文件路径设置
            location /static/ {
                alias  /data/www/order_system/allstatic/;
            }
    
            # 所有其他请求都转发到Django
            location / {
                uwsgi_pass  django;
                include     uwsgi_params;
            }
        }
    }
    
  • 启动ngnix

    systemctl start nginx
    systemctl enable nginx
    
  • 此时的UWSGI配置为

    [uwsgi]
    socket = 127.0.0.1:8001
    chdir = data/www/order_system/django_order_system/
    wsgi-file = django_order_system/wsgi.py
    processes = 1
    static-map = /static=data/www/order_system/allstatic
    
    virtualenv = /envs/django_order_system/
    
    uwsgi --ini data/www/order_system/django_order_system/django_order_system_nginx_uwsgi.ini
    
  • 后台跑:

    nohup uwsgi --ini data/www/order_system/django_order_system/django_order_system_nginx_uwsgi.ini &
    
  • 此时,项目就可以运行了,但是此时涉及到数据库操作就会出错

  • 而且,假如代码有更新,可能需要重启uwsgi,nginx,收集静态资源(如果静态资源有变化),此时就可以使用shell脚本实现

shell脚本

如果只有django代码更新的话:只需要重启uwsgi

如果django代码更新,静态文件也更新的话:重启uwsgi,收集静态资源python manage.py collect

如果涉及到nginx配置文件更新的话:需要重启nginx

  • reboot.sh(重启uwsgi)

    #!/usr/bin/env bash
    
    echo -e "\033[34m--------------------wsgi process--------------------\033[0m"
    
    ps -ef|grep django_order_system_nginx_uwsgi.ini | grep -v grep
    
    sleep 0.5
    
    echo -e '\n--------------------going to close--------------------'
    
    ps -ef |grep django_order_system_nginx_uwsgi.ini | grep -v grep | awk '{print $2}' | xargs kill -9
    
    sleep 0.5
    
    echo -e '\n----------check if the kill action is correct----------'
    
    /envs/django_order_system/bin/uwsgi  --ini django_order_system_nginx_uwsgi.ini &  >/dev/null
    
    echo -e '\n\033[42;1m----------------------started...----------------------\033[0m'
    sleep 1
    
    ps -ef |grep django_order_system_nginx_uwsgi.ini | grep -v grep
    
  • stop.sh(停止uwsgi)

    #!/usr/bin/env bash
    
    echo -e "\033[34m--------------------wsgi process--------------------\033[0m"
    
    ps -ef|grep django_order_system_nginx_uwsgi.ini | grep -v grep
    
    sleep 0.5
    
    echo -e '\n--------------------going to close--------------------'
    
    ps -ef |grep django_order_system_nginx_uwsgi.ini | grep -v grep | awk '{print $2}' | xargs kill -9
    
    sleep 0.5
    

数据库同步

  • 创建数据库

    python manage.py makemigrations
    python manage.py migrate
    
  • 执行离线脚本,初始化数据

    在这里插入图片描述

域名

  • 注册域名

    在这里插入图片描述

  • 解析域名

    在这里插入图片描述

  • 通过审核后即可访问该域名

https部署

  • 个人测试可以白嫖20个证书

    在这里插入图片描述

  • 创建证书

    在这里插入图片描述

  • 下载证书

  • 将证书上传到服务器

  • 修改nginx配置(https)

    (django_order_system) [root@iZ2zeas0eqklkqrh05ocdvZ nginx]# cat nginx.conf
    
    user root;
    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 4096;
    
        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;
        
        upstream django{
        	server 127.0.0.1:8001;
        }
        
        server {
            listen      80;
            server_name duanpeitong.cn;
            rewrite ^(.*) https://$server_name$1 redirect;
        }
        
        server {
            listen       443 ssl;
            server_name  duanpeitong.cn;
    
            #证书文件路径
            ssl_certificate      /data/www/order_system/ssl/8372403_day06.pythonav.com.pem;
            #私钥文件
            ssl_certificate_key  /data/www/order_system/ssl/8372403_day06.pythonav.com.key;
    
            ssl_session_cache    shared:SSL:1m;
            ssl_session_timeout  5m;
            ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
            ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
            ssl_prefer_server_ciphers  on;
            
            # 静态文件路径设置
            location /static/ {
                alias  /data/www/order_system/allstatic/;
            }
            
            # 所有其他请求都转发到Django
            location / {
                uwsgi_pass  django;
                include     uwsgi_params;
            }
        }
    }
    

总结

这只是最基本的部署流程,还有很多地方可以扩展:

  • supervisor杀进程
  • 加入日志处理(nginx,uwsgi,django各个级别的日志)
  • 服务器中用户组,权限的管理(本项目都是通过root用户操作的,而在公司中你只具有一定的权限)
  • 进程数(一般是核数*2),并发,最多可以有多少人等待

若有错误与不足请指出,关注DPT一起进步吧!!!

  • 21
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叫我DPT

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值