Centos部署Python環境
部署Flask應用需要提前配置Python環境,如果您的Centos機器中沒有Python環境,可以參考我的另外一篇博客《Centos離線安裝Python》,在Centos中離線部署Python環境。
安裝Flask應用所需的Python庫
這一步的安裝,有以下假設:
- 假設您已經下載了Flask應用所需的Python庫,並存放在Centos機器的
/root/softwares/06-python-packages
文件夾中;如果沒有,您可以從我的百度網盤鏈接中下載(鏈接:https://pan.baidu.com/s/1RBmPcHim2OWbwNtY3XzlSw ,提取碼:ovjm)。如果我提供的Python庫不能滿足您的需求,您可以把下載後的Python庫存放在06-python-packages文件夾中 - 假設您的Flask應用有requirements.txt文件存儲Flask應用所需的Python庫;如果通過命令
pip freeze > requirements.txt
導出Flask應用依賴的庫至requirements.txt文件。
以上的假設,如果都滿足了,可以開始安裝Flaks應用依賴的Python庫。
- 在Centos中,使用命令進入Flask應用的根目錄
- 使用命令
pip install --no-index --find-links=/root/softwares/06-python-packages/ -r requirements.txt
安裝Python庫
安裝、配置supervisor
安裝supervisor
- 使用命令
cd /root/softwares/09-supervisor
進入09-supervisor文件夾 - 使用命令
rpm -Uvh *.rpm --nodeps --force
安裝supervisor
爲Flask配置supervisor
- 使用命令
echo_supervisord_conf > /etc/supervisord.d/flask.conf
創建flask.conf文件 - 使用命令
vi /etc/supervisord.d/flask.conf
打開該文件,並在此文件的最下方添加以下內容
[program:yngt]
command=python3 manage.py runserve
directory=/root/resources_directory_tree/server
startsecs=1
autostart=true
autorestart=true
相關的配置選項的意思如下:
yngt
表示supervisor子進程的名稱directory
表示腳本目錄command
表示腳本執行的命令startsecs
表示子進程啓動多少秒之後,如果其狀態爲RUNNING,則supervisor認爲其已經啓動autostart
表示子進程會隨着supervisor啓動而啓動autorestart
表示如果子進程如果異常退出後,是否會自動重啓,共有true、false和unexpected,其中unexpected表示如果子進程的退出碼不再exitcodes裏定義的退出碼時,纔會自動重啓
更多的配置選項請查看 https://wuguiyunwei.com/index.php/2017/06/26/1028.html
啓動supervisor子進程
- 使用命令
supervisord -c /etc/supervisord.d/flask.conf
初始化supervisor - 使用命令
supervisorctl -c /etc/supervisord.d/flask.conf
進入supervisorctl命令管理界面 - 使用命令
start yngt
啓動子進程,其中yngt表示上一步配置中的子進程名稱 - 如果Centos重啓後,子進程沒有自動啓動,請重複這一步操作
安裝Nginx
安裝nginx
- 使用命令
cd /root/softwares/03-nginx
進入03-nginx文件夾 - 使用命令
tar -xzvf nginx-1.14.2.tar.gz
解壓,然後cd nginx-1.14.2/
進入nginx-1.14.2文件夾 - 使用命令
./configure --prefix=/usr/local/nginx
配置nginx的編譯選項,其中prefix指定了安裝路徑 - 使用命令
make
編譯nginx源代碼 - 使用命令
make install
安裝編譯後的nginx - 使用命令
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
配置nginx可以全局訪問 - 使用命令
nginx -v
,如果能夠獲取到nginx的版本,表示nginx安裝成功
將nginx添加到系統服務
- 使用命令
vi /etc/init.d/nginx
創建文件,並輸入以下內容
#! /bin/bash
# chkconfig: - 85 15
PATH=/usr/local/nginx
DESC="nginx daemon"
NAME=nginx
DAEMON=$PATH/sbin/$NAME
CONFIGFILE=$PATH/conf/$NAME.conf
PIDFILE=$PATH/logs/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
set -e
[ -x "$DAEMON" ] || exit 0
do_start() {
$DAEMON -c $CONFIGFILE || echo -n "nginx already running"
}
do_stop() {
$DAEMON -s stop || echo -n "nginx not running"
}
do_reload() {
$DAEMON -s reload || echo -n "nginx can't reload"
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
do_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
do_stop
echo "."
;;
reload|graceful)
echo -n "Reloading $DESC configuration..."
do_reload
echo "."
;;
restart)
echo -n "Restarting $DESC: $NAME"
do_stop
do_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2
exit 3
;;
esac
exit 0
- 使用命令
chmod a+x /etc/init.d/nginx
設置權限 - 使用命令
chkconfig --add nginx
註冊成服務 - 使用命令
chkconfig nginx on
配置開機啓動
爲Flask應用配置nginx
- 使用命令
vi /usr/local/nginx/conf/nginx.conf
打開nginx默認配置文件,修改server配置如下:
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
proxy_pass http://127.0.0.1:5000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
index index.html index.htm;
}
其中,proxy_pass和proxy_set_header是新增的配置,其餘不變。
- 使用命令
systemctl reload nginx.service
重新加載nginx配置文件 - 使用命令
nginx -t
,如果提示successful信息,表示nginx配置文件加載成功
nginx常用命令
systemctl start nginx.service
,啓動nginx服務systemctl stop nginx.service
,停止nginx服務systemctl restart nginx.service
,重啓nginx服務systemctl reload nginx.service
重新加載nginx配置文件
其他
如果您想了解更多的關於nginx的內容,請查看以下鏈接: