centos部署flask項目

安裝python3

1、查看當前python版本

[root@localhost bin]# cd /usr/bin
[root@localhost bin]# ls python*
python  python2  python2.7 
[root@localhost bin]# 
查看依賴關係
[root@localhost bin]# ls -al  python*
lrwxrwxrwx. 1 root root   33 Oct 21 12:30 python -> python2
lrwxrwxrwx. 1 root root    9 Oct 19 23:55 python2 -> python2.7
-rwxr-xr-x. 1 root root 7136 Aug  4 08:40 python2.7

# 備份一下老的
[root@localhost bin]# mv python python.bak

python官網ftp下載網址: https://www.python.org/ftp/python/

# 創建安裝目錄,一般在 /usr/local/
[root@localhost bin]# mkdir /usr/local/python3
[root@localhost bin]# cd /usr/local/python3
[root@localhost python3]# ll
total 0
[root@localhost python3]# 

# 下載安裝包
[root@localhost python3]# wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz
# 與網速有關,有安裝包,跳過
# 解壓安裝包
[root@localhost python3]# tar -xvf Python-3.6.3.tgz 
#解壓完成後,查看目錄下文件
[root@localhost python3]# ll
total 22148
drwxr-xr-x. 17  501  501     4096 Oct 21 12:22 Python-3.6.3
-rw-r--r--.  1 root root 22673115 Oct  3 15:47 Python-3.6.3.tgz

# 下載的包是未編譯的,需要編譯
[root@localhost python3]# cd Python-3.6.3/
[root@localhost Python-3.6.3]# ./configure --prefix=/usr/local/python3Dir
[root@localhost Python-3.6.3]# make && make install

# 創建一個軟鏈接 將python 加入/usr/bin
[root@localhost bin]# ln -s /usr/local/python3Dir/bin/python3 /usr/bin/python

# 軟鏈接創建完畢之後。再說個事情,就是centos的yum命令是需要python支持的
[root@localhost bin]# vi /usr/bin/yum

#!/usr/bin/python改成#!/usr/bin/python2.7

# 檢查是否安裝成功
python -V
# 爲pip創建軟連接
[root@VM_0_8_centos bin]# ln -s /usr/local/python3Dir/bin/pip3 /usr/bin/pip

# 升級pip
python -m pip install --upgrade pip

安裝問題記錄

# 第一次在騰訊雲的服務器上安裝,未曾出現問題,但是在虛擬機安裝出現問題,做以下記錄
# 1、zipimport.ZipImportError: can‘t decompress data; zlib not availabl
	解決方案:
	yum install zlib*
	如果出現個問題,則之前安裝的步驟要全部撤銷,否則  yum 會報錯,yum基於python,會有影響,2和3不兼容
	
	在重新編譯之前還需要在安裝源文件中修改Modules/Setup.dist文件,
	
	#zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz
	將這一行註釋打開
	zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz

#  2、升級pip的時候報錯:
	pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Could not fetch URL https://pypi.python.org/simple/pip/: There was a problem confirming the ssl certificate: Can't connect to HTTPS URL because the SSL module is not available. - skipping
Requirement already up-to-date: pip in /usr/local/python3Dir/lib/python3.6/site-packages

  出現這個原因的問題:由於openssl版本過低或者不存在
  yum install -y openssl-devel
  
  然後撤銷之前操作,在編譯的時候加上
  
  ./configure --with-ssl
	

gunicorn啓動Flask項目

命令行啓動

# 安裝virtualenv
pip install virtualenv   # 不在虛擬環境中運行 gunicorn 會報錯,提示找不到 gunicorn
# 創建虛擬環境
[root@VM_0_8_centos flask_app]# mkdir ENV && cd ENV
[root@VM_0_8_centos ENV]# virtualenv -p /usr/bin/python python
                                          python環境     虛擬環境名稱
# 激活環境
[root@VM_0_8_centos ENV]# source python/bin/activate
(python) [root@VM_0_8_centos ENV]# 

# 安裝第三方包
 pip install flask flask-script gunicorn

# 目錄結構
flask_app/
├── ENV
├── main.py
├── manage.py
# 錯誤記錄
# 如果出現:
-bash: virtualenv: 未找到命令 

1、virtualenv --python=/usr/bin/python python
2、環境變量問題:
使用查找find / -name 'virtualenv.py' 是否存在
添加環境變量
vim /etc/profile
# 將下面內容添加到文件的最下面
PATH=$PATH:/usr/local/python3Dir/bin

#是添加的進行生效命令
source /etc/profile

# 最後查看是否添加成功
echo $PATH

main.py

from flask import Flask

app = Flask(__name__)

@app.route('/')
def Hello():
    return "hello world!"

manage.py

from flask_script import Manager

from main import app

manage = Manager(app)

if __name__ == "__main__":
    manage.run()

使用gunicorn啓動flask項目

