Flask部署

2019-08-17  本文已影响0人  小法19

Flask部署

  1. 安装zsh工具
yum install -y zsh
yum install -y git
wget --no-check-certificate https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | sh
  1. 新建用户
useradd www -d /home/www -m -s /bin/zsh
passwd www
  1. 安装Virtualenv
pip install virtualenv
pip install flask
yum -y update
  1. 新建python虚拟环境, 虚拟环境内安装flask
cd /home/www
virtualenv venv
source venv/bin/activate
pip install flask
  1. 新建python测试文件 /home/www/hello.py
vim hello.py
#coding=utf-8
from flask import Flask

app = Flask(__name__)


@app.route('/')
def hello_world():
    return "<h1 style='color:blue'>Hello There!</h1>"


if __name__ == '__main__':
    app.run() 
  1. 在venv激活的情况下安装gunicorn
source /home/www/venv/bin/activate
pip install gunicorn
gunicorn hello:app
netstat -an | grep 80  
  1. 退出激活状态,安装nginx
yum install nginx
systemctl enable nginx
systemctl start nginx
vim /etc/nginx/nginx.conf
server {
    listen 80;
    server_name 47.90.99.85;
    root /home/www;
    access_log /home/www/logs/nginx_access.log;
    error_log /home/www/logs/nginx_error.log;
    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
    }

  }
nginx -t -c /etc/nginx/nginx.conf
nginx -s reload
  1. 安装supervisor到系统, 生成配置文件
pip install supervisor
echo_supervisord_conf > /etc/supervisord.conf
  1. 编辑/etc/supervisord.conf,并在最后一行加入一下字段
    这样配置文件会将/etc/supervisor/conf.d下所有.conf结尾的都会导入进来
[include]
files = /etc/supervisor/conf.d/*.conf
  1. 在创建一个配置文件到/etc/supervisor/conf.d/johnkee.conf
[program:www]
command=/home/www/venv/bin/gunicorn hello:app
directory=/home/www/
user=root
autostart=true
autorestart=true
stdout_logfile=/home/www/logs/supervisor_out.log
stderr_logfile=/home/www/logs/supervisor_err.log
  1. reread来检测修改的配置内容, update来更新
supervisorctl reread
supervisorctl update
  1. 修改falsk文件后重从加载flask文件
supervisorctl restart www
  1. 生成requirements.txt文件(在激活本工程虚拟环境下)
pip freeze > requirements.txt
  1. 在系统自己新建的虚拟环境下安装requirements.txt依赖
pip install -r requirements.txt
pip uninstall urllib3 && pip uninstall requests && pip uninstall chardet && pip install --upgrade --force-reinstall 'requests==2.6.0' urllib3

问题解决办法

certbot证书过期后如何更新证书并生效

certbot renew
nginx -s reload
上一篇下一篇

猜你喜欢

热点阅读