Cenos7 环境下使用 Docker 部署 Django+nginx+uwsgi 环境
一, 系统要求
此次略, 详见
二, 项目目录结构
此环境使用 docker-compose 来编排各个容器的的配置, 包括构建 django 镜像, 项目目录结构如下图:
三, 重点配置文件介绍
0. 相关变量配置 .env 文件
1.docker-compose 编排工具文件配置 docker-compose.YAML
- version: '3'
- services:
- ### Django Project-1 container #########################################
- book:
- build:
- #构建 django 项目 book 容器的路径
- context: ./build/django
- volumes: #项目数据持久化配置 --- 包括项目, uwsgi 配置文件
- - ${PROJECT_FOLDER}/book:/usr/src/App/book:rw
- - ./work/components/uwsgi/book/uwsgi.INI:/etc/uwsgi/book/uwsgi.INI:rw
- restart: always #系统重启后容器自动启动
- privileged: true #容器启动后是否有系统权限
- networks: #定义该容器的网桥名称和 IP 地址
- net-django:
- ipv4_address: 10.127.2.5
- command: uwsgi --INI /etc/uwsgi/book/uwsgi.INI #容器启动后执行的命令, 必须要此命令, 以项目 uwsgi 配置文件启动 uwsgi
- ### Django Project-2 container #########################################
- hos:
- build:
- #构建 django 项目 hos 容器的路径
- context: ./build/django
- volumes: #项目数据持久化配置 --- 包括项目, uwsgi 配置文件
- - ${PROJECT_FOLDER}/hos:/usr/src/App/hos:rw
- - ./work/components/uwsgi/hos/uwsgi.INI:/etc/uwsgi/hos/uwsgi.INI:rw
- restart: always #系统重启后容器自动启动
- privileged: true #容器启动后是否有系统权限
- networks: #定义该容器的网桥名称和 IP 地址
- net-django:
- ipv4_address: 10.127.2.4
- command: uwsgi --INI /etc/uwsgi/hos/uwsgi.INI #容器启动后执行的命令, 必须要此命令, 以项目 uwsgi 配置文件启动 uwsgi
- ### Nginx container #########################################
- nginx:
- image: nginx:1.17-alpine #定义 nginx 使用的镜像, 直接从 docker hub 上获取
- ports: #定义 nginx 开放的 web 端口 80 和 8080
- - "80:80"
- - "8080:8080"
- volumes: #nginx 数据持久化配置 --- 包括项目, 项目 nginx 配置文件和 nginx 日志文件
- - ${PROJECT_FOLDER}/:/usr/src/App:rw
- - ./work/components/nginx/config/nginx.conf:/etc/nginx/nginx.conf:rw #Windows 需要 rw 权限才能开启 nginx 服务
- - ./work/components/nginx/config/conf.d:/etc/nginx/conf.d:rw
- - ./work/components/nginx/log:/var/log/nginx:rw
- restart: always #系统重启后容器自动启动
- privileged: true #容器启动后是否有系统权限
- networks: #定义 nginx 容器的网桥名称和 IP 地址
- net-django:
- ipv4_address: 10.127.2.3
- ### Mariadb container #########################################
- mariadb:
- image: mariadb:10.4.7 #定义 mariadb 使用的镜像, 直接从 docker hub 上获取
- ports: #定义 mariadb 开放的数据库端口 根据. env 配置变量获取 ${MYSQL_PORT} 端口
- - "${MYSQL_PORT}:3306" # ${MYSQL_PORT} 表示地址映射后的端口, 3306 表示 mariadb 容器内的端口
- volumes: #mariadb 数据持久化配置 --- 包括数据库存储位置, 数据库配置文件和数据库日志文件路径
- - ./work/components/mariadb/data:/var/lib/MySQL:rw
- - ./work/components/mariadb/config/MySQL.cnf:/etc/MySQL/conf.d/MySQL.cnf:rw
- - ./work/components/mariadb/log:/var/log/MySQL:rw
- restart: always #系统重启后容器自动启动
- privileged: true #容器启动后是否有系统权限
- environment:
- MYSQL_ROOT_PASSWORD: ${MYSQL_PASSWORD} #通过系统变量 ${MYSQL_PASSWORD} 设置 mariadb 镜像定义的变量 MYSQL_ROOT_PASSWORD
- networks: #定义 mariadb 容器的网桥名称和 IP 地址
- net-django:
- ipv4_address: 10.127.2.2
- ### Add Network Bridge info #########################################
- networks: #定义整个 docker-compose 编排的容器使用的网桥名称和 IP 地址网段, 注意 x.x.x.1 不能使用
- net-django:
- ipam:
- config:
- - subnet: 10.127.2.0/24
2. 构建 django 镜像文件 Dockerfile 配置 Dockerfile
- # 定义该镜像使用的基础镜像为: python:3.7.4-stretch
- FROM python:3.7.4-stretch
- # 定义容器的工作目录
- WORKDIR /usr/src/App
- # 拷贝 pip 安装项目所需要的包名称和第三方 django xadmin 管理后台
- COPY requirements.txt ./
- COPY xadmin-django2.zip ./
- # 安装
- RUN pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple && pip install ./xadmin-django2.zip
- # 开放的端口
- EXPOSE 8000
3.mariadb 数据库配置文件
- # The MySQL Client configuration file.
- #
- # For explanations see
- # http://dev.mysql.com/doc/mysql/en/server-system-variables.html
- [MySQL]
- [mysqld]
- sql-mode="STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
- innodb_force_recovery = 1
- character-set-server=utf8
- explicit_defaults_for_timestamp=true
4.uwsgi 配置文件
说明: uwsgi 是通过 pip install uwsgi 方式安装的
- [uwsgi]
- uid = www-data
- gid = www-data
- master = true
;django 项目目录路径
chdir = /usr/src/App/book/
; 项目的 wsgi 测试服务器配置文件位置
- wsgi-file = /usr/src/App/book/book/wsgi.py
- module = hos.wsgi
; 定义 socket 的地址和端口
- socket = 10.127.2.5:8000
- processes = 8
- threads = 1
- enable-threads = true
- chmod-socket = 664
- chown-socket = book:www-data
- vacuum = true
; 定义静态文件
- static-map=/static=/usr/src/App/book/static
- buffer-size = 65535
book 项目的 uwsgi.INI 配置文件
- [uwsgi]
- uid = www-data
- gid = www-data
- master = true
;django 项目目录路径
chdir = /usr/src/App/hos/
; 项目的 wsgi 测试服务器配置文件位置
- wsgi-file = /usr/src/App/book/hos/wsgi.py
- module = hos.wsgi
; 定义 socket 的地址和端口
- socket = 10.127.2.4:8000
- processes = 8
- threads = 1
- enable-threads = true
- chmod-socket = 664
- chown-socket = hos:www-data
- vacuum = true
; 定义静态文件
- static-map=/static=/usr/src/App/hos/static
- buffer-size = 65535
hos 项目的 uwsgi.INI 配置文件
5.nginx 主配置文件和项目配置文件
- user nginx;
- worker_processes 1;
- pid /var/run/nginx.pid;
- error_log /var/log/nginx/error.log warn;
- events {
- worker_connections 1024;
- }
- http {
- include /etc/nginx/mime.types;
- default_type application/octet-stream;
- 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 /dev/null;
- access_log /var/log/nginx/access.log main;
- sendfile on;
- #tcp_nopush on;
- keepalive_timeout 65;
- #gzip on;
- include /etc/nginx/conf.d/*.conf;
- }
nginx 主配置文件
- server {
- client_max_body_size 1024m;
- listen 80;
- server_name localhost;
- charset utf-8;
- location / {
- include uwsgi_params;
- #定义 uwsgi 的 socket, 即 django 项目容器的地址和端口
- uwsgi_pass 10.127.2.5:8000;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header Host $host;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- proxy_set_header X-Forwarded-Proto $scheme;
- }
- #定义项目静态文件目录, 注意 alias 和 root 的区别
- location /static {
- alias /usr/src/App/book/static;
- }
- error_page 404 /404.HTML;
- location = /40x.HTML {
- }
- error_page 500 502 503 504 /50x.HTML;
- location = /50x.HTML {
- }
- }
- server {
- client_max_body_size 1024m;
- listen 8080;
- server_name localhost;
- charset utf-8;
- location / {
- include uwsgi_params;
- #定义 uwsgi 的 socket, 即 django 项目容器的地址和端口
- uwsgi_pass 10.127.2.4:8000;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header Host $host;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- proxy_set_header X-Forwarded-Proto $scheme;
- }
- #定义项目静态文件目录, 注意 alias 和 root 的区别
- location /static {
- alias /usr/src/App/hos/static;
- }
- error_page 404 /404.HTML;
- location = /40x.HTML {
- }
- error_page 500 502 503 504 /50x.HTML;
- location = /50x.HTML {
- }
- }
nginx 的 django 项目配置文件
6.django 项目配置文件
四, 启动与停止
来源: http://www.bubuko.com/infodetail-3201487.html