目录
一、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 --versionPython 2.7.5$ scl enable rh-python36 bash$ python --versionPython 3.6.3# 4. 安装开发工具$ sudo yum groupinstall 'Development Tools'
注意:
此处设定python3版本,如果重新打开会话,会恢复默认的python2.7
设置默认
$ scl enable python36$ 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 Flaskapp = 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. 安装Nginxyum -y install nginxsystemctl enable nginxsystemctl start nginxsystemctl status nginx# 2. 释放端口防火墙HTTP (`80`) and HTTPS (`443`) ports.firewall-cmd --permanent --zone=public --add-service=httpfirewall-cmd --permanent --zone=public --add-service=httpssudo firewall-cmd --reload# 3. 浏览器`http://YOUR_IP`
2. 重要指令
# 开启服务sudo systemctl start nginx·#无输出sudo service start nginx #发行版命令# 开机启动sudo systemctl enable nginx # 关闭服务sudo systemctl stop nginxsudo service stop nginx# 重启sudo systemctl restart nginxsudo service restart nginx# 重新加载更改 Nginx 的配置时,都需要重新加载或重新启动 Nginxsudo systemctl reload nginxsudo service reload nginx# 测试语法错误sudo nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: 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 nginxsudo systemctl start nginxsudo systemctl restart nginxsudo systemctl reload nginxsudo systemctl disable nginxsudo 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
案例测试 成功部署
- 修改用户组
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 -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: 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/bashwhile truedo # Echo current date to stdout echo `date` # Echo 'error!' to stderr echo 'error!' >&2 sleep 1done
- 配置
long_script.conf
# /etc/supervisord.d/long_script.conf[program:long_script]command=/root/dosupervisor/long.shautostart=trueautorestart=truestderr_logfile=/root/dosupervisor/log/long.err.logstdout_logfile=/root/dosupervisor/log/long.out.log
- 查看状态
[root@VM_0_6_centos ~]# supervisorctl statuslong_script RUNNING pid 16862, uptime 0:00:51[root@VM_0_6_centos ~]# supervisorctl stop long_scriptlong_script: stopped[root@VM_0_6_centos ~]# supervisorctl statuslong_script STOPPED Aug 02 11:22 PM
- 滚动查看结果
tail -f long.out.log
3. 用到的指令
yum -y remove supervisor #卸载supervisord --version# 初始化配置echo_supervisord_conf > /etc/supervisord.confsupervisord : 启动supervisorsupervisorctl reload :修改完配置文件后重新启动supervisorsupervisorctl 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#关闭supervisorsupervisorctl shutdown#重新加载supervisor配置文件,并重启superivisorsupervisorctl reload# 设置开机启动systemctl enable supervisord
4. 开机启动
systemctl enable supervisordsystemctl is-enabled supervisordsystemctl stop supervisordsystemctl start supervisordsystemctl status supervisordsystemctl reload supervisordsystemctl 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 Flaskapp = Flask(__name__)@app.route('/')def hello(): return 'hello word
'if __name__ == '__main__': app.run(host='0.0.0.0',debug=True)
3. 定义启动项
$ cat rungun.sh #!/bin/bashcd /root/myappsource env/bin/activategunicorn hello:app
4. 定义supervisord
$ cat /etc/supervisord.d/hello.ini [program:hello]command=/root/myapp/rungun.shautostart=trueautorestart=truestdout_logfile=/var/log/supervisor/hello/hello_out.logstderr_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 supervisordsupervisorctl reload
更优方案
上述方法只能启动,无法停止或者重启
重新定义supervisord
$ cat /etc/supervisord.d/hello.ini [program:hello]command=/root/myapp/env/bin/gunicorn hello:appdirectory=/root/myapp autostart=trueautorestart=truestdout_logfile=/var/log/supervisor/hello/hello_out.logstderr_logfile=/var/log/supervisor/hello/hello_err.log
命令
(env) [root@VM_0_6_centos bin]# supervisorctl reloadRestarted supervisord(env) [root@VM_0_6_centos bin]# supervisorctl stop hellohello: stopped(env) [root@VM_0_6_centos bin]# supervisorctl start hellohello: started
参考
[1].
[2].
[3].
[4].
[5].
[6].