考虑web的访问量不大,就没有配置nginx。
1、安装 supervisor安装,gunicorn,flask
supervisor:
yum -y install epel-release wget
yum -y install supervisor
gunicorn:
pip install gunicorn
注意gunicorn可能不在PATH变量中,需要带路径执行。
yum install flask
2、配置
flask代码
#!/usr/local/python/bin/python3
from flask import Flask, jsonify
app = Flask(__name__)
tasks = [
{
'id': 1,
'title': u'Buy groceries',
'description': u'Milk, Cheese, Pizza, Fruit, Tylenol',
'done': False
},
{
'id': 2,
'title': u'Learn Python',
'description': u'Need to find a good Python tutorial on the web',
'done': False
}
]
@app.route('/todo/api/v1.0/tasks', methods=['GET'])
def get_tasks():
return jsonify({'tasks': tasks})
if __name__ == '__main__':
app.run(debug=True)
supervisor:
1,echo_supervisord_conf > /etc/supervisord.conf
修改/etc/supervisord.conf,增加如下内容,原来的内容是默认注释掉的,这个是用来通过web监控的,可以通过ip:9001访问查看。[include]是针对启动的进程的配置
[inet_http_server] ; inet (TCP) server disabled by default
port=0.0.0.0:9001 ; (ip_address:port specifier, *:port for all iface)
username=admin ; (default is no username (open server))
password=admin ; (default is no password (open server))
[include]
;files = relative/directory/.ini
files = /etc/supervisor/.conf
2、说明:supervisor目录需要新建
/etc/supervisor/my_job.conf的内容,日志目录如果不存在,则需要手动创建
[program:app]
directory = /home/jenkins/web_api ; 程序的启动目录
command = /usr/local/python/bin/gunicorn -w 8 -b 0.0.0.0:5000 app:app ; 启动命令
autostart = true ; 在 supervisord 启动的时候也自动启动
startsecs = 5 ; 启动 5 秒后没有异常退出,就当作已经正常启动了
autorestart = true ; 程序异常退出后自动重启
startretries = 3 ; 启动失败自动重试次数,默认是 3
;user = leon ; 用哪个用户启动
redirect_stderr = true ; 把 stderr 重定向到 stdout,默认 false
stdout_logfile_maxbytes = 20MB ; stdout 日志文件大小,默认 50MB
stdout_logfile_backups = 20 ; stdout 日志文件备份数
; stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件)
stdout_logfile = /data/logs/app_stdout.log
Supervicor使用
1、启动supervisor
supervisord -c /etc/supervisord.conf
2、supervisorctl的使用
supervisorctl status # 查询进程状态
supervisorctl stop node # 关闭 [program:node] 的进程
supervisorctl start node # 启动 [program:node] 的进程
supervisorctl restart node # 重启 [program:node] 的进程
supervisorctl stop all # 关闭所有进程
supervisorctl start all # 启动所有进程
supervisorctl reload # 重新读取配置文件,读取有更新(增加)的配置文件,不会启动新添加的程序
supervisorctl update # 重启配置文件修改过的程序
配置nginx
yum install epel-release
yum install nginx
/etc/nginx/nginx.conf
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name 192.168.56.101;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://127.0.0.1:5000; # 这里是指向 gunicorn host 的服务地址
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
检查nginx语法
nginx -t
service nginx status
service nginx stop
service nginx start
配置https,https通过443端口访问:
证书配置参考:
https://blog.51cto.com/13043516/2298296?source=dra
/etc/nginx/nginx.conf如下
server {
listen 80;
listen 443 ssl;
server_name 192.168.56.101;
root /usr/share/nginx/html;
ssl_certificate "/etc/pki/CA/cacert.pem";
ssl_certificate_key "/etc/pki/CA/private/cakey.pem";
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://127.0.0.1:5000; # 这里是指向 gunicorn host 的服务地址
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
开机自动启动,后续更新