摘要1:https://www.cnblogs.com/WiseAdministrator/articles/11123271.html#_label2
摘要2:https://www.cnblogs.com/difs/p/9688035.html
摘要3:https://www.cnblogs.com/oyaisusu/p/13817081.html
项目配置
- 创建一个简单的flask项目
from flask import Flask, jsonify
app = Flask(__name__)
@app.route("/index")
def index():
return jsonify("hello world!")
if __name__ == '__main__':
app.run(host="0.0.0.0", port=8000)
- 项目结构
tree -L 1 # 显示第一层目录
[root@k8s-master datas]# tree -L 1
[root@k8s-master datas]# tree -L 1
.
├── app.py # flask项目
├── html # 存储nginx静态文件地址
├── mypro # 项目虚拟环境
├── nginx.conf # nginx配置文件
├── nginx.conf.bak # 备份
├── nohup.out
├── __pycache__
├── uwsgi.ini # uwsgi启动文件
└── uwsgi.pid
3 directories, 6 files
配置uwsgi
- 编写uwsgi.ini
[uwsgi]
socket=172.23.72.65:8000 # 使用nginx启动使用socket。单独通过uwsgi启动用 http:172.23.72.65:8000
chdir = /tmp/datas
virtualenv =/tmp/datas/mypro
wsgi-file = /tmp/datas/app.py
callable = app
#plugins = python
master = true
vacuum = true
chmod-socket = 664
processes = 3
daemonize = /var/log/mypro/uwsgi.log
pidfile = /tmp/datas/uwsgi.pid
- 通过uwsgi.ini启动flask项目
uwsgi --ini uwsgi.ini
配置nginx
- 编辑nginx.conf文件
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
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;
keepalive_timeout 65;
#gzip on;
server {
listen 80; # 监听80端口
server_name 172.23.72.65; # flask服务地址
location / {
uwsgi_pass 172.23.72.65:8000; # flask服务地址+端口
include uwsgi_params; # 默认uwsgi_params
root /var/nginx/html; # nginx根目录
index index.html index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/nginx/html;
}
}
}
docker配置nginx
- 拉取nginx镜像
docker pull nginx
- 启动nginx
# 启动容器
docker run --name nginx-uwsgi -p 80:80 -d nginx
# 获取容器id
docker ps
- 将项目中nginx.conf配置文件替换掉容器中默认的nginx.conf配置文件
docker cp /tmp/datas/nginx.conf 7c9fcf3ece3e:/etc/nginx/nginx.conf # 7c9fcf3ece3e: 容器id
- 将当前容器保存为镜像
docker commit 7c9fcf3ece3e nginx-test # 生成新nginx镜像nginx-test
- 启动新镜像nginx-test,重新启动uwsgi
# 启动nginx
docker run --name nginx-test -v /tmp/datas/html/:/var/nginx/html -p 8915:80 -d nginx-test
# 杀死uwsgi
pkill -9 uwsgi
# 重新启动uwsgi
uwsgi --ini uwsgi.ini
- 客户端访问