(python) [root@VM_0_8_centos flask_app]# gunicorn -w 4 -b 127.0.0.1:8080 manage:app
[2020-05-27 11:30:10 +0800] [18437] [INFO] Starting gunicorn 20.0.4
[2020-05-27 11:30:10 +0800] [18437] [INFO] Listening at: http://127.0.0.1:8080 (18437)
[2020-05-27 11:30:10 +0800] [18437] [INFO] Using worker: sync
[2020-05-27 11:30:10 +0800] [18450] [INFO] Booting worker with pid: 18450
[2020-05-27 11:30:10 +0800] [18454] [INFO] Booting worker with pid: 18454
[2020-05-27 11:30:10 +0800] [18465] [INFO] Booting worker with pid: 18465
[2020-05-27 11:30:10 +0800] [18468] [INFO] Booting worker with pid: 18468

檢驗:

[root@VM_0_8_centos ~]# curl http://127.0.0.1:8080/
hello world![root@VM_0_8_centos ~]# 
# 看到hello world 及正常工作

更好的啓動方式

# 編寫配置文件 gunicorn.py 或 (gunicorn.conf)



      1 # 並行工作線程數
      2 workers = 4
      3 # 監聽內網端口8080
      4 bind = "0.0.0.0:8080"
      5 # 設置守護進程【關閉連接時,程序人在運行】
      6 daemon = True
      7 # 設置超時時間
      8 timeout = 30
      9 # 設置訪問日誌和錯誤信息日誌路徑
     10 accesslog = "./logs/acess.log"
     11 errorlog = "./logs/error.log"

# 啓動方式
gunicron manage:app -c gunicron

錯誤總結:

  1. gunicorn.conf 沒有啓動成功,出現!!!Warning:WARNING: configuration file…!!!,將文件名修改爲gunicorn.py
  2. 開啓日誌記錄後,,要手動創建logs文件夾

Supervisor(4.2.0文檔)

簡介

Supervisor is a client/server system that allows its users to control a number of processes on UNIX-like operating systems.

Supervisor 是一個客戶機/服務器系統,允許用戶控制類unix操作系統的進程數量。

Supervisor has been tested and is known to run on Linux (Ubuntu 9.10), Mac OS X (10.4/10.5/10.6), and Solaris (10 for Intel) and FreeBSD 6.1. It will likely work fine on most UNIX systems.

Supervisor will not run at all under any version of Windows.

Supervisor is intended to work on Python 3 version 3.4 or later and on Python 2 version 2.7.

Supervisor 已經測試,是在Linux上運行Ubuntu 9.10, Mac OS X(10.4 / 10.5/10.6),和Solaris英特爾(10)和FreeBSD 6.1。它可能會在大多數UNIX系統正常工作。

Supervisor 將不運行任何版本的Windows

Supervisor 是爲了工作在Python 3版本3.4或更高版本和Python 2.7版本2。

