ubuntu下使用gunicorn部署Flask

安装 supervisor 和 gunicorn

sudo apt-get install supervisor
conda install gunicorn

测试一下: gunicorn wsgi:application

  • wsgi:python文件名(不含后缀)
  • application:Flask实例名称,不一定是 app
  • 在application父目录运行,否则找不到application

示例:

cd /home/username/flaskAPP/ && gunicorn server:app 

创建gunicorn配置文件 /home/username/flaskAPP/gunicorn.conf

# gunicorn.py

# 并行工作进程数
workers = 1
# 指定每个工作者的线程数
threads = 2
# 监听内网端口8000
bind = '127.0.0.1:8000'
# 设置守护进程,将进程交给supervisor管理
daemon = 'false'
# 工作模式,有 sync, eventlet, gevent, tornado, gthread, 缺省值sync
worker_class = 'sync'
# 设置最大并发量
worker_connections = 50
# 设置访问日志和错误信息日志路径
accesslog = '/home/username/flaskAPP/log/gunicorn_acess.log'
errorlog = '/home/username/flaskAPP/log/gunicorn_error.log'
# 设置日志记录水平
# debug, info, warning, error, critical.
loglevel = 'error'
# 在加载应用程序之前切换目录
chdir = '/home/username/flaskAPP/'
# reload:默认为False。此设置用于开发,每当应用程序发生更改时,都会导致工作重新启动。
reload = True

创建目录: /home/username/flaskAPP/log/

用配置文件启动:

gunicorn -c /home/username/flaskAPP/gunicorn.py server:app

用 supervisor 运行 gunicorn, 创建配置文件 sudo vi /etc/supervisor/conf.d/gunicorn.py

[program:gunicorn]
command = /home/username/miniconda3/bin/gunicorn -c /home/username/flaskAPP/gunicorn.py server:app
directory = /home/username
autostart = true
startsecs = 20
autorestart = true
startretries = 3
user = username
environment = STNORESTART="1", HOME="/home/username"

运行:

添加:
sudo supervisorctl update
启动
supervisorctl start gunicorn
重启
supervisorctl restart gunicorn
停止
supervisorctl stop gunicorn

正文完
 
评论(没有评论)