- Flask:后台业务处理
- Nginx:反向代理
- Gunicorn: 运行flask app
- Supervisor:监听控制gunicorn进程
原文链接https://medium.com/ymedialabs-innovation/deploy-flask-app-with-nginx-using-gunicorn-and-supervisor-d7a93aa07c18
1.新建Flask应用文件 app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return "Hello World"
if __name__ == '__main__':
app.run(debug=True)
2.设置Gunicorn
安装 gunicorn
pip install gunicorn
启动Gunicore 进程运行Flask app
gunicorn app:app -b localhost:8000 &
该命令会让Gunicorn进程在后台运行
3.使用supervisor
安装
sudo apt-get install supervisor
supervisor会监视Gunicorn进程确保如果发生任何意外都能顺利重启,确保Gunicorn在开机时启动
添加配置文件
在/etc/supervisor/conf.d/创建配置文件hello_world.conf
[program:helloworld] #app名字
drectory=/home/ubuntu/python/hello_world # app所在目录
command=/home/ubuntu/.virtualenvs/flaskdemo/bin/gunicorn app:app -b localhost:8000 # 启动命令
autostart=true #是否自动启动
autorestart=true#是否自动重新启动
strerr_logfile=/var/log/hello_world/hello_world.err.log #错误日志的位置,不存在需要创建
strout_logfile=/var/log/hello_world/hello_world.out.log # 所有日志位置,不存在需要创建
应用配置
sudo supervisorctl reread
sudo service supervisor restart
执行上述命令,将去启动一个新的进程。检查所有监视app的状态
sudo supervisorctl status
4.配置nginx
安装
sudo apt-get install nginx
sudo vim /etc/nginx/conf.d/virtual.conf
server {
listen 80;
server_name your_public_dnsname_here; #配置ip地址
location / {
proxy_pass http://127.0.0.1:8000; #这里的必须和gunicorn启动的端口一致
}
}
// 如果有多个服务需要部署继续在下面添加
server {
listen 80;
server_name your_public_dnsname_here; #配置ip地址
location / {
proxy_pass http://127.0.0.1:8000; #这里的必须和gunicorn启动的端口一致
}
}
配置完成
启动Nginx
sudo nginx -t
sudo service nginx restart
访问ip地址,应该可以打开网页了
0 Comments latest
No comments.