Flask——Linux+Nginx+Uwsgi+Supervisor 部署

codelife發表於2019-08-25

部署前需要關閉防火牆:

iptables -F

安裝python3

環境centos7.2

下載、解壓(編譯安裝,把下載好的python3安裝包解壓):

Linux下預設系統自帶python2.7的版本,這個版本被系統很多程式所依賴,所以不建議刪除,如果使用最新的Python3那麼我們知道編譯安裝原始碼包和系統預設包之間是沒有任何影響的,所以可以安裝python3和python2共存

  首先去python官網下載python3的原始碼包,網址:https://www.python.org/

  進去之後點選導航欄的Downloads,也可以滑鼠放到Downloads上彈出選單選擇Source code,就是原始碼包的意思,這裡選擇最新版本36.2,當然下面也有很多其他歷史版本,點進去之後頁面下方可以看到下載連結,包括原始碼包、Mac OSX安裝包、Windows的安裝包

tar -xvf Python-3.6.2.tgz


進入目錄:

cd Python-3.6.2/

新增配置:

./configure --with-ssl --prefix=/usr/python

這裡配置自己的安裝目錄,接下來編譯原始碼、安裝:

make && make install

如果出現錯誤提示缺少zlib...啥的則需要安裝zlib相關依賴包

yum -y install zlib*

注:如果安裝後出現Ignoring ensurepip failure: pip 9.0.1 requires SSL/TLS

yum install -y openssl-devel

2、進入 Python安裝包,修改Module路徑的setup檔案:

vim Modules/Setup

找到下面一行程式碼,去掉註釋:

#zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz
去掉註釋
zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz

系統中原來的python在/usr/bin/python,透過ls -l可以看到,python是一個軟連結,連結到本目錄下的python2.7

我們可以把這個刪除,也可以新建一個python3的軟連結,只不過執行時python要改成python3,或者python指令碼頭部宣告要改為#!/usr/bin/python3

ln -s /usr/python/bin/python3 /usr/bin/python3

安裝pip

wget https://bootstrap.pypa.io/get-pip.py --no-check-certificate
python3 get-pip.py

如果pip安裝模組時提示

pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.

則需要安裝openssl

yum install -y openssl-devel

然後重新編譯安裝即可

安裝virtualenv

pip install virtualenv
mkdir myproject
cd myproject
virtualenv --no-site-packages venv (指定python版本:virtualenv -p /usr/bin/python3 venv)
linux:source venv/bin/activate windows:venv\Scripts\activate
deactivate 退出

注:如果出現 bash: pip: command not found

首先查詢pip安裝目錄 find / -name pip

然後建立軟鏈 ln -s /usr/python/bin/pip /usr/bin/pip

安裝uwsgi

pip install uwsgi

配置uwsgi

在網站根目錄下建立config.ini檔案,寫入以下內容

[uwsgi]
# uwsgi 啟動時所使用的地址與埠
socket = 127.0.0.1:8001
# 指向網站目錄
chdir = /home/project/flask/
# python 啟動程式檔案
wsgi-file = manage.py
# python 程式內用以啟動的 application 變數名
callable = app
# 處理器數
processes = 4
# 執行緒數
threads = 2
#狀態檢測地址
stats = 127.0.0.1:9191

安裝nginx

yum -y install nginx

配置nginx

vim /etc/nginx/conf.d/default.conf
server{
    listen 80;
    server_name www.chenyy.xin;
    root /home/project/flask;
    location / {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:8001; # 指向uwsgi 所應用的內部地址,所有請求將轉發給uwsgi 處理
        uwsgi_param UWSGI_PYHOME /home/project/flask/flask_env361; # 指向虛擬環境目錄
        uwsgi_param UWSGI_CHDIR /home/project/flask; # 指向網站根目錄
        uwsgi_param UWSGI_SCRIPT manage:app; # 指定啟動程式
    }
}

#開啟
/bin/systemctl start nginx.service

#重啟
/bin/systemctl restart nginx.service

安裝supervisor

yum install python-setuptools
pip install supervisor

測試是否安裝成功

echo_supervisord_conf

建立配置檔案

mkdir -m 755 -p /etc/supervisor/
cd /etc
echo_supervisord_conf > /etc/supervisor/supervisord.conf
cd supervisor

建立專案配置檔案目錄

mkdir -m 755 conf.d
cd /etc/supervisor/conf.d
vim flask.conf

[program:flask]
command =/home/project/flask/flask_env361/bin/uwsgi /home/project/flask/config.ini
directory =/home/project/flask
user =root
autostart=true
autorestart=true
startsecs=5
priority=1
stopasgroup=true
killasgroup=true
stderr_logfile=/home/project/log/error.log#要確保該目錄存在

在主配文件中引入flask.conf

vim supervisord.conf

找到include

;[include]
;files = relative/directory/*.ini
//改為
[include]
files = ./conf.d/*.conf

啟動supervisor

supervisord -c supervisord.conf

如果出現以下錯誤

Error: Another program is already listening on a port that one of our HTTP servers is configured to use. Shut this program down first before starting supervisord.
For help, use /root/.pyenv/versions/2.7.6/bin/supervisord -h

找到 supervisor.sock

unlink supervisor.sock

重新啟動supervisor

用supervisorctl檢視已經被監控的program

(flask_env361) [root@iZ2zefw4wlcm0he1pjkawoZ supervisor]# supervisorctl
flask RUNNING pid 1764, uptime 0:59:45

如果supervisorctl沒效果,換成

supervisorctl -c /etc/supervisor/supervisord.conf

重啟監控服務

supervisorctl reload

異常解決

[root@iZ2zefw4wlcm0he1pjkawoZ supervisor]# supervisorctl
unix:///tmp/supervisor.sock no such file
supervisor> reload
Really restart the remote supervisord process y/N? y
error: <class  'socket.error'>, [Errno 2] No such file or directory: file: /usr/lib64/python2.7/socket.py line: 224
supervisor> status
unix:///tmp/supervisor.sock no such file
supervisor> exit
[root@iZ2zefw4wlcm0he1pjkawoZ supervisor]# supervisord
/usr/lib/python2.7/site-packages/supervisor/options.py:298: UserWarning: Supervisord is running as root and it is searching for its configuration file in default locations (including its current working directory); you probably want to specify a "-c" argument specifying an absolute path to a configuration file for improved security.
'Supervisord is running as root and it is searching '
[root@iZ2zefw4wlcm0he1pjkawoZ supervisor]# supervisorctl
flask RUNNING pid 2 2995, uptime 0:00:15

安裝mysql

1.下載MySQL源

wget http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm

2.安裝MySQL源

sudo rpm -ivh mysql57-community-release-el7-7.noarch.rpm

3.安裝mysql-community-server

sudo yum install mysql-community-server -y

4.重啟mysql

systemctl restart mysqld
注:自動啟動服務用 systemctl enable mysqld

5.檢視臨時密碼

grep 'A temporary password' /var/log/mysqld.log

6.資料庫修改密碼

mysql -u root -p
set global validate_password_policy=0;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'xxxxxx';

yum -y install mysql-devel

檢視日誌

tail -f /var/log/mysqld.log
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章