gunicorn 安裝部署詳解

2021-09-28 18:38:17 字數 1947 閱讀 8568

gunicorn是乙個unix上被廣泛使用的高效能的python wsgi unix http server。

和大多數的web框架相容,並具有實現簡單,輕量級,高效能等特點。

pip install gunicorn

flask程式需要先安裝flask module,pip install flask。

gunicorn_demo.py

from flask import flask

def demo():

return "gunicorn and flask demo."

[2017-12-23 10:36:09 +0000] [24441] [info] starting gunicorn 19.7.1

[2017-12-23 10:36:09 +0000] [24441] [info] listening at: (24441)

[2017-12-23 10:36:09 +0000] [24441] [info] using worker: sync

[2017-12-23 10:36:09 +0000] [24446] [info] booting worker with pid: 24446

測試結果

# curl /demo

gunicorn and flask demo.

gunicorn是乙個wsgi http server,可以如上一章節所示直接起停,提供http服務。

不過在production環境,起停和狀態的監控最好用supervisior之類的監控工具,然後在gunicorn的前端放置乙個http proxy server, 譬如nginx。

下面是supervisor和nginx的配置。

supervisor_gunicorn.conf

[program:gunicorn_demo]

process_name=%(program_name)s

numprocs=1

priority=901

directory = /opt/gunicorn_demo/

autostart = true

startsecs = 20

autorestart = true

startretries = 3

user = root

redirect_stderr = true

stdout_logfile_maxbytes = 20mb

stdout_logfile_backups = 10

stdout_logfile = /dev/null

-c gunicorn_demo.py, 即是gunicorn本身的配置檔案,下面是gunicorn的基本配置,下一章節會詳細說明gunicorn的配置項。

import multiprocessing

bind = '127.0.0.1:8000'

workers = multiprocessing.cpu_count() * 2 + 1

backlog = 2048

worker_class = "gevent"

worker_connections = 1000

daemon = false

debug = true

proc_name = 'gunicorn_demo'

pidfile = './log/gunicorn.pid'

errorlog = './log/gunicorn.log'

nginx.conf 部分配置

server 

}

gunicorn配置項可以通過gunicorn的啟動命令列中設定,也可以通過配置檔案指定。強烈建議使用乙個配置檔案。

配置項如下:

gunicorn 部署 flask 應用

一般比較習慣使用uwsgi進行部署django,flask應用。但,有時受許可權限制,無法安裝uwsgi,所以就使用gunicorn進行部署。之前使用python3的使用,發現 supervisor是不支援。所以就不打算用supervisor了。純粹介紹gunicorn 簡單部署方法,完整流程。環境...

使用gunicorn部署Flask

gunicorn是乙個python wsgi的web服務框架,只支援在unix系統上執行,於ruby的unicorn專案。gunicorn使用prefork master worker模型,能夠與各種wsgi web框架協作。gunicorn安裝非常簡單,使用命令pip install guncor...

gunicorn部署flask服務

greenlet是乙個輕量級的協程庫,gevent是基於greenlet的網路庫。gunicorn是支援wsgi協議的http server,gevent是它支援的模式之一。pip install gunicorn pip install gevent 3.啟動 gunicorn啟動有很多引數,可以...