Linux環境上部署Flask


[該文章只涉及個人部署的簡單流程,讀者可通過其它途徑了解詳細部署流程]

依個人部署項目可預先安裝好需要的環境,這里已提前安裝好LNMP環境

1.安裝Python環境

  安裝virtualenv環境
  配置環境變量
  配置虛擬環境保存的路徑,執行sh文件生成 mkvirtualenv 等命令

source virtualenvwrapper.sh

2.配置Git
  yum install git
  將遠程倉庫拉去到本地

3.安裝項目依賴包
  pip install -i https://pypi.douban.com/simple/ -r requirements.txt

4.修改項目配置文件
  config 數據庫連接配置、默認路徑等
  數據庫遷移,生成數據庫遷移腳本,實現模型 <===> 數據庫表 之間的映射

  嘗試啟動服務

5.安裝和使用uwsgi 對應的應用服務:gunicorn
  是一個應用服務器,非靜態文件的網絡請求必須通過該應用完成,當然也可以充當靜態文件服務器(不推薦使用,而推薦使用Nginx)
  uwsgi是用Python編寫,因此可通過 pip install uwsgi 安裝,且必須安裝在系統級別的Python的環境中

  *****通過 uwsgi 部署項目時 請求流程
  Client <===> Nginx <===> uwsgi <===> Django/Flask等服務

  嘗試啟動:
    uwsgi --http :8080 --module bbs.wsgi --virtualenv=/applications/python/env/bbs-env

  編寫配置文件

[uwsgi]
# 服務器上是通過uwsgi來啟動項目,也就是說啟動了uwsgi,也就啟動了項目
socket=127.0.0.1:8001
# 項目目錄
chdir=/applications/python/bbs

# Python 虛擬環境的路徑
home=/applications/python/env/bbs-env

# flask程序的啟動文件,通常在本地是通過運行 python manage.py runserver 來啟動項目的
wsgi-file=/applications/python/bbs/server.py
# 程序內啟用的application變量名
callable=app

http = :9001

# 啟動uwsgi的用戶名和用戶組
uid=root
gid=root

# 設置socket權限
chmod-socket=666

# 啟用主進程
master=true
# 自動移除unix Socket和pid文件當服務停止的時候
vacuum=true

6.安裝和使用Nginx
  作用:
    動靜分離 (靜態資源:js/cs/圖片等文件)
    反向代理
    負載均衡
  安裝以及配置好環境變量,並這是Nginx開機自啟動

  配置Nginx配置文件並且啟動

7.supervisor配置
  管理uwsgi,可在uwsgi發生意外時自動重啟應用服務
  安裝,且必須安裝在系統級別的Python的環境中 pip3 install git+https://github.com/Supervisor/supervisor
  創建配置文件 bbssupervisor.conf

[program:bbs]
command = uwsgi --ini /applications/conf/uwsgi/flask.bbs.ini

directory = /applications/python/bbs

startsecs = 0
stopwaitsecs = 0
autostart = true
autorestart = true

stdout_logfile = /applications/python/bbs/bbssupervisor.log
stderr_logfile = /applications/python/bbs/bbssupervisor.err

[supervisord]
loglevel = info

[inet_http_server]
port = :9001
username = admin
password = admin123

# 配置通過 supervisorctl 管理的配置項
[supervisorctl]
serverurl = http://127.0.0.1:9001
username = admin
password = admin123

# 必須指定的 查看官方文檔
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

  通過 supervisord 啟動 uwsgi
    supervisord -c bbssupervisor.conf

  通過以上啟動了supervisor之后,可使用supervisorctl 管理 supervisor
    supervisorctl -c bbssupervisor.conf
  管理台常用命令:
    status
    start pargram_name
    restart pargram_name
    reload
    quit

001 簡單壓力測試:    ab Apache旗下的壓力測試工具
  yum install httpd-tools -y

[root@AL~]# ab -n 100000 -c 100 url
一次一百個請求
ab -n 1000 -c 100 http://47.101.180.183/

002 安裝 supervisor 出錯:        

[root@AL bbs]# pip3 install supervisor
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Collecting supervisor
Downloading http://mirrors.aliyun.com/pypi/packages/ba/65/92575a8757ed576beaee59251f64a3287bde82bdc03964b89df9e1d29e1b/supervisor-3.3.5.tar.gz (421kB)
100% |████████████████████████████████| 430kB 19.7MB/s 
Complete output from command python setup.py egg_info:
Supervisor requires Python 2.4 or later but does not work on any version of Python 3. You are using version 3.6.5 (default, Mar 16 2019, 12:35:52)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]. Please install using a supported version.

----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-o5f3s_ep/supervisor/    

解決:
  pip3 install git+https://github.com/Supervisor/supervisor

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM