Linux環境上部署Flask

[該文章只涉及我的部署的簡單流程,讀者可經過其它途徑瞭解詳細部署流程]

依我的部署項目可預先安裝好須要的環境,這裏已提早安裝好LNMP環境python

1.安裝Python環境git

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

source virtualenvwrapper.sh

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

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

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

  編寫配置文件app

[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
    quitsocket

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
相關文章
相關標籤/搜索