Nginx多容器部署web项目uwsgi+Django+Redis+Mysql

一、准备工作

1.镜像拉取

# 查看自己的镜像
docker images

# 拉取项目需要的镜像
docker pull python:3.6
docker pull mysql:5.7
docker pull redis

Nginx多容器部署web项目uwsgi+Django+Redis+Mysql

2.构建docker网络,让他们在同一个网络下实现互通

docker network ls # 查看自己的docker网络
docker network create luffu\y  # 创建luffy网络

Nginx多容器部署web项目uwsgi+Django+Redis+Mysql

二、测试网络

1.启动mysql

# 需要指定root
docker run --name mysql --privileged=true -p 3307:3306  -v /data/mysql/datadir:/var/lib/mysql -v /data/mysql/conf.d:/etc/mysql/conf.d -e  MYSQL_ROOT_PASSWORD=123456 -d --network luffy  mysql:5.7

  --name:指定容器名

--p:映射宿主主机端口  因为本机有了一个3306端口 所以我改成了3307

-v:挂载宿主目录到容器目录

-e:设置环境变量,此处指定root密码

-d:后台运行容器

--privileged=true 防止挂载数据卷出现权限问题

--network 指定网络名称空间

2.启动python

docker run -dit --network luffy --name python python:3.6 /bin/bash
# 这里我没有指定映射端口 先不慌

3.启动redis

docker run -dit --network luffy --name redis -p 6377:6379 -v /data/redis:/data/redis nginx

4.查看启动的容器并测试网络

docker ps 

Nginx多容器部署web项目uwsgi+Django+Redis+Mysql

docker exec -it python ping mysql

Nginx多容器部署web项目uwsgi+Django+Redis+Mysql

ok,并且记录下mysql的本地回环地址172.18.0.2 端口号 3306(可以在启动容器时自己指定)

也可以通过docker inspect mysql(容器名)来查看id

Nginx多容器部署web项目uwsgi+Django+Redis+Mysql

5.进入到mysql容器里面创建luffy数据库

5.1进入容器设置客户端访问权限

docker exec -it mysql bash  # 进入容器执行bash
mysql -uroot -p123456  # 登录

ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
mysql> create database luffy default charset utf8;  # 创建数据库 根据django连接的数据库
# 设置权限账号密码:账号密码要与项目中配置的一致
mysql> grant all privileges on luffy.* to 'luffy'@'%' identified by 'Luffy123?';
mysql> grant all privileges on luffy.* to 'luffy'@'localhost' identified by 'Luffy123?';
mysql> flush privileges;
--------------------------------------------------------
Query OK, 1 row affected (0.00 sec)


ctrl +P +Q #容器不停止退出

5.2 测试连接

mysql

Nginx多容器部署web项目uwsgi+Django+Redis+Mysql

redis

Nginx多容器部署web项目uwsgi+Django+Redis+Mysql

三、python准备工作

1、进入容器安装依赖

docker exec -it centos /bin/bash

安装软件管理包和可能使用的依赖

pip -V
---------------------------------
pip 20.3.1 from /usr/local/lib/python3.6/site-packages/pip (python 3.6)


pyhon -V
----------------------------------
Python 3.6.12

pip install uwsgi -i https://pypi.doubanio.com/simple  # 安装uwsgi
pip install django==2.2.2 -i https://pypi.doubanio.com/simple  # 安装django
----------------------------------
....
  Stored in directory: /root/.cache/pip/wheels/65/f4/1f/460436beb9f2a87299f8787db2b983854793ead943c540b058
Successfully built uwsgi
Installing collected packages: uwsgi

2.修改mysql和redis的配置

Nginx多容器部署web项目uwsgi+Django+Redis+Mysql

3.把luffy文件拷贝到容器上

docker cp 主机文件路径 容器id:容器内部路径

docker cp /home/luffy/luffy-api python:home/luffy

四、通过uwsgi启动python项目

1.进入python容器

docker exec -it python /bin/bash

2.查看导入的python项目

cd /home/luffy  # 根据自己的路径
ls
-------------------------------------------------------------
Luffy  celery_task  logs  luffyapi.xml	manage.py  manage_.py  requerment.txt  uwsgi.log

pip install -r requerment.txt -i https://pypi.doubanio.com/simple  # 安装项目依赖库

ps:如果需要再次修改配置文件我们需要安装一些工具

