• django系列13 - 部署代码到云服务器上


    1、部署 portainer

    1. 安装docker、docker-compose,参考:docker-compose安装
    2. 云服务器中执行:
    yum update
    mkdir /opt/vhosts/portainer
    cd /opt/vhosts/portainer
    touch docker-compose.yml
    vim docker-compose.yml
    
    • 1
    • 2
    • 3
    • 4
    • 5
    1. 复制以下内容到 docker-compose.yml
    version: '3'
    
    services:
      portainer:
        image: portainer/portainer-ce
        container_name: portainer
        restart: always
        ports:
          - 9001:9000
        command: -H unix:///var/run/docker.sock
        volumes:
          - /var/run/docker.sock:/var/run/docker.sock
          - portainer_data:/data
    
    volumes:
      portainer_data:
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    1. 登录 portainer 网站:http://云服务器IP:9001 ,创建管理员账号:admin / admin@123456
      在这里插入图片描述

    2、拉取源码并配置环境变量

    mkdir /opt/vhosts/auto-test-plt-project
    cd /opt/vhosts/auto-test-plt-project
    git clone https://github.com/tengfei-jiao/auto_test_platform.git
    git pull origin master
    cd auto_test_platform
    cp .env.sample .env
    vim .env
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    复制以下内容到.env文件中,注意修改你的云服务IP

    DEBUG=False
    DATABASE_URL=mysql://auto_test_plt:atp2021$Atstudy@mysql-8:3306/auto_test_plt
    TEST_PLT_API_TIMEOUT_CONNECT=3.1
    TEST_PLT_API_TIMEOUT_RESP=30
    LIST_PER_PAGE=10
    LOG_DIR=/var/log/django
    ERROR_LOG_FILE=error.log
    INFO_LOG_FILE=info.log
    LOG_TEST_PLT_LEVEL=WARNING
    CELERY_BROKER_URL=amqp://rabbit:atstudy@你的云服务IP:5672/test_plt
    CELERY_RESULT_EXPIRES=259200
    CELERY_TASK_TIME_LIMIT=600
    DINGTALK_WEB_HOOK_TOKEN=xxx
    DINGTALK_WEB_HOOK_SIGN=xxx
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    3、使用docker部署代码

    3.1 创建项目的docker镜像及数据卷

    # 构建镜像
    cd /opt/vhosts/auto-test-plt-project/auto_test_platform
    docker build -t auto_test_platform:v1.0.0 .
    
    # 创建数据卷
    cd /opt/vhosts/auto-test-plt-project
    mkdir -p ./mysql-data 
    mkdir -p ./rabbitmq-data 
    mkdir -p ./logs/rabbitmq 
    mkdir -p ./logs/django 
    mkdir -p ./logs/worker 
    mkdir -p ./logs/beat 
    mkdir -p ./logs/flower 
    mkdir -p ./logs/nginx 
    mkdir -p ./nginx/
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    3.2、编写nginx、docker-compose配置文件

    cd /opt/vhosts/auto-test-plt-project
    touch nginx/nginx.conf
    vim nginx/nginx.conf
    
    • 1
    • 2
    • 3

    复制以下内容到 nginx.conf中

    #user nobody;
    worker_processes 1;
    error_log /var/log/nginx/error.log;
    
    events {
      worker_connections 1024; 
    }
    
    http {
      include mime.types;
      default_type application/octet-stream;
      access_log /var/log/nginx/access.log;
      sendfile on;
      keepalive_timeout 65;
      
      server {
        listen 80;
        server_name auto_test_platform;
    		
        location /static/ {
          expires 30d;
          autoindex on;
          add_header Cache-Control private;
          alias /static/;
        }
    			
        location / {
          proxy_pass http://auto_test_platform-web:8000;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header Host $host;
          proxy_redirect off;
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    创建 docker-compose.yml 文件,并执行:docker-compose up -d

    cd /opt/vhosts/auto-test-plt-project
    touch docker-compose.yml
    vim docker-compose.yml
    
    • 1
    • 2
    • 3
    version: '3'
    
    services:
      db:
        # ★注意:该镜像默认已经包含CONVERT_TZ所需的时区数据,不需要再从官网下载导入
        image: mysql
        container_name: mysql-8
        command: --default-authentication-plugin=mysql_native_password --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --innodb_buffer_pool_size=512M
        restart: always
        ports:
          - 3306:3306
        volumes:
          - ./mysql-data:/var/lib/mysql
        environment:
          MYSQL_ROOT_PASSWORD: Woaizhongguo@21cen
          MYSQL_DATABASE: auto_test_plt
          MYSQL_USER: auto_test_plt
          MYSQL_PASSWORD: atp2021$$Atstudy
          # ★注意:小心在docker-compose yml中$字面量必须用$$代替,尽量避免在密码中使用$。
        networks:
          - default
    
      phpmyadmin:
        image: phpmyadmin
        container_name: phpmyadmin
        depends_on:
          - db
        restart: always
        ports:
          - 8080:80
        environment:
          - PMA_HOST=mysql-8
          - PMA_PORT=3306
          - UPLOAD_LIMIT=20M
        networks:
          - default
    
      rabbitmq:
        image: rabbitmq:management
        container_name: rabbitmq-common
        # ★注意:先建立目录
        volumes:
          - ./rabbitmq-data/:/var/lib/rabbitmq/
        restart: always
        ports:
          - 5672:5672
          - 15672:15672
        environment:
          - RABBITMQ_DEFAULT_USER=rabbit
          - RABBITMQ_DEFAULT_PASS=atstudy
        networks:
          - default
    
      auto_test_plt:
        # ★注意:先git clone源码然后构建镜像 docker build -t auto_test_platform:v1.0.0 .
        image: auto_test_platform:v1.0.0
        container_name: auto_test_platform-web
        depends_on:
          - db
        working_dir: /app
        # ★注意:先建立目录
        volumes:
          - ./auto_test_platform:/app
          - ./logs/django:/var/log/django
        expose:
          - 8000
        networks:
          - default
    
      # 注意worker和beat的日志级别,稳定投产之后就不要用info了
      celery_worker:
        image: auto_test_platform:v1.0.0
        container_name: auto_test_platform-celery_worker
        depends_on:
          - auto_test_plt
          - rabbitmq
        working_dir: /app
        # ★注意:先建立目录volumes
        volumes:
          - ./auto_test_platform:/app
          - ./logs/worker:/var/log/worker
        entrypoint: celery -A auto_test_platform worker -l info -f /var/log/worker/celery-worker.log
        networks:
          - default
    
      celery_beat:
        image: auto_test_platform:v1.0.0
        container_name: auto_test_platform-celery_beat
        depends_on:
          - auto_test_plt
          - rabbitmq
        working_dir: /app
        # ★注意:先建立目录
        volumes:
          - ./auto_test_platform:/app
          - ./logs/beat:/var/log/beat
        entrypoint: celery -A auto_test_platform beat -l info -f /var/log/beat/celery-beat.log
        networks:
          - default
    
      celery_flower:
        image: auto_test_platform:v1.0.0
        container_name: auto_test_platform-celery_flower
        depends_on:
          - auto_test_plt
          - rabbitmq
        working_dir: /app
        ports:
          - 5555:5555
        volumes:
          - ./auto_test_platform:/app
          - ./logs/flower:/var/log/flower
        entrypoint: celery -A auto_test_platform flower --port=5555 --broker_api=http://rabbit:51testing@rabbitmq-common:15672/api/
        networks:
          - default
    
      nginx:
        image: nginx:latest
        container_name: auto_test_platform-nginx
        depends_on:
          - auto_test_plt
        # ★注意:先建立目录,复制nginx.conf
        volumes:
          - ./auto_test_platform/static:/static
          - ./nginx/nginx.conf:/etc/nginx/nginx.conf
          - ./logs/nginx:/var/log/nginx
        ports:
          - 80:80
        networks:
          - default
    
    networks:
      default:
        driver: bridge
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134

    4、验证各个网站是否正常

    portainer平台:http://你的云服务IP:9001【admin / admin@123456】
    phpmyadmin平台:http://你的云服务IP:8080/【auto_test_plt / atp2021$Atstudy】
    flower平台:http://你的云服务IP:5555/
    rabbitmq平台:http://你的云服务IP:15672/【rabbit / atstudy】
    接口自动化测试平台:http://你的云服务IP/admin 【root / test@123】
    
    • 1
    • 2
    • 3
    • 4
    • 5
    1. 登录 rabbitmq 并创建 host:
      请添加图片描述
    2. 登录 portainer 并重启各个服务,检查是否运行正常
      在这里插入图片描述

    5、其他问题处理

    其他问题的处理文章末加博主微信即可

  • 相关阅读:
    亚马逊账户评级多久更新一次,亚马逊账户评级一般多少分-站斧浏览器
    2、中间件-CAP理论
    App Inventor 2 实现Ascii码转换(Ascii编码与解码)
    CTF--Web安全--SQL注入之‘绕过方法’
    C++ Primer 二 字符串、向量和数组
    Hexo最新实战:(一)Hexo7.0+GitHub Pages博客搭建
    ucontext的简单介绍
    vue draggable怎么用?怎么写一个拖拽的看板?
    scala 转换、过滤、分组、排序
    ovn metadata (by quqi99)
  • 原文地址:https://blog.csdn.net/weixin_45451320/article/details/126559330