diff --git a/项目部署.md b/项目部署.md new file mode 100644 index 0000000..1d1d431 --- /dev/null +++ b/项目部署.md @@ -0,0 +1,275 @@ +# 项目部署 + +## 初始化工作 + +### 部署目录 + +根目录:/opt + +项目目录:/opt/project + +项目启动日志目录:/opt/logs + +## 生产环境要求 + +服务器系统版本:Centos 7.2以上 + +| 软件名称 | 版本 | +| ---------- | ------------ | +| Python | >= 3.8 | +| Mysql | >= 8.0 | +| Nginx | >= 1.16 | +| Git | 最新,非必须 | +| Supervisor | >= 3.4.0 | + +## 项目配置 + +### 接口项目配置 + +**项目主要配置信息:** + +文件路径:kinit-api/application/settings.py + +```python +# 关闭代码调试 +DEBUG = False +``` + +**导出项目接口依赖包:** + +kinit-api 根目录下执行命令: + +``` +pip freeze > requirements.txt +``` + +### 前端项目打包 + +kinit-admin 打包项目根目录下执行命令: + +``` +pnpm run build:pro +``` + +### 软件安装配置 + +#### Mysql + +安装配置请查看链接:https://www.cnblogs.com/yanglang/p/10782941.html + +``` +scp mysql-8.0.27-1.el7.x86_64.rpm-bundle.tar root@127.0.0.1:/usr/local/pag + +// 安装命令: +tar -xvf mysql-8.0.27-1.el7.x86_64.rpm-bundle.tar +rpm -ivh mysql-community-common-8.0.27-1.el7.x86_64.rpm --nodeps --force +rpm -ivh mysql-community-libs-8.0.27-1.el7.x86_64.rpm --nodeps --force +rpm -ivh mysql-community-client-8.0.27-1.el7.x86_64.rpm --nodeps --force +rpm -ivh mysql-community-server-8.0.27-1.el7.x86_64.rpm --nodeps --force +rpm -qa | grep mysql + +rpm -e mysql-community-common --nodeps +rpm -e mysql-community-libs --nodeps +rpm -e mysql-community-client --nodeps +rpm -e mysql-community-server --nodeps + +mysqld --initialize +chown mysql:mysql /var/lib/mysql -R +systemctl start mysqld.service +systemctl enable mysqld +cat /var/log/mysqld.log | grep password +mysql -uroot -p +ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'b0Nrzh97G9lJkQMK'; +create user 'root'@'%' identified with mysql_native_password by 'b0Nrzh97G9lJkQMK'; +grant all privileges on *.* to 'root'@'%' with grant option; +flush privileges; +``` + +安装配置完成后建议使用Navicat Premium 作为 Mysql 的客户端 + +1. 首先需要创建 kinit 系统数据库 + +2. 然后将 kinit-api\static\kinit.sql 文件导入 + +#### Python + +```shell +# 下载python安装包 +wget https://www.python.org/ftp/python/3.8.2/Python-3.8.2.tgz +# 创建安装目录 +mkdir /usr/local/python3 +# 解压安装包 +tar -zxvf Python-3.8.2.tgz +# 进入解压目录 +cd Python-3.8.2 +# 配置 +./configure --prefix=/usr/local/python3 +#编译 +make && make install +#创建软链接 +ln -s /usr/local/python3/bin/python3 /usr/bin/python3 +ln -s /usr/local/python3/bin/python3 /usr/local/bin/python3 +#pip创建软链接 +ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3 +# 更新pip +pip3 install --upgrade pip +``` + +#### Git + +安装: + +``` +yum -y install git +``` + +获取项目文件: + +``` +mkdir -p /opt/project/ +git clone -b release 项目Git仓库 +``` + +或者也可以通过压缩包上传的方式上传到服务器,推荐使用 Git 方式 + +#### Python 虚拟环境 + +创建项目所使用的 python 虚拟环境: + +```shell +# 安装 virtualenv +pip3 install virtualenv + +# 找到虚拟环境的安装目录 +find / -name virtualenv +# 创建软链接 +ln -s /usr/local/python3/bin/virtualenv /usr/bin/ + +# 进入虚拟环境目录 +mkdir -p /opt/env +cd /opt/env + +# 新建虚拟环境,会在当前目录生成一个文件夹 +virtualenv kinit + +# 进入虚拟环境(命令行前面会出现虚拟环境名称) +cd kinit +source bin/activate + +# 退出虚拟环境命令(之后的设置都是在虚拟环境中设置,这里不需要退出) +deactivate +``` + +安装接口项目依赖包: + +```shell +# 在虚拟环境中执行 +# 进入到 saas-api 根目录下执行 +pip3 install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/ +``` + +#### Supervisor + +官方文档:http://supervisord.org/ + +安装配置请查看:https://www.jianshu.com/p/0b9054b33db3 + +创建项目接口 supervisor 配置文件: + +```shell +vim /etc/supervisord.d/kinit.api.ini +``` + +内容如下: + +```ini +[program:kinit.api] +directory=/opt/project/kinit/kinit-api/ +command=/opt/env/kinit/bin/gunicorn main:app -w 2 -k uvicorn.workers.UvicornWorker -b 0.0.0.0:9000 +startsecs=10 +stopwaitsecs=10 +autostart=true +autorestart=true +redirect_stderr=true +stdout_logfile_maxbytes=10MB +stdout_logfile_backups = 3 +stdout_logfile=/opt/logs/kinit.api.log +``` + +更新 supervisor 配置: + +``` +supervisorctl update +``` + +更新后会自动启动进程 + +可以通过命令查看: + +``` +supervisorctl status +``` + +#### Nginx + +安装: + +``` +yum -y install nginx +``` + +增加配置文件 + +``` +vim /etc/nginx/conf.d/kinit.conf +``` + +内容如下: + +```nginx +server +{ + listen 80; + server_name 127.0.0.1; + root /opt/project/kinit/kinit-admin/dist/; + + keepalive_timeout 180s; + client_max_body_size 20m; + + location ~ \.well-known{ + allow all; + } + location / { + try_files $uri $uri/ @router; + root /opt/project/kinit/kinit-admin/dist/; + index index.html; + } + location /api/ { + proxy_pass http://127.0.0.1:9000/; + client_max_body_size 20m; + } + + location @router { + rewrite ^.*$ /index.html break; + } + + access_log /opt/logs/kinit.log; + error_log /opt/logs/kinit.log; +} + +``` + +配置完成后启动: + +``` +systemctl start nginx +# 开启自启动 +systemctl enable nginx +``` + +查看当前状态: + +``` +systemctl status nginx +``` +