1.下载git
sudo yum update
sudo yum install git
如果是Ubuntu下:
sudo apt-get update
sudo apt-get install git
2. 把项目clone到home文件夹下
cd /home
mkdir bg_flask_one
cd bg_flask_one/
git clone https://gitee.com/sunyonghuixx/zh_backend_flask.git
3.安装python 到 /usr/local/下
安装依赖包:命令可以在任意位置
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make libffi-devel
安装wget
yum install wget
下载python3.8
wget https://www.python.org/ftp/python/3.8.1/Python-3.8.1.tgz
# 解压压缩包
tar -zxvf Python-3.8.1.tgz
# 进入文件夹
cd Python-3.8.1
# 配置安装位置
./configure prefix=/usr/local/python3
# 安装
make && make install
4.添加软连接
因为腾讯云的Centos7.6 默认安装了python3.6 软连接的python3 pip3 被占 这里使用了 python38 和 pip38
#添加python3的软链接
ln -s /usr/local/python3/bin/python3.8 /usr/bin/python38
#添加 pip3 的软链接
ln -s /usr/local/python3/bin/pip3.8 /usr/bin/pip38
5. 创建虚拟环境
下载虚拟环境包
pip38 install virtualenv
# 配置软连接
ln -s /usr/local/python3/bin/virtualenv /usr/bin/virtualenv
# 返回根目录 创建虚拟环境的管理目录
cd /
# 创建虚拟环境的管理目录
mkdir -p /data/env
# 进入管理目录
cd /data/env
# 创建虚拟环境
virtualenv -p /usr/bin/python38 flask_one_env
# 进入虚拟环境
source /data/env/flask_one_env/bin/activate
# 退出虚拟环境
deactivate
6.安装MySQL数据库
在线下载mysql安装包
wget https://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
安装MySQL
rpm -ivh mysql57-community-release-el7-8.noarch.rpm
安装mysql服务
cd /etc/yum.repos.d/
yum -y install mysql-server
此时如果出现 Public key ....is not installed 说明秘钥过期
执行下面命令:
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
再次执行
yum -y install mysql-server
启动MySQL
systemctl start mysqld
获取MySQL临时密码
grep 'temporary password' /var/log/mysqld.log
进入MySQL
mysql -uroot -p 使用临时密码进入 C4iro:f1lj(l
把MySQL的密码校验强度改为低风险
set global validate_password_policy=LOW;
修改MySQL密码
ALTER USER 'root'@'localhost' IDENTIFIED BY 'Srysy123#';
# ALTER USER 'root'@'localhost' IDENTIFIED BY 'Bysjx123#';
关闭Cenots的防火墙
sudo systemctl disable firewalld
设置远程访问:
允许任意ip访问 也可以指定ip
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'Srysy123#' WITH GRANT OPTION;
刷新设置
FLUSH PRIVILEGES;
如果是腾讯云轻量级服务器:
在防火墙上把MySQL(3306)的端口打开
如果是腾讯云服器3306端口在安全组里面设置
创建项目数据库:
create database ys_flask_db charset=utf8mb4;
数据库迁移
# 进入项目目录
cd /home/project/ai-backend
# 进入虚拟环境
source /data/env/flask_one_env/bin/activate
# 下载项目所需的python包
pip install -r requirements.txt
# python包安装好之后
python app.py 运行项目 用于激活 Flask-migrte (这个命令只需要执行一次)
ctrl + c 关闭运行项目
flask --help # 查看 是否有 db命令
如下所示:
(flask_one_env) [root@VM-0-16-centos ai-backend]# flask --help
Usage: flask [OPTIONS] COMMAND [ARGS]...
A general utility script for Flask applications.
An application to load must be given with the '--app' option, 'FLASK_APP'
environment variable, or with a 'wsgi.py' or 'app.py' file in the current
directory.
Options:
-e, --env-file FILE Load environment variables from this file. python-
dotenv must be installed.
-A, --app IMPORT The Flask application or factory function to load, in
the form 'module:name'. Module can be a dotted import
or file path. Name is not required if it is 'app',
'application', 'create_app', or 'make_app', and can be
'name(args)' to pass arguments.
--debug / --no-debug Set debug mode.
--version Show the Flask version.
--help Show this message and exit.
Commands:
db Perform database migrations.
routes Show the routes for the app.
run Run a development server.
shell Run a shell in the app context.
# 如果db命令存在 执行下面操作
falsk db init
flask db migrate
flask db upgrade
7.配置uwsgi
# 全局下载 uwsgi
pip38 install uwsgi
# 全局链接
ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi
在项目目录下创建配置文件
touch uwsgi.ini
touch uwsgi.pid
touch uwsgi.log
touch uwsgi.status
uwsgi.ini 的配置
[uwsgi]
# uwsgi启动时所使用的地址与端口
socket=127.0.0.1:8000
# 必须全部为绝对路径
# 项目的路径 ,pwd指令中显示的路径
chdir=/home/project/ai-backend
# flask的wsgi文件 启动文件
wsgi-file=/home/project/ai-backend/app.py
# python 程序内用以启动的 application 变量名
callable=app
# Python虚拟环境的路径 , 进入到虚拟环境目录里面执行pipenv --venv得到
virtualenv=/data/env/flask_one_env
daemonize=/home/project/ai-backend/uwsgi.log
stats=/home/project/ai-backend/uwsgi.status
pidfile=/home/project/ai-backend/uwsgi.pid
# 进程相关
# 主进程
master=True
# 最大数量的工作进程
processes=10
# 线程数
threads=4
#状态检测地址
# stats = 127.0.0.1:9191
启动
uwsgi --ini uwsgi.ini
# 会出现下面这个命令 真正启动命令在 uwsgi.log
[uWSGI] getting INI configuration from uwsgi.ini
在uwsgi.log出现这个变成功了
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x1d23380 pid: 18049 (default app)
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 18049)
spawned uWSGI worker 1 (pid: 18051, cores: 4)
spawned uWSGI worker 2 (pid: 18055, cores: 4)
spawned uWSGI worker 3 (pid: 18056, cores: 4)
#说明 此时还没有配置nginx 还没能正常访问
# 停止 uwsgi
uwsgi --stop uwsgi.pid
# 重启 uwsgi
uwsgi --reload uwsgi.pid
8.配置nginx
# 查看是否已经下载了nginx
nginx -v
#下载:
安装 EPEL 仓库
Nginx 不是 CentOS 官方软件源的一部分。所以,要安装 Nginx,需要添加 Extra Packages for Enterprise Linux (EPEL) 存储库。使用以下命令安装 EPEL 仓库:
sudo yum install epel-release
安装 Nginx
使用以下命令安装 Nginx:
sudo yum install nginx
# 配置nginx的conf文件
不需要在nginx的初始文件下修改
初始文件会自动加载.d下的所有conf文件
cd /etc/nginx/conf.d/
touch xxx.conf
配置xxx.conf
纯后台的配置 .conf 文件
server {
listen 80; # 监听请求的端口号
server_name 43.134.178.153; # 域名或 IP 地址
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8000; # uWSGI 服务器监听的 IP 和端口号
}
}
前后台一起配置的 .conf文件
server {
listen 80; # 前端端口
server_name 43.134.178.153;
location / {
root /home/project/ai-vue/dist; #前端静态dist文件的路径
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location /api {
proxy_pass http://127.0.0.1:8848/; # 反向代理到后端
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server {
listen 8848; # 监听请求的端口号 这是后台的配置
server_name 43.134.178.153; # 域名或 IP 地址
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8000; # uWSGI 服务器监听的 IP 和端口号
}
}
启动nginx
systemctl status nginx
此时应该就能访问了
如果不能访问
重启 uwsgi 试试
uwsgi --inin uwsgi.ini
# 检查nginx 下的 .conf 文件格式是否正确
sudo nginx -t
# 重启ngnix
sudo systemctl restart nginx
网友评论