![](https://files.ynotes.cn/18-7-23/70377481.jpg)
#### **项目目录结构**
```bash
blog
|-- docker-compose.yml #docker-compose编排文件
|-- mysql
| |-- conf
| | `-- mysqld.cnf #mysql配置文件
| |-- data #mysql数据存放目录
| |-- db_init_sql
| | `-- blog.sql #blog的sql数据表结构
| `-- log #mysql日志目录
|-- nginx
| |-- conf
| | |-- mysite.template #生成blog.itisme.co.conf配置的样例文件
| | `-- nginx.conf #nginx配置文件
| |-- log #nginx日志目录
| |
| |
| `-- ssl
| |-- fullchain.pem #ssl证书链
| `-- privkey.pem #ssl证书私钥
`-- uwsgi-django
|-- build
| |-- Dockerfile #构建uwsgi-django镜像的文件
| `-- requirements.txt #django项目需要的安装包
|-- conf
| `-- config.ini #uwsgi启动配置参数
`-- my_project #django项目
|-- blog #blog应用
|-- manage.py
`-- my_project
```
#### **新建docker项目数据配置存放目录**
```bash
$ mkdir /data/docker_project/blog -p
$ cd /data/docker_project/blog
```
#### **创建mysql容器使用的目录**
```bash
$ mkdir mysql/{conf,data,db_init_sql,log} -p
$ chmod 777 mysql/log
```
conf:存放mysql配置文件
data:存放mysql数据的目录
db_init_sql:存放的是mysql容器初始化的sql(存放django项目的建表语句)
log:存放mysql日志,修改权限为777
#### **编辑mysql配置文件mysql/conf/mysqld.cnf(项目使用emoji表情,编码使用的是utf8mb4)**
```bash
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
default-time-zone = '+08:00'
character-set-server=utf8
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
character-set-client-handshake = FALSE
innodb_buffer_pool_size = 128M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
```
#### **创建uwsgi-django容器使用的目录**
```bash
$ mkdir uwsgi-django/{build,my_project,conf} -p
```
my_project:存放django项目
build:存放构建镜像uwsgi-django需要的文件
conf:存放uwsgi启动的配置文件
#### **创建uwsgi-django的Dockerfile文件(用于构建uwsgi-django镜像)**
```bash
$ vim uwsgi-django/build/Dockerfile
```
```
FROM python:2.7-slim
RUN apt-get update && apt-get install -y \
gcc \
gettext \
mysql-client default-libmysqlclient-dev \
libpq-dev \
--no-install-recommends && rm -rf /var/lib/apt/lists/*
ENV DJANGO_VERSION 1.9.5
RUN pip install mysqlclient psycopg2 uwsgi django=="$DJANGO_VERSION"
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install -r requirements.txt
WORKDIR /usr/src/app/my_project
```
#### **项目的requirements.txt上传到django目录(项目所在的环境执行)**
```bash
$ pip freeze >/root/requirements.txt
```
#### **拷贝requirements.txt到docker-compose编排所在机器uwsgi-django目录**
```bash
$ scp /root/requirements.txt root@docker-host:/data/docker_project/blog/uwsgi-django/build
```
### **增加uwsgi程序的配置文件uwsgi-django/conf/config.ini**
```
[uwsgi]
socket = 0.0.0.0:8888
chdir = /usr/src/app/my_project
module = my_project.wsgi
master = true
processes = 10
socket = /tmp/my_project.sock
vacuum = true
uid = 498
```
#### **创建nginx容器使用的目录**
```bash
$ mkdir nginx/{conf,ssl,log}
$ chmod 777 nginx/log
```
conf:存放nginx的配置文件
ssl:存放SSL证书目录
log:存放日志目录
#### **编辑nginx/conf/nginx.conf**
```nginx
user nginx;
worker_processes 2;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
use epoll;
worker_connections 10240;
}
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 /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/conf.d/blog.itisme.co.conf;
}
```
#### **编辑nginx/conf/mysite.template(生成blog.itisme.co.conf的模板文件,通过envsubst替换环境变量)**
```nginx
upstream uwsgi-django {
server uwsgi-django:$UWSGI_PORT; # for a web port socket (we'll use this first)
}
server {
listen $NGINX_PORT;
server_name $NGINX_HOST;
charset utf-8;
rewrite ^(.*)$ https://${server_name}$1 permanent;
}
server {
listen $NGINX_SSL_PORT ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name $NGINX_HOST;
#证书配置
ssl_certificate "/etc/nginx/ssl/blog.itisme.co/fullchain.pem";
ssl_certificate_key "/etc/nginx/ssl/blog.itisme.co/privkey.pem";
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
# max upload size
client_max_body_size 75M; # adjust to taste
#django项目的上传文件的目录
location /upload {
alias /data/app/my_project/my_project/upload; # your Django project's media files - amend as required
}
#django项目的静态文件目录
location /static {
alias /data/app/my_project/my_project/static_all;
}
#django项目uwsgi配置
location / {
uwsgi_pass uwsgi-django;
include /data/app/my_project/my_project/uwsgi_params; # the uwsgi_params file you installed
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
```
#### **拷贝证书到nginx/ssl目录**
```bash
$ scp fullchain.pem root@docker-host:/data/docker_project/blog/nginx/ssl
$ scp privkey.pem root@docker-host:/data/docker_project/blog/nginx/ssl
```
#### **编辑docker-compose.yml (客户端->nginx->uwsgi->django->db)**
```bash
$ vim docker-compose.yml
```
```yaml
version: '3'
services:
db:
image: mysql:5.7
restart: always
container_name: blog-db
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_DATABASE: blog
MYSQL_USER: blog
MYSQL_PASSWORD: 123456
volumes:
#挂载mysql配置文件
- ./mysql/conf/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf
#挂载数据库初始化脚本
- ./mysql/db_init_sql:/docker-entrypoint-initdb.d
#挂载mysql数据目录
- ./mysql/data:/var/lib/mysql
#挂载mysql日志目录
- ./mysql/log:/var/log
uwsgi-django:
#使用./uwsgi-django/build/目录下的Dockerfile构建镜像
build: ./uwsgi-django/build/
#当存在build时,image参数表示的是构建后的镜像名
image: uwsgi-django:1.9.5
restart: always
depends_on:
- db
container_name: blog-uwsgi-django
environment:
DB_NAME: blog
DB_USER: blog
DB_PASS: 123456
DB_PORT: 3306
WEB_URL: blog.itisme.co
volumes:
#挂载django项目
- ./uwsgi-django/my_project:/usr/src/app/my_project
#挂载uwsgi配置文件
- ./uwsgi-django/conf:/usr/src/app/uwsgi/conf
command: uwsgi /usr/src/app/uwsgi/conf/config.ini
nginx:
#使用nginx官方稳定版镜像
image: nginx:stable
restart: always
depends_on:
- uwsgi-django
container_name: blog-nginx
environment:
NGINX_HOST: blog.itisme.co
NGINX_PORT: 80
NGINX_SSL_PORT: 443
UWSGI_PORT: 8888
ports:
- 80:80
- 443:443
volumes:
#挂载nginx配置
- ./nginx/conf/nginx.conf:/etc/nginx/nginx.conf
#挂载站点模板文件
- ./nginx/conf/mysite.template:/etc/nginx/conf.d/mysite.template
#挂载ssl证书
- ./nginx/ssl/fullchain.pem:/etc/nginx/ssl/blog.itisme.co/fullchain.pem
- ./nginx/ssl/privkey.pem:/etc/nginx/ssl/blog.itisme.co/privkey.pem
#挂载项目静态文件
- ./uwsgi-django/my_project/my_project/upload:/data/app/my_project/my_project/upload
- ./uwsgi-django/my_project/my_project/static_all:/data/app/my_project/my_project/static_all
#挂载uwsgi参数文件
- ./uwsgi-django/my_project/my_project/uwsgi_params:/data/app/my_project/my_project/uwsgi_params
- ./nginx/log/:/var/log/nginx/
#envsubst替换/etc/nginx/conf.d/mysite.template变量
command: /bin/bash -c "envsubst < /etc/nginx/conf.d/mysite.template > /etc/nginx/conf.d/blog.itisme.co.conf && nginx -g 'daemon off;'"
```
#### **修改django项目的mysql配置(修改项目里的mysql配置成上面的environment中指定的环境变量)**
```bash
import os
_env = os.environ
#django指定可以访问的域
DOMAIN = _env['WEB_URL']
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': _env['DB_NAME'],
'HOST': 'db',
'PORT': _env['DB_PORT'],
'USER': _env['DB_USER'],
'PASSWORD': _env['DB_PASS'],
'OPTIONS': {'charset':'utf8mb4'},
}
}
```
#### **启动项目**
```bash
$ docker-compose up
```
#### **启动项目后台运行**
```bash
$ docker-compose up -d
```
#### **查看docker进程**
```bash
$ docker-compose ps
```
```
Name Command State Ports
----------------------------------------------------------------------
blog-db docker-entrypoint.sh mysqld Up 3306/tcp
blog-nginx /bin/bash -c envsubst < /e ... Up 0.0.0.0:443->443/tcp, 0.0.0.0:8880->80/tcp
blog-uwsgi-django uwsgi /usr/src/app/uwsgi/c ... Up
```
#### **浏览器访问https://ynotes.cn/blog/**