在使用docker容器时,有时候里边没有安装vim,敲vim命令时提示说:vim: command not found,这个时候就需要安装vim,可是当你敲apt-get install vim命令时,提示:未找到指令
# 我们需要
apt-get update

#这个命令的作用是:同步 /etc/apt/sources.list 和 /etc/apt/sources.list.d 中列出的源的索引,这样才能获取到最新的软件包。

等更新完毕以后再敲命令:apt-get install vim命令即可。

3.进行数据库迁移

python manage_.py makemigrations
python manage_.py migrate

Nginx多容器部署web项目uwsgi+Django+Redis+Mysql

4.用Navicat创建点数据

INSERT INTO luffy_teacher(id, orders, is_show, is_delete, created_time, updated_time, name, role, title, signature, image, brief) VALUES (1, 1, 1, 0, '2019-07-14 13:44:19.661327', '2019-07-14 13:46:54.246271', 'Alex', 1, '老男孩Python教学总监', '金角大王', 'teacher/alex_icon.png', '老男孩教育CTO & CO-FOUNDER 国内知名PYTHON语言推广者 51CTO学院2016\2017年度最受学员喜爱10大讲师之一 多款开源软件作者 曾任职*部、飞信、中金公司、NOKIA中国研究院、华尔街英语、ADVENT、汽车之家等公司');

INSERT INTO luffy_teacher(id, orders, is_show, is_delete, created_time, updated_time, name, role, title, signature, image, brief) VALUES (2, 2, 1, 0, '2019-07-14 13:45:25.092902', '2019-07-14 13:45:25.092936', 'Mjj', 0, '前美团前端项目组架构师', NULL, 'teacher/mjj_icon.png', '是马JJ老师, 一个集美貌与才华于一身的男人,搞过几年IOS,又转了前端开发几年,曾就职于美团网任高级前端开发,后来因为不同意王兴(美团老板)的战略布局而出家做老师去了,有丰富的教学经验,开起车来也毫不含糊。一直专注在前端的前沿技术领域。同时,爱好抽烟、喝酒、烫头(锡纸烫)。 我的最爱是前端,因为前端妹子多。');

INSERT INTO luffy_teacher(id, orders, is_show, is_delete, created_time, updated_time, name, role, title, signature, image, brief) VALUES (3, 3, 1, 0, '2019-07-14 13:46:21.997846', '2019-07-14 13:46:21.997880', 'Lyy', 0, '老男孩Linux学科带头人', NULL, 'teacher/lyy_icon.png', 'Linux运维技术专家,老男孩Linux金牌讲师,讲课风趣幽默、深入浅出、声音洪亮到爆炸');

INSERT INTO luffy_course_category(id, orders, is_show, is_delete, created_time, updated_time, name) VALUES (1, 1, 1, 0, '2019-07-14 13:40:58.690413', '2019-07-14 13:40:58.690477', 'Python');

INSERT INTO luffy_course_category(id, orders, is_show, is_delete, created_time, updated_time, name) VALUES (2, 2, 1, 0, '2019-07-14 13:41:08.249735', '2019-07-14 13:41:08.249817', 'Linux');

INSERT INTO luffy_course(id, orders, is_show, is_delete, created_time, updated_time, name, course_img, course_type, brief, level, pub_date, period, attachment_path, status, students, sections, pub_sections, price, course_category_id, teacher_id) VALUES (1, 1, 1, 0, '2019-07-14 13:54:33.095201', '2019-07-14 13:54:33.095238', 'Python开发21天入门', 'courses/alex_python.png', 0, 'Python从入门到入土&&&Python从入门到入土&&&Python从入门到入土&&&Python从入门到入土&&&Python从入门到入土&&&Python从入门到入土&&&Python从入门到入土&&&Python从入门到入土&&&Python从入门到入土&&&Python从入门到入土&&&Python从入门到入土&&&Python从入门到入土', 0, '2019-07-14', 21, '', 0, 231, 120, 120, 0.00, 1, 1);

INSERT INTO luffy_course(id, orders, is_show, is_delete, created_time, updated_time, name, course_img, course_type, brief, level, pub_date, period, attachment_path, status, students, sections, pub_sections, price, course_category_id, teacher_id) VALUES (2, 2, 1, 0, '2019-07-14 13:56:05.051103', '2019-07-14 13:56:05.051142', 'Python项目实战', 'courses/mjj_python.png', 0, '', 1, '2019-07-14', 30, '', 0, 340, 120, 120, 99.00, 1, 2);

