Flask部署最详细步骤

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 "

Hello There!

" if __name__ == '__main__': app.run()
  1. 在venv激活的情况下安装gunicorn
source /home/www/venv/bin/activate
pip install gunicorn
  • 测试gunicorn是否成功
gunicorn hello:app
netstat -an | grep 80  
  • 去浏览器输入阿里云的公网ip,就可以看到hello.py已经运行
  1. 退出激活状态,安装nginx
yum install nginx
systemctl enable nginx
systemctl start nginx
  • 激活nginx和启动nginx

  • 测试nginx是否安装成功,去浏览器输入阿里云公ip,看网页输出结果

  • 添加nginx配置文件,新版本打开nginx默认配置文件,在里面添加如下内容

vim /etc/nginx/nginx.conf
server {
    listen 80;
    server_name 47.90.99.88;
    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:指定配置文件
nginx -t -c /etc/nginx/nginx.conf
  • 重启nginx, (# service nginx restart)-s:send signal to a master process: stop, quit, reopen, reload
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

问题解决办法

  • error: , [Errno 2] No such file or directory: file: /usr/lib64/python2.7/socket.py line: 224
    

    解决办法

    supervisord
    

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

certbot renew
nginx -s reload

certbot证书过期后报错解决办法:https://certbot.eff.org/docs/install.html

wget https://dl.eff.org/certbot-auto
sudo mv certbot-auto /usr/local/bin/certbot-auto
sudo chown root /usr/local/bin/certbot-auto
sudo chmod 0755 /usr/local/bin/certbot-auto
/usr/local/bin/certbot-auto --help
/usr/local/bin/certbot-auto --install-only 
/opt/eff.org/certbot/venv/bin/certbot

你可能感兴趣的:(部署)