一、Python
1. 源安裝 Python3
# 開發者工具
$ sudo yum -y install yum-utils
$ sudo yum-builddep python
# 下載解壓
$ wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz
$ tar -zxf Python-3.6.8.tgz
$ cd Python-3.6.8/
# 編譯安裝
$ ./configure
$ make
$ sudo make install
$ python3 --version
# 設置默認版本
$ alias python='/usr/local/bin/python3.6'
2. SCL安裝 Python3
# 1. 激活SCL
$ sudo yum install centos-release-scl
# 2.安裝python3
$ sudo yum install rh-python36
# 3.使用python3
$ python --version
Python 2.7.5
$ scl enable rh-python36 bash
$ python --version
Python 3.6.3
# 4. 安裝開發工具
$ sudo yum groupinstall 'Development Tools'
注意:
此處設定python3版本,如果重新打開會話,會恢復默認的python2.7
設置默認
$ scl enable python36 <command>
$ scl enable python36 bash
3. 虛擬環境venv
$ mkdir myapp
$ cd myapp
$ scl enable rh-python36 bash
$ python -m venv env
$ source env/bin/activate
(env) [xw@VM_0_6_centos myapp]$
4. 安裝Flask
hello.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World!'
(env) [xw@VM_0_6_centos myapp]$pip install --upgrade pip
(env) [xw@VM_0_6_centos myapp]$pip install Flask
(env) [xw@VM_0_6_centos myapp]$ export FLASK_APP=hello
(env) [xw@VM_0_6_centos myapp]$ flask run
(env) [xw@VM_0_6_centos myapp]$ deactivate
5. 安裝gunicorn
(env) [root@VM_0_6_centos myapp]$ pip3 install gunicorn
(env) [root@VM_0_6_centos myapp]# gunicorn hello:app
二、安裝Nginx
1. 安裝Nginx
# 1. 安裝Nginx
yum -y install nginx
systemctl enable nginx
systemctl start nginx
systemctl status nginx
# 2. 釋放端口防火牆HTTP (`80`) and HTTPS (`443`) ports.
firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
# 3. 瀏覽器`http://YOUR_IP`
2. 重要指令
# 開啟服務
sudo systemctl start nginx·#無輸出
sudo service start nginx #發行版命令
# 開機啟動
sudo systemctl enable nginx
# 關閉服務
sudo systemctl stop nginx
sudo service stop nginx
# 重啟
sudo systemctl restart nginx
sudo service restart nginx
# 重新加載
更改 Nginx 的配置時,都需要重新加載或重新啟動 Nginx
sudo systemctl reload nginx
sudo service reload nginx
# 測試語法錯誤
sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# 查看nginx狀態
sudo systemctl status nginx
# 查看nginx版本
sudo nginx -v
# 輸出 Nginx 版本以及配置選項
sudo nginx -V
指令小結:
sudo systemctl stop nginx
sudo systemctl start nginx
sudo systemctl restart nginx
sudo systemctl reload nginx
sudo systemctl disable nginx
sudo systemctl enable nginx
三、設置 Nginx server
1. 創建目錄結構
/var/www/
├── example.com
│ └── public_html
├── example2.com
│ └── public_html
├── example3.com
│ └── public_html
- 新建文件
mkdir -p /var/www/example.com/public_html
- 創建
index.html
sudo nano /var/www/example.com/public_html/index.html
用nano
文本編輯器粘貼/var/www/example.com/public_html/index.html
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>案例測試</title>
</head>
<body>
<h1>成功部署</h1>
</body>
</html>
- 修改用戶組
sudo chown -R nginx: /var/www/example.com
2. 配置server
Nginx 服務器塊配置文件文件必須以.conf
結尾,並存儲在目錄/etc/nginx/conf.d
中
1.新建example.com.conf
/etc/nginx/conf.d/example.com.conf
server {
listen 80;
listen [::]:80;
root /var/www/example.com/public_html;
index index.html;
server_name example.com www.example.com;
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
location / {
try_files $uri $uri/ =404;
}
}
- 測試配置
sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
- 重啟
sudo systemctl restart nginx
3. Flask + Nginx + Gunicorn
- 配置服務器
server {
listen 80;
server_name 49.234.220.252;
access_log /var/log/nginx/hello/access.log;
error_log /var/log/nginx/hello/error.log;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-Ip $remote_addr;
}
}
- 啟動
$ cd myapp/
$ source env/bin/activate
# 創建日志文件夾
(env) $ mkdir -p /var/log/nginx/hello/
# 修改所有者
(env) $ sudo chown -R nginx: /var/log/nginx/hello
(env) $ sudo nginx -t
(env) $ systemctl reload nginx
(env) $ gunicorn hello:app
- 查看進程
# pstree -ap|grep gunicorn
四、Supervisor
進程管理工具,方便的監聽、啟動、停止、重啟一個或多個進程。
- supervisor:要安裝的軟件的名稱。
- supervisord:裝好supervisor軟件后,supervisord用於啟動supervisor服務。
- supervisorctl:用於管理supervisor配置文件中program。
1. 安裝supervisor
yum -y install supervisor
# 生成2個文件
`-- /etc/
|-- ...
|-- supervisord.conf # 配置文件
`-- supervisord.d/ # 配置文件夾
修改supervisord.conf
[unix_http_server]
file=/var/run/supervisor.sock
[supervisord]
logfile=/var/log/supervisor/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=/var/run/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)
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///var/run/supervisor.sock
[include]
files = supervisord.d/*.ini
2. 自定義配置示例
- 測試程序結構
`-- dosupervisor/
|-- log/ # 日志文件夾
| |-- long.err.log #錯誤
| `-- long.out.log #輸出
`-- long.sh # 調用程序
# long.sh
#!/bin/bash
while true
do
# Echo current date to stdout
echo `date`
# Echo 'error!' to stderr
echo 'error!' >&2
sleep 1
done
- 配置
long_script.conf
# /etc/supervisord.d/long_script.conf
[program:long_script]
command=/root/dosupervisor/long.sh
autostart=true
autorestart=true
stderr_logfile=/root/dosupervisor/log/long.err.log
stdout_logfile=/root/dosupervisor/log/long.out.log
- 查看狀態
[root@VM_0_6_centos ~]# supervisorctl status
long_script RUNNING pid 16862, uptime 0:00:51
[root@VM_0_6_centos ~]# supervisorctl stop long_script
long_script: stopped
[root@VM_0_6_centos ~]# supervisorctl status
long_script STOPPED Aug 02 11:22 PM
- 滾動查看結果
tail -f long.out.log
3. 用到的指令
yum -y remove supervisor #卸載
supervisord --version
# 初始化配置
echo_supervisord_conf > /etc/supervisord.conf
supervisord : 啟動supervisor
supervisorctl reload :修改完配置文件后重新啟動supervisor
supervisorctl status :查看supervisor監管的進程狀態
supervisorctl start 進程名 :啟動XXX進程
supervisorctl stop 進程名 :停止XXX進程
supervisorctl stop all:停止全部進程。
supervisorctl update:根據最新的配置文件,啟動新配置或有改動的進程,配置沒有改動的進程不會受影響而重啟
# 看進程服務
ps -ef | grep supervisord
#啟動supervisor,-c制定讓其讀取的配置文件
supervisord -c /etc/supervisord.d/long_script.conf
#關閉supervisor
supervisorctl shutdown
#重新加載supervisor配置文件,並重啟superivisor
supervisorctl reload
# 設置開機啟動
systemctl enable supervisord
4. 開機啟動
systemctl enable supervisord
systemctl is-enabled supervisord
systemctl stop supervisord
systemctl start supervisord
systemctl status supervisord
systemctl reload supervisord
systemctl restart supervisord
systemctl restart supervisord
supervisorctl reload
五、Flask+Gunicorn+Nginx+Supervisord
1. 目錄結構
$ tree -I "env|__pycache*|*.pyc" -FCL 3
.
|-- hello.py
`-- rungun.sh*
2. 主程序
# hello.py
$ cat hello.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return '<h1>hello word</h1>'
if __name__ == '__main__':
app.run(host='0.0.0.0',debug=True)
3. 定義啟動項
$ cat rungun.sh
#!/bin/bash
cd /root/myapp
source env/bin/activate
gunicorn hello:app
4. 定義supervisord
$ cat /etc/supervisord.d/hello.ini
[program:hello]
command=/root/myapp/rungun.sh
autostart=true
autorestart=true
stdout_logfile=/var/log/supervisor/hello/hello_out.log
stderr_logfile=/var/log/supervisor/hello/hello_err.log
5. 配置nginx
$ cat /etc/nginx/conf.d/hello.conf
server {
listen 80;
server_name 49.234.220.252;
access_log /var/log/nginx/hello/access.log;
error_log /var/log/nginx/hello/error.log;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-Ip $remote_addr;
}
}
6. 命令
systemctl restart supervisord
supervisorctl reload
更優方案
上述方法只能啟動,無法停止或者重啟
重新定義supervisord
$ cat /etc/supervisord.d/hello.ini
[program:hello]
command=/root/myapp/env/bin/gunicorn hello:app
directory=/root/myapp
autostart=true
autorestart=true
stdout_logfile=/var/log/supervisor/hello/hello_out.log
stderr_logfile=/var/log/supervisor/hello/hello_err.log
命令
(env) [root@VM_0_6_centos bin]# supervisorctl reload
Restarted supervisord
(env) [root@VM_0_6_centos bin]# supervisorctl stop hello
hello: stopped
(env) [root@VM_0_6_centos bin]# supervisorctl start hello
hello: started
參考
[1]. How To Install Nginx on CentOS 7
[2]. How to Install Python 3 on CentOS 7
[3]. How to install Python3 on CentOS
[4]. Deploying a Flask Site Using NGINX Gunicorn, Supervisor and Virtualenv on Ubuntu
[6]. python web 部署:nginx + gunicorn + supervisor + flask 部署筆記