vue+drf项目-上线

1.Docker

入门到精通

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
"""
1)docker就是Linux上及其轻量的虚拟机

2)虚拟机是存在宿主系统中,通过镜像给宿主系统拓展子系统的(一个镜像理论上可以做n个子系统)

3)两个特别重要的概念
image:镜像
container:容器(子系统)

4)工作方式:
i)在服务器系统下载docker(为服务器安装虚拟机,服务器系统就是宿主系统)
ii)用docker去下载对应需求的镜像:镜像操作
iii)通过镜像制造容器(装子系统):容器操作


5)优势
i)超级轻量级,docker安装的容器体积可以按kB度量
ii)集群部署,docker可以通过一个镜像制造出多个一模一样的容器
iii)便捷管理所有镜像与容器(命令行操作)

6)镜像和容器特点
一般一个镜像中只包含一种软件:比如mysql镜像,创建的容器中只有mysql环境;redis镜像,创建的容器中只有redis环境
注:一个镜像中有多个环境的需要自己制作镜像
"""

CentOS安装Docker

设置管理Docker的仓库

1
2
3
4
5
6
7
"""
1)安装依赖
sudo yum install -y yum-utils device-mapper-persistent-data lvm2

2)安装稳定仓库
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
"""

安装Docker Engine-Community

1
2
3
4
5
6
"""
1)安装
>: yum install docker-ce docker-ce-cli containerd.io

2)如果提示您接受 GPG 密钥,请允许
"""

Docker基础命令

开启关闭

1
2
3
4
5
6
7
8
9
10
"""
1)启动
>: service docker start | systemctl start docker

2)关闭
>: service docker stop | systemctl stop docker

3)重启
>: service docker restart | systemctl restart docker
"""

镜像操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
"""
1)列出镜像
>: docker image ls

2)拉取镜像
>: docker [image] pull 镜像名:版本
eg: docker image pull hello-world:latest

3)删除镜像
>: docker image rm 镜像ID

4)将容器反向打包镜像
>: docker save -o <文件名.tar> <镜像名>

5)根据打包的镜像文件加载镜像
>: docker load -i <文件名.tar>
"""

容器操作

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
"""
1)创建容器
>: docker run [选项参数] 镜像名 [命令]

2)查看所有容器:-a就是--all代表全部展示
>: docker container ls [-a]
>: docker ps [-a]

2)停止一个已经在运行的容器
>: docker [container] stop 容器id

3)启动一个已经停止的容器
>: docker [container] start 容器id

4)kill掉一个已经在运行的容器
>: docker [container] kill 容器id

5)删除容器
>: docker [container] rm 容器id

6)进入容器
>: docker [container] exec 参数 容器id bash
eg: docker exec -it 685e1 bash

7)退出容器
>: exit
"""

Docker安装Mysql

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
"""
1)安装
>: docker image pull mysql:5.7

2)创建容器:root根用户的密码MYSQL_ROOT_PASSWORD采用的是root
>: docker container run -itd -p3306:3306 -e MYSQL_ROOT_PASSWORD=root mysql:5.7

注:本地测试
>: mysql -hIP -P端口 -uroot -p密码

>: mysql -h 39.99.192.127 -P 3306 -u root -p
>: root
>: create database luffy default charset=utf8;
>: grant all privileges on luffy.* to 'luffy'@'%' identified by 'Luffy123?';
>: grant all privileges on luffy.* to 'luffy'@'localhost' identified by 'Luffy123?';
>: flush privileges;
"""

Docker安装Redis

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
"""
1)安装
>: docker pull redis:4.0.9

2)创建容器
>: docker container run -itd -p6379:6379 redis:4.0.9

3)进入容器
>: docker container exec -it <容器名称/容器ID> bash

4)测试
>: redis-cli

>: redis-cli -h 39.99.192.127 -p 6379
>: config set requirepass Admin123
"""

2.上线

2.1基础环境准备

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

1
2
>: yum -y groupinstall "Development tools"
>: yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel psmisc libffi-devel

安装Mysql

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
1)前往用户根目录
>: cd ~

2)下载mysql57
>: wget http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm

也可以本地上传,这条命令要在本地终端上执行
>: scp -r C:\Users\dell\Desktop\pkg\mysql57-community-release-el7-10.noarch.rpm root@39.99.192.127:~

3)安装mysql57
>: yum -y install mysql57-community-release-el7-10.noarch.rpm
>: yum -y install mysql-community-server

