Flask官方有个样例项目flaskr 指南路径 http://flask.palletsprojects.com/en/1.1.x/tutorial/
这里通过systemd+gunicorn(http)+venv+python3.6方式部署。flask项目打包可以参考相关文档
python setup bdist_whell
build/lib/flaskr 这里是项目文件,第二种方式会使用到
dist/flaskr-1.0.0-py3-none-any.whl 这个是whell类型包,可以通过pip安装
备注:gunicorn出了http方式之外,还有unix socket方式(与nginx配合使用),文档 http://docs.gunicorn.org/en/stable/deploy.html
# 安装python3
yum install python36
wget https://bootstrap.pypa.io/get-pip.py
python3 get-pip.py
# 配置python虚拟化环境
cd /opt
python3 -m venv venv
. /opt/venv/bin/activate
(venv) [root@cloudlab ~]# pip install flaskr-1.0.0-py3-none-any.whl #自动安装setup.py配置的依赖
(venv) [root@cloudlab ~]# pip list
Package Version
------------------ -------
atomicwrites 1.3.0
attrs 19.1.0
Click 7.0
Flask 1.1.1
flaskr 1.0.0 # 这个就是flask项目
// 省略 ....
# 初始化数据库
FLASK_APP=flaskr flask init-db
Initialized the database.
/opt/venv/var/flaskr-instance/flaskr.sqlite
# 虚拟化环境里面安装gunicorn
. /opt/venv/bin/activate
(venv) [root@cloudlab ~]# pip install gunicorn
# 编写gunicorn.service
vi /usr/lib/systemd/system/gunicorn.service
[Unit]
Description='demo project'
After=network.target
[Service]
Type=forking
PrivateTmp=true
Restart=on-failure
PIDFile=/run/gunicorn.pid
ExecStart=/opt/venv/bin/gunicorn -D --access-logfile /var/log/gunicorn.log --pid /run/gunicorn.pid flaskr:create_app() -b 0.0.0.0:8080 # 也可以指定配置文件启动
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
启动服务
systemctl daemon-reload
systemctl start gunicorn.service
netstat -pltn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 1421/python3
mv build/lib/flaskr /opt/app/
tree /opt/app/flaskr
/opt/app/flaskr
├── static
│ └── style.css
├── templates
│ ├── auth
│ │ ├── login.html
│ │ └── register.html
│ ├── base.html
│ └── blog
│ ├── create.html
│ ├── index.html
│ └── update.html
├── __init__.py
├── auth.py
├── blog.py
├── db.py
├── schema.sql
└── requirements.txt
. /opt/venv/bin/activate
# 安装项目依赖
(venv) [root@cloudlab ~]# pip install -r requirements.txt
# 安装gunicorn
(venv) [root@cloudlab flaskr]# pip install gunicorn
# 编写gunicorn.service
vi /usr/lib/systemd/system/gunicorn.service
[Unit]
Description='demo project'
After=network.target
[Service]
Type=forking
PrivateTmp=true
Restart=on-failure
PIDFile=/run/gunicorn.pid
WorkingDirectory=/opt/app # 可以看到这里比第一种方式多了一个家目录的设置
ExecStart=/opt/venv/bin/gunicorn -D --access-logfile /var/log/gunicorn.log --pid /run/gunicorn.pid flaskr:create_app() -b 0.0.0.0:8080 # 也可以指定配置文件启动
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
# 数据库初始化
FLASK_APP=/opt/app/flaskr flask init-db
Initialized the database.
ll /opt/app/instance/flaskr.sqlite
-rw-r--r-- 1 root root 5120 Aug 7 19:59 /opt/app/instance/flaskr.sqlite
启动服务
systemctl daemon-reload
systemctl start gunicorn.service
netstat -pltn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 1421/python3