flask項(xiàng)目開發(fā)到部署配置

flask項(xiàng)目開發(fā)到部署配置

啟動(dòng)

  • python manager runserver --threaded

flask-sqlacodegen生成model數(shù)據(jù)層

flask-sqlacodegen mysql+pymysql://root:root@127.0.0.1/tiantianlian --outfile "web/models/model.py"
flask-sqlacodegen mysql+pymysql://root:root@127.0.0.1/tiantianlian --outfile "web/models/AppSet.py" --tables app_sets --flask
flask-sqlacodegen mysql+pymysql://root:root@127.0.0.1/tiantianlian --outfile "web/models/Campus.py" --tables campus --flask
flask-sqlacodegen mysql+pymysql://root:root@127.0.0.1/tiantianlian --outfile "web/models/Classes.py" --tables classes --flask
flask-sqlacodegen mysql+pymysql://root:root@127.0.0.1/tiantianlian --outfile "web/models/Feedback.py" --tables feedback --flask
flask-sqlacodegen mysql+pymysql://root:root@127.0.0.1/tiantianlian --outfile "web/models/MemberRole.py" --tables member_role --flask
flask-sqlacodegen mysql+pymysql://root:root@127.0.0.1/tiantianlian --outfile "web/models/Member.py" --tables members --flask
flask-sqlacodegen mysql+pymysql://root:root@127.0.0.1/tiantianlian --outfile "web/models/PaperQuestion.py" --tables paper_question --flask
flask-sqlacodegen mysql+pymysql://root:root@127.0.0.1/tiantianlian --outfile "web/models/Paper.py" --tables papers --flask
flask-sqlacodegen mysql+pymysql://root:root@127.0.0.1/tiantianlian --outfile "web/models/QuestionLog.py" --tables question_log --flask
flask-sqlacodegen mysql+pymysql://root:root@127.0.0.1/tiantianlian --outfile "web/models/Question.py" --tables questions --flask
flask-sqlacodegen mysql+pymysql://root:root@127.0.0.1/tiantianlian --outfile "web/models/RoleRule.py" --tables role_rule --flask
flask-sqlacodegen mysql+pymysql://root:root@127.0.0.1/tiantianlian --outfile "web/models/Roles.py" --tables roles --flask
flask-sqlacodegen mysql+pymysql://root:root@127.0.0.1/tiantianlian --outfile "web/models/Rules.py" --tables rules --flask
flask-sqlacodegen mysql+pymysql://root:root@127.0.0.1/tiantianlian --outfile "web/models/Tag.py" --tables tags --flask
flask-sqlacodegen mysql+pymysql://root:root@127.0.0.1/tiantianlian --outfile "web/models/Uesr.py" --tables users --flask

need

Flask==1.0.2
Flask-Cache==0.13.1
Flask-Cors==3.0.6
Flask-SQLAlchemy==2.3.2
PyJWT==1.4.2
PyMySQL==0.9.2
redis==3.0.1
requests==2.20.0
Werkzeug=0.14.1

線上部署

1.安裝python3

sudo mkdir /usr/local/python3 # 創(chuàng)建安裝目錄
$ wget --no-check-certificate https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgz # 下載 Python 源文件 
# 注意:wget獲取https的時(shí)候要加上:--no-check-certifica
$ tar -xzvf Python-3.6.2.tgz # 解壓縮包
$ cd Python-3.6.2 # 進(jìn)入解壓目錄
sudo ./configure --prefix=/usr/local/python3 # 指定創(chuàng)建的目錄
sudo make
sudo make install # 編譯安裝

2.配置兩個(gè)版本共存

創(chuàng)建 python3 的軟鏈接:

$ sudo ln -s /usr/local/python3/bin/python3 /usr/bin/python3

3.安裝pip

# 下載源代碼
$ wget --no-check-certificate https://github.com/pypa/pip/archive/9.0.1.tar.gz

$ tar -zvxf 9.0.1.tar.gz    # 解壓文件

$ cd pip-9.0.1

$ python3 setup.py install # 使用 Python 3 安裝

創(chuàng)建鏈接:

$ sudo ln -s /usr/local/python3/bin/pip /usr/bin/pip3

升級(jí) pip

$ pip install --upgrade pip

3.安裝gunicorn

創(chuàng)建虛擬環(huán)境

cd /home/www/blog
mkdir venv
python3 -m venv venv

激活虛擬環(huán)境:

source venv/bin/activate

然后根據(jù)requirements.txt文件安裝依賴包:

pip3 install -r requirements.txt

安裝gunicorn

