Python使用virtualenv部署flask项目

发布于 2021-09-14  9 次阅读


环境

os: ubuntu20.04
python: python3.6


virtualenv

virtualenv用来建立一个虚拟的python环境,一个专属于项目的python环境。

通过pip安装virtualenv
python3.6 -m pip3 install virtualenv

创建一个独立的Python运行环境
python3.6 -m virtualenv venv

用source进入该环境
source venv/bin/activate

安装第三方包
python3.6 -m pip3 install -r requirements.txt


gunicorn

gunicorn是一个unix上被广泛使用的高性能的python wsgi unix http server

安装gunicorn
python3.6 -m pip3 install gunicorn

gunicorn启动flask
gunicorn -w 4 -b 0.0.0.0:8000 main:app

参数解释:
-w INT,用于处理工作进程的数量,默认为1
-b ADDRESS,ip加端口,绑定运行的主机

以配置文件的方式启动

bind = '0.0.0.0:8000'
user = 'www'
workers = 2
threads = 4
worker_connections = 1000
backlog = 512
daemon = True
chdir = '/'
access_log_format = '%(t)s %(p)s %(h)s "%(r)s" %(s)s %(L)s %(b)s %(f)s" "%(a)s"'
loglevel = 'info'
worker_class = 'geventwebsocket.gunicorn.workers.GeventWebSocketWorker'
errorlog = chdir + '/logs/error.log'
accesslog = chdir + '/logs/access.log'
pidfile = chdir + '/logs/your.pid'

启动gunicorn
gunicorn -c gunicorn.conf main:app