INSERT INTO luffy_course(id, orders, is_show, is_delete, created_time, updated_time, name, course_img, course_type, brief, level, pub_date, period, attachment_path, status, students, sections, pub_sections, price, course_category_id, teacher_id) VALUES (3, 3, 1, 0, '2019-07-14 13:57:21.190053', '2019-07-14 13:57:21.190095', 'Linux系统基础5周入门精讲', 'courses/lyy_linux.png', 0, '', 0, '2019-07-14', 25, '', 0, 219, 100, 100, 39.00, 2, 3);

INSERT INTO luffy_course_chapter(id, orders, is_show, is_delete, created_time, updated_time, chapter, name, summary, pub_date, course_id) VALUES (1, 1, 1, 0, '2019-07-14 13:58:34.867005', '2019-07-14 14:00:58.276541', 1, '计算机原理', '', '2019-07-14', 1);

INSERT INTO luffy_course_chapter(id, orders, is_show, is_delete, created_time, updated_time, chapter, name, summary, pub_date, course_id) VALUES (2, 2, 1, 0, '2019-07-14 13:58:48.051543', '2019-07-14 14:01:22.024206', 2, '环境搭建', '', '2019-07-14', 1);

INSERT INTO luffy_course_chapter(id, orders, is_show, is_delete, created_time, updated_time, chapter, name, summary, pub_date, course_id) VALUES (3, 3, 1, 0, '2019-07-14 13:59:09.878183', '2019-07-14 14:01:40.048608', 1, '项目创建', '', '2019-07-14', 2);

INSERT INTO luffy_course_chapter(id, orders, is_show, is_delete, created_time, updated_time, chapter, name, summary, pub_date, course_id) VALUES (4, 4, 1, 0, '2019-07-14 13:59:37.448626', '2019-07-14 14:01:58.709652', 1, 'Linux环境创建', '', '2019-07-14', 3);

INSERT INTO luffy_course_Section(id, is_show, is_delete, created_time, updated_time, name, orders, section_type, section_link, duration, pub_date, free_trail, chapter_id) VALUES (1, 1, 0, '2019-07-14 14:02:33.779098', '2019-07-14 14:02:33.779135', '计算机原理上', 1, 2, NULL, NULL, '2019-07-14 14:02:33.779193', 1, 1);

INSERT INTO luffy_course_Section(id, is_show, is_delete, created_time, updated_time, name, orders, section_type, section_link, duration, pub_date, free_trail, chapter_id) VALUES (2, 1, 0, '2019-07-14 14:02:56.657134', '2019-07-14 14:02:56.657173', '计算机原理下', 2, 2, NULL, NULL, '2019-07-14 14:02:56.657227', 1, 1);

INSERT INTO luffy_course_Section(id, is_show, is_delete, created_time, updated_time, name, orders, section_type, section_link, duration, pub_date, free_trail, chapter_id) VALUES (3, 1, 0, '2019-07-14 14:03:20.493324', '2019-07-14 14:03:52.329394', '环境搭建上', 1, 2, NULL, NULL, '2019-07-14 14:03:20.493420', 0, 2);

INSERT INTO luffy_course_Section(id, is_show, is_delete, created_time, updated_time, name, orders, section_type, section_link, duration, pub_date, free_trail, chapter_id) VALUES (4, 1, 0, '2019-07-14 14:03:36.472742', '2019-07-14 14:03:36.472779', '环境搭建下', 2, 2, NULL, NULL, '2019-07-14 14:03:36.472831', 0, 2);

INSERT INTO luffy_course_Section(id, is_show, is_delete, created_time, updated_time, name, orders, section_type, section_link, duration, pub_date, free_trail, chapter_id) VALUES (5, 1, 0, '2019-07-14 14:04:19.338153', '2019-07-14 14:04:19.338192', 'web项目的创建', 1, 2, NULL, NULL, '2019-07-14 14:04:19.338252', 1, 3);

INSERT INTO luffy_course_Section(id, is_show, is_delete, created_time, updated_time, name, orders, section_type, section_link, duration, pub_date, free_trail, chapter_id) VALUES (6, 1, 0, '2019-07-14 14:04:52.895855', '2019-07-14 14:04:52.895890', 'Linux的环境搭建', 1, 2, NULL, NULL, '2019-07-14 14:04:52.895942', 1, 4);

5.配置uwsgi文件

1)进行uwsgi服务配置,内容如下
>: vim /home/project/luffyapi/luffyapi.xml