pip3 install gunicorn

啟動(dòng)服務(wù):

gunicorn -w 4 -b 127.0.0.1:8000 manager:app

4.nignx配置

server
{
    listen 80;
    listen 443 ssl;
    server_name ttl.pxx.com;
    index index.html index.htm default.php default.htm default.html;
    root /www/wwwroot/ttl_pxx_com;
 


    #SSL-END
    
    #ERROR-PAGE-START  錯(cuò)誤頁配置,可以注釋呼巷、刪除或修改
    error_page 404 /404.html;
    error_page 502 /502.html;
    #ERROR-PAGE-END
    
    #PHP-INFO-START  PHP引用配置在塔,可以注釋或修改
    include enable-php-00.conf;
    #PHP-INFO-END
    
    #REWRITE-START URL重寫規(guī)則引用,修改后將導(dǎo)致面板設(shè)置的偽靜態(tài)規(guī)則失效
    include /www/server/panel/vhost/rewrite/ttl.pxx.com.conf;
    #REWRITE-END
    
    #禁止訪問的文件或目錄
    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
    {
        return 404;
    }
    
    #一鍵申請(qǐng)SSL證書驗(yàn)證目錄相關(guān)設(shè)置
    location ~ \.well-known{
        allow all;
    }
    
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
        error_log off;
        access_log /dev/null;
    }
    
    location ~ .*\.(js|css)?$
    {
        expires      12h;
        error_log off;
        access_log /dev/null; 
    }
   
    location / {
        try_files $uri $uri/ @router;
         index index.html;
    }
    location @router {
         rewrite ^.*$ /index.html last;
    }
    
    location ^~ /api {
        proxy_pass http://127.0.0.1:8000/;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    }
    access_log  /www/wwwlogs/ttl.pxx.com.log;
    error_log  /www/wwwlogs/ttl.pxx.com.error.log;
}

4.安裝supervisor進(jìn)程守護(hù)

安裝supervisor在/www/supervisord目錄下

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

線上文件如下

; Sample supervisor config file.
;
; For more information on the config file, please see:
; http://supervisord.org/configuration.html
;
; Notes:
;  - Shell expansion ("~" or "$HOME") is not supported.  Environment
;    variables can be expanded using this syntax: "%(ENV_HOME)s".
;  - Comments must have a leading space: "a=b ;comment" not "a=b;comment".

[unix_http_server]
file=/www/supervisord/supervisor.sock   ; (the path to the socket file)
;chmod=0700                 ; socket file mode (default 0700)
;chown=nobody:nogroup       ; socket file uid:gid owner
;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))

[supervisord]
logfile=/www/supervisord/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB        ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10           ; (num of main logfile rotation backups;default 10)
loglevel=info                ; (log level;default info; others: debug,warn,trace)
pidfile=/www/supervisord/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=false               ; (start in foreground if true;default false)
minfds=1024                  ; (min. avail startup file descriptors;default 1024)
minprocs=200                 ; (min. avail process descriptors;default 200)
;umask=022                   ; (process file creation umask;default 022)
;user=chrism                 ; (default is current user, required if root)
;identifier=supervisor       ; (supervisord identifier, default is 'supervisor')
;directory=/tmp              ; (default is not to cd during start)
;nocleanup=true              ; (don't clean up tempfiles at start;default false)
;childlogdir=/tmp            ; ('AUTO' child log dir, default $TEMP)
;environment=KEY="value"     ; (key value pairs to add to environment)
;strip_ansi=false            ; (strip ansi escape codes in logs; def. false)

; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///www/supervisord/supervisor.sock ; use a unix:// URL  for a unix socket
;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
;username=chris              ; should be same as http_username if set
;password=123                ; should be same as http_password if set
;prompt=mysupervisor         ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history  ; use readline history if available

; The below sample program section shows all possible program subsection values,
; create one or more 'real' program: sections to be able to control them under
; supervisor.

;[program:theprogramname]
;command=/bin/cat              ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1                    ; number of processes copies to start (def 1)
;directory=/tmp                ; directory to cwd to before exec (def no cwd)
;umask=022                     ; umask for process (default None)
;priority=999                  ; the relative start priority (default 999)
;autostart=true                ; start at supervisord start (default: true)
;autorestart=unexpected        ; whether/when to restart (default: unexpected)
;startsecs=1                   ; number of secs prog must stay running (def. 1)
;startretries=3                ; max # of serial start failures (default 3)
;exitcodes=0,2                 ; 'expected' exit codes for process (default 0,2)
;stopsignal=QUIT               ; signal used to kill process (default TERM)
;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false             ; send stop signal to the UNIX process group (default false)
;killasgroup=false             ; SIGKILL the UNIX process group (def false)
;user=chrism                   ; setuid to this UNIX account to run the program
;redirect_stderr=true          ; redirect proc stderr to stdout (default false)
;stdout_logfile=/a/path        ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10     ; # of stdout logfile backups (default 10)
;stdout_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0)
;stdout_events_enabled=false   ; emit events on stdout writes (default false)
;stderr_logfile=/a/path        ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups=10     ; # of stderr logfile backups (default 10)
;stderr_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0)
;stderr_events_enabled=false   ; emit events on stderr writes (default false)
;environment=A="1",B="2"       ; process environment additions (def no adds)
;serverurl=AUTO                ; override serverurl computation (childutils)

; The below sample eventlistener section shows all possible
; eventlistener subsection values, create one or more 'real'
; eventlistener: sections to be able to handle event notifications
; sent by supervisor.

;[eventlistener:theeventlistenername]
;command=/bin/eventlistener    ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1                    ; number of processes copies to start (def 1)
;events=EVENT                  ; event notif. types to subscribe to (req'd)
;buffer_size=10                ; event buffer queue size (default 10)
;directory=/tmp                ; directory to cwd to before exec (def no cwd)
;umask=022                     ; umask for process (default None)
;priority=-1                   ; the relative start priority (default -1)
;autostart=true                ; start at supervisord start (default: true)
;autorestart=unexpected        ; whether/when to restart (default: unexpected)
;startsecs=1                   ; number of secs prog must stay running (def. 1)
;startretries=3                ; max # of serial start failures (default 3)
;exitcodes=0,2                 ; 'expected' exit codes for process (default 0,2)
;stopsignal=QUIT               ; signal used to kill process (default TERM)
;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false             ; send stop signal to the UNIX process group (default false)
;killasgroup=false             ; SIGKILL the UNIX process group (def false)
;user=chrism                   ; setuid to this UNIX account to run the program
;redirect_stderr=true          ; redirect proc stderr to stdout (default false)
;stdout_logfile=/a/path        ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10     ; # of stdout logfile backups (default 10)
;stdout_events_enabled=false   ; emit events on stdout writes (default false)
;stderr_logfile=/a/path        ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups        ; # of stderr logfile backups (default 10)
;stderr_events_enabled=false   ; emit events on stderr writes (default false)
;environment=A="1",B="2"       ; process environment additions
;serverurl=AUTO                ; override serverurl computation (childutils)

; The below sample group section shows all possible group values,
; create one or more 'real' group: sections to create "heterogeneous"
; process groups.

;[group:thegroupname]
;programs=progname1,progname2  ; each refers to 'x' in [program:x] definitions
;priority=999                  ; the relative start priority (default 999)

; The [include] section can just contain the "files" setting.  This
; setting can list multiple files (separated by whitespace or
; newlines).  It can also contain wildcards.  The filenames are
; interpreted as relative to this file.  Included files *cannot*
; include files themselves.

;[include]
;files = relative/directory/*.ini
[include]
files = /www/supervisord/conf/*.conf

在/www/supervisord/conf/下創(chuàng)建文件ttl.conf

[program:ttl]
command=/www/wwwroot/api_ttl_pxx_com/venv/bin/gunicorn -w4 -b 127.0.0.1:8000  manager:app    ;supervisor啟動(dòng)命令
directory=/www/wwwroot/api_ttl_pxx_com                                                 ; 項(xiàng)目的文件夾路徑
startsecs=0                                                               ; 啟動(dòng)時(shí)間
stopwaitsecs=0                                                            ; 終止等待時(shí)間
autostart=true                                                           ; 是否自動(dòng)啟動(dòng)
autorestart=true                                                         ; 是否自動(dòng)重啟
stdout_logfile=/www/wwwlogs/ttl.log                            ; log 日志
stderr_logfile=/www/wwwlogs/ttl.err                            ; 錯(cuò)誤日志

使用 supervsior 啟動(dòng) gunicorn

$ sudo supervisord -c supervisord.conf  
$ sudo supervisorctl start ttl

然后接著查看/etc/rc.local的權(quán)限

[root@openstack ~]# ll /etc/rc.local
lrwxrwxrwx. 1 root root 13 1月  29 10:45 /etc/rc.local -> rc.d/rc.local
[root@openstack ~]# ll /etc/rc.d/rc.local
-rw-r--r--. 1 root root 473 1月  25 23:52 /etc/rc.d/rc.local

/etc/rc.d/rc.local沒有執(zhí)行權(quán)限襟己,于是按說明的內(nèi)容執(zhí)行:

[root@openstack ~]# chmod +x /etc/rc.d/rc.local
[root@openstack ~]# ll /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 473 1月  25 23:52 /etc/rc.d/rc.local

在開機(jī)文件rc.loacl中添加開機(jī)啟動(dòng)

supervisord -c /www/supervisord/supervisord.conf

superviosrd命令
查看單個(gè)任務(wù)狀態(tài): supervisorctl status 服務(wù)名

# supervisorctl status sparkportal
sparkportal                      RUNNING   pid 26073, uptime 1 day, 23:12:10

啟動(dòng)/停止/重啟任務(wù)

啟動(dòng)任務(wù)
supervisorctl start 服務(wù)名

# supervisorctl stop sparkportal
sparkportal: stopped
#supervisorctl status sparkportal
sparkportal                      STOPPED   Jan 05 01:59 PM

停止任務(wù)
supervisorctl stop 服務(wù)名

# supervisorctl start sparkportal
sparkportal: started
# supervisorctl status sparkportal
sparkportal                      RUNNING   pid 32207, uptime 0:00:05

重啟任務(wù)
supervisorctl restart 服務(wù)名

# supervisorctl restart sparkportal
sparkportal: stopped
sparkportal: started
# supervisorctl status sparkportal

查看gunicorn狀態(tài)

pstree -ap |grep gunicorn

重啟進(jìn)程
kill -HUP 進(jìn)程編號(hào)

kill -HUP 2072
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市随抠,隨后出現(xiàn)的幾起案子禁筏,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,252評(píng)論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件第练,死亡現(xiàn)場(chǎng)離奇詭異阔馋,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)娇掏,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,886評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門垦缅,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人驹碍,你說我怎么就攤上這事壁涎。” “怎么了志秃?”我有些...
    開封第一講書人閱讀 168,814評(píng)論 0 361
  • 文/不壞的土叔 我叫張陵怔球,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我浮还,道長(zhǎng)竟坛,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,869評(píng)論 1 299
  • 正文 為了忘掉前任钧舌,我火速辦了婚禮担汤,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘洼冻。我一直安慰自己崭歧,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,888評(píng)論 6 398
  • 文/花漫 我一把揭開白布撞牢。 她就那樣靜靜地躺著率碾,像睡著了一般。 火紅的嫁衣襯著肌膚如雪屋彪。 梳的紋絲不亂的頭發(fā)上所宰,一...
    開封第一講書人閱讀 52,475評(píng)論 1 312
  • 那天,我揣著相機(jī)與錄音畜挥,去河邊找鬼仔粥。 笑死,一個(gè)胖子當(dāng)著我的面吹牛蟹但,可吹牛的內(nèi)容都是我干的躯泰。 我是一名探鬼主播,決...
    沈念sama閱讀 41,010評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼矮湘,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼斟冕!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起缅阳,我...
    開封第一講書人閱讀 39,924評(píng)論 0 277
  • 序言:老撾萬榮一對(duì)情侶失蹤磕蛇,失蹤者是張志新(化名)和其女友劉穎景描,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體秀撇,經(jīng)...
    沈念sama閱讀 46,469評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡超棺,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,552評(píng)論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了呵燕。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片棠绘。...
    茶點(diǎn)故事閱讀 40,680評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖再扭,靈堂內(nèi)的尸體忽然破棺而出氧苍,到底是詐尸還是另有隱情,我是刑警寧澤泛范,帶...
    沈念sama閱讀 36,362評(píng)論 5 351
  • 正文 年R本政府宣布让虐,位于F島的核電站,受9級(jí)特大地震影響罢荡,放射性物質(zhì)發(fā)生泄漏赡突。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,037評(píng)論 3 335
  • 文/蒙蒙 一区赵、第九天 我趴在偏房一處隱蔽的房頂上張望惭缰。 院中可真熱鬧,春花似錦笼才、人聲如沸漱受。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,519評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽拜效。三九已至,卻和暖如春各谚,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背到千。 一陣腳步聲響...
    開封第一講書人閱讀 33,621評(píng)論 1 274
  • 我被黑心中介騙來泰國(guó)打工昌渤, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人憔四。 一個(gè)月前我還...
    沈念sama閱讀 49,099評(píng)論 3 378
  • 正文 我出身青樓膀息,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親了赵。 傳聞我的和親對(duì)象是個(gè)殘疾皇子潜支,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,691評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容