4)启动mysql57并查看启动状态
>: systemctl start mysqld.service
>: systemctl status mysqld.service

5)查看默认密码并登录
>: grep "password" /var/log/mysqld.log
>: mysql -uroot -p

6)修改密码
>: ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password';
>: ALTER USER 'root'@'localhost' IDENTIFIED BY 'Owen1234?';

安装Redis

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
1)前往用户根目录
>: cd ~

2)下载redis-5.0.5
>: wget http://download.redis.io/releases/redis-5.0.5.tar.gz
>: scp -r C:\Users\dell\Desktop\pkg\redis-5.0.5.tar.gz root@39.99.192.127:~

3)解压安装包
>: tar -xf redis-5.0.5.tar.gz

4)进入目标文件
>: cd redis-5.0.5

5)编译环境
>: make

6)复制环境到指定路径完成安装
>: cp -r ~/redis-5.0.5 /usr/local/redis

7)配置redis可以后台启动:修改下方内容
>: vim /usr/local/redis/redis.conf

daemonize yes

8)完成配置修改
>: esc
>: :wq

9)建立软连接
>: ln -s /usr/local/redis/src/redis-server /usr/bin/redis-server
>: ln -s /usr/local/redis/src/redis-cli /usr/bin/redis-cli

10)后台运行redis
>: cd /usr/local/redis
>: redis-server ./redis.conf &

ctrl + c

11)测试redis环境
>: redis-cli
ctrl + c

12)关闭redis服务
>: pkill -f redis -9

安装Python3.6

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
1)前往用户根目录
>: cd ~

2)下载 或 上传 Python3.6.7
# 服务器终端
>: wget https://www.python.org/ftp/python/3.6.7/Python-3.6.7.tar.xz

# 本地终端,给服务器上传
>: scp -r 本地Python-3.6.7.tar.xz ssh root@39.99.192.127:服务器路径
>: scp -r C:\Users\dell\Desktop\pkg\Python-3.6.7.tar.xz ssh root@39.99.192.127~

3)解压安装包
>: tar -xf Python-3.6.7.tar.xz

4)进入目标文件
>: cd Python-3.6.7

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

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

7)建立软连接:终端命令 python3,pip3
>: ln -s /usr/local/python3/bin/python3.6 /usr/bin/python3
>: ln -s /usr/local/python3/bin/pip3.6 /usr/bin/pip3

8)删除安装包与文件:
>: rm -rf Python-3.6.7
>: rm -rf Python-3.6.7.tar.xz

配置pip源

1
2
3
4
5
6
7
8
9
10
11
12
1)创建pip配置路径
>: mkdir ~/.pip

2)进入目录编辑配置文件:填入下方内容
cd ~/.pip && vim pip.conf

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

安装uwsgi

1
pip3 install uwsgi

安装虚拟环境

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
1)安装依赖
>: pip3 install virtualenv
>: pip3 install virtualenvwrapper

2)建立虚拟环境软连接
>: ln -s /usr/local/python3/bin/virtualenv /usr/bin/virtualenv

3)配置虚拟环境:填入下方内容
>: vim ~/.bash_profile

VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/python3/bin/virtualenvwrapper.sh

4)退出编辑状态
>: esc

5)保存修改并退出
>: :wq

6)更新配置文件内容
>: source ~/.bash_profile

7)虚拟环境默认根目录:~/.virtualenvs

安装Nginx

1
2
3
4
yum -y install nginx

#启动
nginx

2.2 路飞项目部署:Nginx + uwsgi + django + vue

2.2.1 配置前台项目

打包静态资源上线

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
"""
##settings.js
base_url: 'http://1.116.65.90:8000', // 真实环境:django项目就是跑在8000端口上的

# 本地终端操作
1)本地项目打包,前往luffycity项目目录下
>: cnpm run build

2)本地终端上传
>: scp -r dist root@1.116.65.90:~

# 开始服务器连接,在服务器终端操作
3)移动并重命名
mv ~/dist /www/luffy_web
"""

2.2.2 后台部署

settings配置修改

上线的配置文件配置

1
2
# 内容拷贝dev.py,前身就是settings.py
cp dev.py pro.py

pro.py

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
# 关闭测试环境
DEBUG = False
ALLOWED_HOSTS = [
'1.116.65.90' # 公网ip地址
]

CORS_ORIGIN_ALLOW_ALL = True # 允许所有跨域

