Python web 部署:Flask+Nginx+gunicorn+supervisor

Falsk的建立這里再敘述,主要說(shuō)的是部署方面的內(nèi)容。

使用gunicorn部署Flask程序

安裝gunicorn

pip install gunicorn
使用gunicorn啟動(dòng)flask
gunicorn -w4 -b0.0.0.0:8080 web:app
注:web為主程序的名字
此時(shí),我們需要用 8080 的端口進(jìn)行訪問(wèn),原先的5000并沒(méi)有啟用。其中 gunicorn 的部署中,,-w 表示開(kāi)啟多少個(gè) worker,-b 表示 gunicorn 開(kāi)發(fā)的訪問(wèn)地址。
結(jié)束gunicorn 需要執(zhí)行 pkill gunicorn,我們還可以使用一個(gè)進(jìn)程管理工具來(lái)管理系統(tǒng)的進(jìn)程----supervisor

安裝supervisor

pip install supervisor
echo_supervisord_conf > supervisor.conf # 生成 supervisor 默認(rèn)配置文件
vim supervisor.conf # 修改 supervisor 配置文件,添加 gunicorn 進(jìn)程管理

在 supervisor.conf配置晚間底部添加

[program:web]
command=/home/zxjd/Web/venv/bin/gunicorn -w4 -b0.0.0.0:8080 web:app ; supervisor啟動(dòng)命令
directory=/home/zxjd/Web/                       ; 項(xiàng)目的文件夾路徑
startsecs=0                                     ; 啟動(dòng)時(shí)間
stopwaitsecs=0                                  ; 終止等待時(shí)間
autostart=false                                 ; 是否自動(dòng)啟動(dòng)
autorestart=false                               ; 是否自動(dòng)重啟
stdout_logfile=/home/zxjd/Web/log/gunicorn.log  ; log 日志
stderr_logfile=/home/zxjd/Web/log/gunicorn.err  ; 錯(cuò)誤日志

supervisor的基本使用命令

supervisord -c supervisor.conf 通過(guò)配置文件啟動(dòng)
supervisorsupervisorctl -c supervisor.conf status 察看supervisor的狀態(tài)
supervisorctl -c supervisor.conf reload 重新載入 配置文件
supervisorctl -c supervisor.conf start [all]|[appname] 啟動(dòng)指定/所有 supervisor管理的程序進(jìn)程
supervisorctl -c supervisor.conf stop [all]|[appname] 關(guān)閉指定/所有 supervisor管理的程序進(jìn)程

supervisor還可以使用web的管理界面,相關(guān)配置請(qǐng)看其他教程
使用supervisor啟動(dòng)gunicorn。
supervisord -c supervisor.conf

安裝配置Nginx

sudo apt-get install nginx
可以采用supervisor管理Nginx進(jìn)程
supervisor的配置如下:

[program:nginx]
command=/usr/sbin/nginx
startsecs=0
stopwaitsecs=0
autostart=false
autorestart=false
stdout_logfile=/home/zxjd/Web/log/nginx.log
stderr_logfile=/home/zxjd/Web/log/nginx.err

Nginx的配置:
配置文件為/etc/nginx/sites-avaliable/default

server {
    listen 80;
    server_name example.org; # 這是HOST機(jī)器的外部域名,用地址也行

    location / {
        proxy_pass http://127.0.0.1:8080; # 這里是指向 gunicorn host 的服務(wù)地址
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

  }

啟動(dòng)服務(wù)
supervisor -c supervisor.conf start all
根據(jù)主機(jī)的ip地址和8080端口其他客戶端就可以訪問(wèn)了。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容