阿里云+python3.11+flask+mysql+uwsgi简易部署

一、更新系统

yum update

二、安装python3.11

查看python和pip版本

ls -al /usr/bin | grep -E 'python|pip'
lrwxrwxrwx. 1 root root 6 Nov 7 2020 pip2.7 -> ./pip2
-rwxr-xr-x 1 root root 209 Aug 2 16:14 pip3.6
-rwxr-xr-x. 1 root root 21816 Nov 7 2020 python2.7
lrwxrwxrwx. 1 root root 31 Nov 3 2020 python3.6 -> /usr/libexec/platform-python3.6

安装成功(见安装文档)

-rwxr-xr-x 1 root root 30678904 Jan 5 10:15 python3.11
-rwxr-xr-x 1 root root 230 Jan 5 10:25 pip3.11

更新pip(确保使用pip3.11)

pip3 install --upgrade pip

三、git拉代码

安装git

yum install git

初始化git

mkdir -p /www/uns/source
cd /www/uns/source/
git init

让git记住账号密码

.git文件夹下的config文件,添加:

vi .git/config

[credential]
helper = store

拉代码

git remote add origin https://gitee.com/xsourcecc/uns.git
git pull origin master

安装项目依赖的包(确保使用pip3.11)

pip install flask
pip install flask_sqlalchemy
pip install flask_migrate
pip install wtforms
pip install pymysql
pip install cryptography
pip install flask_restful
pip install flask_marshmallow
pip install marshmallow-sqlalchemy
pip install marshmallow
pip install pyOpenSSL
pip install requests

2023.3.25

安装flask_sqlalchemy时,会自动安装SQLAlchemy==2.0,但是我最初开发时,是基于1.4的,所以要重新安装旧的SQLAlchemy。有空再研究怎么升级到2.0。

pip uninstall SQLAlchemy
pip install SQLAlchemy==1.4.46

四、安装mysql并创建数据库

安装文档 by 阿里云

创建mysql账号

密码不要带@,否则sqlalchemy报错

create user 'admin'@'%' identified by '密码';
grant all privileges on *.* to 'admin'@'%' with grant option;
flush privileges;
CREATE DATABASE `uns` CHARACTER SET 'utf8mb4'; --这一步最好用客户端来创建

用flask建库

先把migrations文件夹删了(如果有)

flask db init
flask db migrate
flask db upgrade

然后,导入数据库初始静态数据

五、安装uwsgi服务器

pip install uwsgi

创建配置文件

cd /source/crm
vi uwsgi.ini
[uwsgi]
http=:5000
chdir=/source/crm
wsgi-file=/source/crm/app.py
pidfile=/source/crm/uwsgi.pid
daemonize=/source/crm/uwsgi.log
callable=app
processes=4
master=true
threads=2

简易使用

前台启动(配置pidfile后就会自动变成后台启动)

uwsgi --ini uwsgi.ini

后台启动

nohup uwsgi --ini uwsgi.ini

关闭

uwsgi --stop uwsgi.pid

关闭防火墙(实际不需要关)

systemctl stop firewalld

你可能感兴趣的:(python,python,flask)