# 静态文件配置:上线后还有额外配置,见下方 后台样式问题
STATIC_URL = '/static/'

# 后台http根路径
BASE_URL = 'http://1.116.65.90:8000'
# 前台http根路径
LUFFY_URL = 'http://1.116.65.90:80'
# 订单支付成功的后台异步回调接口
NOTIFY_URL = BASE_URL + '/order/success/'
# 订单支付成功的前台同步回调接口
RETURN_URL = LUFFY_URL + '/order/pay/success/'

REST_FRAMEWORK = {
# 渲染模块
'DEFAULT_RENDERER_CLASSES': [
'rest_framework.renderers.JSONRenderer',
# 'rest_framework.renderers.BrowsableAPIRenderer',
],
}

wsgi.py和manage.py修改

1
2
3
4
5
#拷贝manage.py文件
cp manage.py manage_pro.py

# 需要做上线修改的内容
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'luffyapi.settings.pro')

项目依赖导出

1
2
3
4
5
6
7
8
9
10
11
1)进入本地项目根目录
>: cd 项目根目录

2)本地导出项目环境
>: pip3 freeze > requirements.txt

3)如果环境中有特殊的安装包,需要处理一下xadmin
packages.txt中的
xadmin==2.0.1
要被替换为
https://codeload.github.com/sshwsfc/xadmin/zip/django2

git克隆项目代码

1
2
3
4
5
6
1)创建存放后台项目的目录
>: mkdir /www

2)进入后台项目管理目录同步git仓库中的项目
>: cd /wwws
>: git clone https://gitee.com/gtdong/luffy_api.git

项目虚拟环境配置

1
2
3
4
5
6
7
1)创建线上luffy项目虚拟环境
>: mkvirtualenv luffy
>: workon luffy

2)安装所需环境,在packages.txt所在目录下安装执行packages.txt文件
>: pip install uwsgi
>: pip install -r /www/luffy_api/requirements.txt

后台静态样式处理

修改线上配置

1
2
3
4
5
6
7
1)编辑线上配置文件
>: vim /www/luffy_api/luffy_api/settings/pro.py

2)修改static配置,新增STATIC_ROOT、STATICFILES_DIRS
STATIC_URL = '/static/'
STATIC_ROOT = '/www/luffy_api/luffy_api/static'
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)

迁移静态样式:项目目录下

1
2
3
4
5
1)项目目录下没有 static 文件夹需要新建
>: mkdir /home/project/luffyapi/luffyapi/static

2)完成静态文件迁移
>: python /home/project/luffyapi/manage_pro.py collectstatic

uwsgi配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 1.创建uwsgi服务配置文件
>: vim /www/luffy_api/uwsgi.ini

[uwsgi]
socket = 127.0.0.1:8808 # 内部端口,自定义
chdir = /www/luffy_api # 项目路径
module = luffy_api.wsgi:application # luff_yapi为wsgi.py所在目录名  
master = True # 启用主进程
processes = 4 # 进程数
pidfile = /tmp/uwsgi.pid # 进程pid存放位置
daemonize = /www/luffy_api/logs/uwsgi.log #日志存放位置

# 2.uwsgi启动项目
uwsgi --ini /www/luffy_api/uwsgi.ini

# 3.停止与重启
# 停止
uwsgi --stop /tmp/uwsgi.pid

# 重启
uwsgi --reload /tmp/uwsgi.pid

supervisor管理celery

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
# 1.安装supervisord
pip3 install supervisor

# 2.安装celery
pip3 install celery

# 3.生成默认配置文件
echo_supervisord_conf > /etc/supervisord.conf

# 4.修改配置文件
vim /etc/supervisord.conf

[supervisord]
logfile=/tmp/supervisord.log ; main log file; default $CWD/supervisord.log
logfile_maxbytes=50MB ; max main logfile bytes b4 rotation; default 50MB
logfile_backups=10 ; # of main logfile backups; 0 means none, default 10
loglevel=info ; log level; default info; others: debug,warn,trace
pidfile=/tmp/supervisord.pid ; supervisord pidfile; default supervisord.pid
nodaemon=false ; start in foreground if true; default false
silent=false ; no logs to stdout if true; default false
minfds=1024 ; min. avail startup file descriptors; default 1024
minprocs=200 ; min. avail process descriptors;default 200

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket

;定时任务配置
[program:luffycelerybeat] ;自定义任务名
command= celery -A celery_task beat -l info ;启动任务的命令
numprocs=1 ;进程数配置
directory=/www/luffy_api/ ;项目目录
autostart=true
startsecs=10
startretries=3
stopwaitsecs=600
killasgroup=true
user=root
redirect_stderr=true
stdout_logfile=/www/luffy_api/logs/supervisord-celery-beat.log
stderr_logfile=/www/luffy_api/logs/supervisord-celery-beat-error.log
;日志配置

;worker配置
[program:luffyceleryworker]
command=celery -A celery_task worker -l info
numprocs=1
directory=/www/luffy_api/
autostart=true
startsecs=10
startretries=3
stopwaitsecs=600
killasgroup=true
user=root
redirect_stderr=true
stdout_logfile=/www/luffy_api/logs/supervisord-celery-worker.log
stderr_logfile=/www/luffy_api/logs/supervisord-celery-worker-error.log

[group:luffycelery];任务组配置
programs=luffycelerybeat,luffyceleryworker

# 5.supervisor启动
supervisord -c /etc/supervisord.conf

# 6.supervisorctl相关命令
supervisorctl status # 查看状态
supervisorctl reread # 读取配置信息
supervisorctl update # 加载最新的进程
supervisorctl clear 进程名 # 清空进程日志
supervisorctl stop 进程名 # 停止进程
supervisorctl start 进程名 # 启动进程
supervisorctl reload # 重新加载配置
supervisorctl start|stop|restart all # 表示启动,关闭,重启所有进程。

nginx配置

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
#去向Nginx配置目录,备份配置,完全更新配置:填入下方内容
vim /etc/nginx/conf.d/luffy.conf

server {
listen 80;
server_name 127.0.0.1; # 改为自己的域名,没域名修改为127.0.0.1:80
charset utf-8;
location / {
root /www/luffy_web; # html访问路径
index index.html; # html文件名称
try_files $uri $uri/ /index.html; # 解决单页面应用刷新404问题
}
location /admin/ { # djanngo admin后台配置
proxy_pass http://127.0.0.1:8000/admin/;
proxy_redirect default ;
}
location /static {
alias /www/luffy_api/luffy_api/static;
}
}

server {
listen 8000;
server_name 127.0.0.1; # 改为自己的域名,没域名修改为127.0.0.1:80
charset utf-8;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8808; # 端口要和uwsgi里配置的一样
uwsgi_param UWSGI_SCRIPT luffy_api.wsgi; #wsgi.py所在的目录名+.wsgi
uwsgi_param UWSGI_CHDIR /www/luffy_api/; # 项目路径
}
# 新增的配置静态文件
location /static {
alias /www/luffy_api/luffy_api/static;
}
}

数据库设置

1
2
3
4
5
6
7
8
9
10
11
12
13
1)管理员连接数据库
>: mysql -uroot -p

2)创建数据库
>: create database luffy default charset=utf8;

3)设置权限账号密码:账号密码要与项目中配置的一致
>: grant all privileges on luffy.* to 'luffy'@'%' identified by 'Luffy123?';
>: grant all privileges on luffy.* to 'luffy'@'localhost' identified by 'Luffy123?';
>: flush privileges;

4)退出mysql
>: quit;

完成项目的数据库迁移:Django采用2.0.7

1
2
3
4
5
6
7
# 必须在luffy环境下面
1)数据库迁移
>: cd /www/luffy_api/
>: python manage_prod.py migrate

2)创建超级用户
>: python manage_prod.py createsuperuser

如何Django不是2.0.7的其他版本,数据库迁移保存解决方案:修改源码

1
2
3
4
5
6
7
8
9
10
1)修改base.py源码
>: vim /root/.virtualenvs/luffy/lib/python3.6/site-packages/django/db/backends/mysql/base.py

方案:3637行注释掉

2)修改operations.py源码
>: vim /root/.virtualenvs/luffy/lib/python3.6/site-packages/django/db/backends/mysql/operations.py

方案:146行添加
query = query.encode()

2.2.3 重启服务

1
2
3
4
5
6
1)关闭 uwsgi,重新启动 uwsgi
uwsgi --reload /tmp/uwsgi.pid

2)关闭 nginx,重新启动 nginx
>: nginx -s stop
>: nginx

2.2.4 线上项目测试

1
2
3
4
1)本地浏览器访问xadmin后台
http://1.116.65.90/admin
2)登录,录入测试数据
3)或是导出本地数据库为sql,再在线上导入sql