1.安装必须的几个工具

yum install python-devel # 开发版包含python.h头文件
yum install epel-release # 安装setputools工具 centos默认没有
yum install python-setuptools # 安装setputools工具
easy_install pip # 安装强大的pip
pip install virtualenv #安装虚拟环境
pip install supervisor #安装supervisor

2.在虚拟机中安装flask
cd /www/shop
vim shop.py # 一下内容

from flask import Flask
app = Flask(__name__)
@app.route(‘/’)
def index():
return ‘hello world’
if __name__ == ‘__main__':
app.debug = True
app.run()

安装环境 flask是在虚拟机中安装的
virtualenv venv
source venv/bin/activate

pip install flask
pip install gunicorn

3.配置supervisor

echo_supervisord_conf > /etc/supervisord.conf

supervisord -c /etc/supervisord.conf

ps aux | grep supervisord

vi /etc/supervisord.conf 最后两行改为

[include]
files = /etc/supervisor/*.conf
vim /etc/supervisor/shop.conf 适当修改

[program:shop]
command=/www/shop/venv/bin/gunicorn -w4 -b0.0.0.0:8001 shop:app
;numprocs=1
;process_name=%(program_name)s
directory=/www/shop
user=root
autorestart=true
redirect_stderr=true
stdout_logfile = /www/shop/log/info.log
loglevel=info
supervisorctl reload 重载配置
supervisorctl status 查看状态

4.配置nginx
新建nginx配置文件 /usr/local/nginx/conf/vhost/shop.conf

server {

listen 80;
server_name yourdomain.com;

root /www/shop;

location / {
proxy_pass http://127.0.0.1:8001; #gunicorn对应的端口
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

}
保存后测试配置并重启nginx配置
/usr/local/nginx/sbin/nginx -t
/etc/init.d/nginx reload
直接下载看:部署flask项目

Comments are closed.

Post Navigation