安裝

  1. Installing to A System With Internet Access

    Supervisor can be installed with pip install:

    pip install supervisor
    
  2. supervisor需要重寫配置文件

    # 在目錄下創建conf文件夾,用來存放supervisor配置文件
    (python) [root@VM_0_8_centos test_app]# mkdir conf && echo_supervisord_conf > /usr/wc/test_app/conf/supervisord.conf
    (python) [root@VM_0_8_centos test_app]# ls ./conf/
    supervisord.conf
    
  3. supervisor.conf文件說明

    [unix_http_server]
    file=/tmp/supervisor.sock   ; UNIX socket 文件,supervisorctl 會使用
    ;chmod=0700                 ; socket 文件的 mode,默認是 0700
    ;chown=nobody:nogroup       ; socket 文件的 owner,格式: uid:gid
    
    ;[inet_http_server]         ; HTTP 服務器,提供 web 管理界面
    ;port=127.0.0.1:9001        ; Web 管理後臺運行的 IP 和端口,如果開放到公網,需要注意安全性
    ;username=user              ; 登錄管理後臺的用戶名
    ;password=123               ; 登錄管理後臺的密碼
    
    
    ;[inet_http_server]         ; inet (TCP) server disabled by default
    ;port=127.0.0.1:9001        ; ip_address:port specifier, *:port for all iface
    ;username=user              ; default is no username (open server)
    ;password=123               ; default is no password (open server)
    
    
    [supervisord]
    logfile=/tmp/supervisord.log ; 日誌文件,默認是 $CWD/supervisord.log
    logfile_maxbytes=50MB        ; 日誌文件大小,超出會 rotate,默認 50MB
    logfile_backups=10           ; 日誌文件保留備份數量默認 10
    loglevel=info                ; 日誌級別,默認 info,其它: debug,warn,trace
    pidfile=/tmp/supervisord.pid ; pid 文件
    nodaemon=false               ; 是否在前臺啓動,默認是 false,即以 daemon 的方式啓動
    minfds=1024                  ; 可以打開的文件描述符的最小值,默認 1024
    minprocs=200                 ; 可以打開的進程數的最小值,默認 200
    
    [rpcinterface:supervisor]
    supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
    
    [supervisorctl]
    serverurl=unix:///tmp/supervisor.sock ; 通過 UNIX socket 連接 supervisord,路徑與 unix_http_server 部分的 file 一致
    ;serverurl=http://127.0.0.1:9001 ; 通過 HTTP 的方式連接 supervisord
    
    ; 包含其他的配置文件
    [include]
    files = relative/directory/*.ini    ; 可以是 *.conf 或 *.ini
    

啓動

(python) [root@VM_0_8_centos test_app]# supervisord -c ./conf/supervisord.conf 
Unlinking stale socket /tmp/supervisor.sock    # 報錯

# 解決方案:

(python) [root@VM_0_8_centos test_app]# find / -name "supervisor.sock"
/tmp/supervisor.sock
(python) [root@VM_0_8_centos test_app]# unlink /tmp/supervisor.sock 
(python) [root@VM_0_8_centos test_app]# 

# 重新執行命令
(python) [root@VM_0_8_centos test_app]# supervisord -c ./conf/supervisord.conf 
(python) [root@VM_0_8_centos test_app]# 
# 沒有報錯,說明啓動正常

# 查看進程
(python) [root@VM_0_8_centos test_app]# ps aux | grep supervisord
root     19212  0.0  0.8 212352 16276 ?        Ss   17:28   0:00 /usr/wc/flask_app/ENV/python/bin/python /usr/wc/flask_app/ENV/python/bin/supervisord -c ./conf/supervisord.conf
root     25480  0.0  0.8 212352 16276 ?        Ss   17:31   0:00 /usr/wc/flask_app/ENV/python/bin/python /usr/wc/flask_app/ENV/python/bin/supervisord -c ./conf/supervisord.conf
root     27881  0.0  0.0 112724   996 pts/2    R+   17:32   0:00 grep --color=auto supervisord
(python) [root@VM_0_8_centos test_app]# 
# http://localhost:9001 refused connection   報錯這個,將配置文件中的這四個打開,去掉分號
# 修改前
;[inet_http_server]         ; inet (TCP) server disabled by default
;port=127.0.0.1:9001        ; ip_address:port specifier, *:port for all iface
;username=user              ; default is no username (open server)
;password=123               ; default is no password (open server)

# 修改後
[inet_http_server]         ; inet (TCP) server disabled by default
port=127.0.0.1:9001        ; ip_address:port specifier, *:port for all iface
username=user              ; default is no username (open server)
password=123               ; default is no password (open server)

添加管理項目

首先在conf目錄下創建 test_app.conf文件

flask+gunicorn啓動

編寫test_app.conf文件

[program:test_app]
directory = /usr/wc/test_app ; 程序的啓動目錄
command = gunicorn manage:app -c gunicorn.py ; 啓動命令
autostart = true     ; 在 supervisord 啓動的時候也自動啓動
startsecs = 5        ; 啓動 5 秒後沒有異常退出,就當作已經正常啓動了
autorestart = true   ; 程序異常退出後自動重啓
startretries = 3     ; 啓動失敗自動重試次數,默認是 3
user = root          ; 用哪個用戶啓動
redirect_stderr = true  ; 把 stderr 重定向到 stdout,默認 false
stdout_logfile_maxbytes = 20MB  ; stdout 日誌文件大小,默認 50MB
stdout_logfile_backups = 20     ; stdout 日誌文件備份數
; stdout 日誌文件,需要注意當指定目錄不存在時無法正常啓動,所以需要手動創建目錄(supervisord 會自動創建日誌文件)
stdout_logfile = /usr/wc/test_app/logs/usercenter_stdout.log

編寫完成以後,修改supervisor.conf 中的 [include]

[include]
files = /usr/wc/test_app/conf/*.conf

保存以後,重載supervisor才能加載,新配置

(python) [root@VM_0_8_centos conf]# supervisorctl reload
Restarted supervisord
(python) [root@VM_0_8_centos conf]# supervisorctl status
test_app                         RUNNING   pid 19259, uptime 0:00:14

訪問開啓的flask項目

瀏覽器輸入: http://服務器ip:8080/ ------> hello world!

補充

supervisorctl的用法

supervisord : 啓動supervisor
supervisorctl reload :修改完配置文件後重新啓動supervisor
supervisorctl status :查看supervisor監管的進程狀態
supervisorctl start 進程名 :啓動XXX進程
supervisorctl stop 進程名 :停止XXX進程
supervisorctl stop all:停止全部進程,注:start、restart、stop都不會載入最新的配置文件。
supervisorctl update:根據最新的配置文件,啓動新配置或有改動的進程,配置沒有改動的進程不會受影響而重啓

設置開機啓動

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章