flask 部署

时间:2019-11-17 16:55:54   收藏:0   阅读:113

项目部署

基于ubuntu 16.04系统,使用 Gunicorn + Nginx 进行布署

技术分享图片

技术分享图片技术分享图片

技术分享图片

ssh 用户名@ip地址

相关环境安装

以下操作都在远程服务器上进行操作 (ubuntu 16.04)

sudo apt-get update
apt-get install mysql-server
apt-get install libmysqlclient-dev
sudo apt-get install redis-server
pip install virtualenv
pip install virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/workspace
source /usr/local/bin/virtualenvwrapper.sh
source ~/.bashrc

requirements文件

Python 项目中可以包含一个 requirements.txt 文件,用于记录所有依赖包及其精确的版本号,以便在新环境中进行部署操作。

pip freeze > requirements.txt
pip install -r requirements.txt
sudo apt-get build-dep python-mysqldb

Nginx (软件)

相关操作

$ sudo apt-get install nginx
/etc/init.d/nginx start #启动
/etc/init.d/nginx stop  #停止

启动失败大概率是因为配置文件错误

# 如果是多台服务器的话,则在此配置,并修改 location 节点下面的 proxy_pass 
upstream flask {
        server 127.0.0.1:5000;
        server 127.0.0.1:5001;
}
server {
        # 监听80端口
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/html;

        index index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
                # 请求转发到gunicorn服务器
                proxy_pass http://127.0.0.1:5000;
                # 请求转发到多个gunicorn服务器
                # proxy_pass http://flask;
                # 设置请求头,并将头信息传递给服务器端 
                proxy_set_header Host $host;
                # 设置请求头,传递原始请求ip给 gunicorn 服务器
                proxy_set_header X-Real-IP $remote_addr;
        }
}

Gunicorn(python拓展包)

相关操作

pip install gunicorn
gunicorn -h
# -w: 表示进程(worker) -b:表示绑定ip地址和端口号(bind)
gunicorn -w 2 -b 127.0.0.1:5000 运行文件名称(注意只是名称 没有.py的后缀):Flask程序实例名

启动失败大概率是因为解释器或者环境问题

参考阅读: Gunicorn相关配置:https://blog.csdn.net/y472360651/article/details/78538188

其他操作

scp -r 本地文件路径 root@39.106.21.198:远程保存路径

数据库模板文件迁移
 

原文:https://www.cnblogs.com/xujin247/p/11876876.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!