<uwsgi>
   # 这里的172.18.0.3通过dockers insepct python 获取的ip地址
   <socket>172.18.0.3:9090</socket> <!-- 内部端口,自定义 --> 
   <chdir>/home/luffy/</chdir> <!-- 项目路径 -->            
   <module>Luffy.wsgi</module>  <!-- luffyapi为wsgi.py所在目录名--> 
   <processes>4</processes> <!-- 进程数 -->     
   <daemonize>uwsgi.log</daemonize> <!-- 日志文件 -->
</uwsgi>

6.先跑一下

python manage_.py runserver 0.0.0.0:9090
-------------------------------------------------------------
Performing system checks...

System check identified no issues (0 silenced).
January 21, 2021 - 10:22:14
Django version 2.2.2, using settings 'Luffy.settings.pro'
Starting development server at http://0.0.0.0:9090/
Quit the server with CONTROL-C.

跑起来了,但是我们外部是无法访问的,因为没有映射端口

7.将容器打包成镜像

7.1docker export [容器ID|容器名称]>[压缩包名称]

docker export python>luffy.tar

7.2docker import [压缩包名称] [镜像名称:Tag]

docker import luffy.tar luffy:v1

Nginx多容器部署web项目uwsgi+Django+Redis+Mysql

8 测试新镜像

Nginx多容器部署web项目uwsgi+Django+Redis+Mysql

docker run -dit --name python --network luffy -p9090:9090 -v /docker_test/nginx/static:/home/luffy/Luffy/static luffy:v1 /bin/bash

docker exec -it python /bin/bash


1)因为已经挂载了静态文件配置所以我们只需要收集就好了
#记得根据setting/pro.py下静态文件路径配置

2)完成静态文件迁移
>: python /home/luffy/manage_.py collectstatic

# 找到路径 并运行
python /home/luffy/manage_.py runserver 0.0.0.0:9090
-------------------------------------------------------------------------
Performing system checks...

System check identified no issues (0 silenced).
January 21, 2021 - 10:42:21
Django version 2.2.2, using settings 'Luffy.settings.pro'
Starting development server at http://0.0.0.0:9090/
Quit the server with CONTROL-C.

# 起来后我们访问一下后台
[ip地址]:9090/xadmin/

Nginx多容器部署web项目uwsgi+Django+Redis+Mysql

9.uwsgi启动

uwsgi -x /home/luffy/luffyapi.xml

五、nginx反向代理Vue和Django

nginx(不放在容器里)

5.1安装Nginx

1)前往用户根目录
>: cd ~

2)下载nginx1.13.7
>: wget http://nginx.org/download/nginx-1.13.7.tar.gz

3)解压安装包
>: tar -xf nginx-1.13.7.tar.gz

4)进入目标文件
>: cd nginx-1.13.7

5)配置安装路径:/usr/local/nginx
>: ./configure --prefix=/usr/local/nginx

6)编译并安装
>: make && sudo make install

7)建立软连接:终端命令 nginx
>: ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx

8)删除安装包与文件:
>: cd ~
>: rm -rf nginx-1.13.7
>: rm -rf nginx-1.13.7.tar.xz

9)测试Nginx环境,服务器运行nginx,本地访问服务器ip
>: nginx
>: 服务器绑定的域名 或 ip:80

5.2配置文件构建

4)去向Nginx配置目录,备份配置,完全更新配置:填入下方内容
>: vim /usr/local/nginx/conf/nginx.conf


events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    server {
        listen 80;
        server_name  127.0.0.1; # 改为自己的域名,没域名修改为127.0.0.1:80
        charset utf-8;
        location / {
            root /docker_test/nginx/html; # html访问路径
            index index.html; # html文件名称
            try_files $uri $uri/ /index.html; # 解决单页面应用刷新404问题
        }
    }
     # 新增的server
    server {
        listen 9000;
        server_name  127.0.0.1; # 改为自己的域名,没域名修改为127.0.0.1:80
        charset utf-8;
        location / {
           include uwsgi_params;
           uwsgi_pass 172.18.0.3:9090;  # 端口要和uwsgi里配置的一样
        }
# 新增的配置静态文件
        location /static {
            alias /docker_test/nginx/static;
        }
    }
}

5.3启动

nginx -s reload
nginx 

Nginx多容器部署web项目uwsgi+Django+Redis+Mysql

上一篇:luffy-17/上线


下一篇:python项目_mysql数据库的使用