Ubuntu18.04 Docker部署Kinship(Django)项目过程

1 Docker的安装

https://blog.csdn.net/weixin_41735055/article/details/100355179

2 下载镜像

docker pull programize/python3.6.8-dlib

下载的镜像里包含python3.6.8和dlib19.17.0

3 启动镜像

docker run -it --name kinship -p 7777:80 -p 3307:3306 -p 5556:5555 programize/python3.6.8-dlib /bin/bash

4 将项目拷贝到/root家目录下(Ctrl+D推出容器,容器外进行)

docker cp Kinship/ kinship:/root

5 安装python依赖库

5.1 修改pip源

在家目录下新建.pip文件夹,文件夹里新建pip.conf文件并加入如下内容:

[global]
index-url=http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com

【注:如果安装vim显示连接不上debian服务器,则修改/etc/apt/sources.list,具体参考我的另一篇博客“Debian修改源”】

5.2 分别安装以下python库

celery==4.3.0
Django==2.2.3
face-recognition==1.2.3
flower==0.9.3
mysqlclient==1.4.2.post1
pandas==0.25.0
redis==3.3.7
uWSGI==2.0.18

6 安装MySQL数据库

6.1 安装mysql-server

apt install mysql-server

6.2 启动mysql

service mysql start

6.3 配置mysql

mysql_secure_installation

【参考:https://www.jianshu.com/p/f32dae444cd3】

6.4 登录mysql,创建kinship数据库

create database kinship charset=utf8;

6.5 配置mysql远程访问

6.5.1 修改配置文件

vim /etc/mysql/mariadb.conf.d/50-server.cnf

注释掉bind-address = 127.0.0.1

6.5.2 授权用户

连接mysql:

grant all on *.* to root@'%' identified by '你的密码' with grant option;
flush privileges;

6.5.3 退出并重启mysql服务

service mysql restart

6.6 提前解决Django数据库迁移django.db.utils.OperationalError: (1698, "Access denied for user 'root'@'localhost'")错误

6.6.1 登录mysql

6.6.2 执行操作

use mysql;
select User,Host,plugin from user;
update user set plugin='mysql_native_password';
flush privileges;

7 安装redis

7.1 安装redis-server

apt install redis-server

7.2 启动redis

redis-server /etc/redis/redis.conf

7.3 测试连接

redis-cli

8 安装nginx

8.1 安装nginx

apt install nginx

8.2 修改nginx配置文件(/etc/nginx/nginx.conf)

将第一行user www-data;改为user root;

8.3 修改nginx配置文件(/etc/nginx/sites-enabled/default)为以下内容

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    server_name _;

    location / {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:8080;
        client_max_body_size 1000M;
    }

    location /static {
        alias /root/Kinship/static;
    }
}

8.4 启动nginx

nginx

8.5 重启nginx

nginx -s reload

9 项目配置

9.1 Django迁移数据库

python manage.py makemigrations
python manage.py migrate

9.2 向kinship数据库的user表中插入如下数据

image.png

10 使用Supervisor管理后台程序启动

10.1 Supervisor安装:

pip install supervisor

10.2 Supervisor配置:

echo_supervisord_conf > supervisord.conf

10.3 supervisord.conf最下面追加内容:

[program:mysql]
directory=/root/Kinship
command=service mysql restart
numprocs=1
autostart=true
autorestart=true
stopsignal=INT

[program:redis]
directory=/root/Kinship
command=redis-server /etc/redis/redis.conf
numprocs=1
autostart=true
autorestart=true
stopsignal=INT

[program:uwsgi]
directory=/root/Kinship
command=uwsgi --ini uwsgi.ini
numprocs=1
autostart=true
autorestart=true
stopsignal=INT

[program:nginx]
directory=/root/Kinship
command=nginx
numprocs=1
autostart=true
autorestart=true
stopsignal=INT

[program:celery] 
directory=/root/Kinship
command=celery -A celery_tasks.tasks worker --loglevel info --logfile celery_worker.log
numprocs=1
autostart=true
autorestart=true
stopsignal=INT

[program:flower] 
directory=/root/Kinship
command=flower -A celery_tasks.tasks --port=5555
numprocs=1
autostart=true
autorestart=true
stopsignal=INT

10.4 Supervisor启动:

supervisord -c supervisord.conf

10.5 Supervisor关闭:

supervisorctl -c supervisord.conf shutdown

10.6 Supervisor重启:

supervisorctl -c supervisord.conf reload

11 退出容器并提交为镜像

你可能感兴趣的:(Ubuntu18.04 Docker部署Kinship(Django)项目过程)