flask项目:项目部署 | flask |《python学习之路》| python 技术论坛-金年会app官方网
部署环境
基于macos 10.15.4系统,使用 gunicorn nginx 进行布署,云服务器为阿里云
服务器选择
阿里云服务器
进入控制台,查看实例创建情况
给安全组配置规则,添加5000端口(一并加上5001端口)
利用命令行进行远程服务器登录
ssh 用户名@ip地址
相关环境安装
以下操作都在远程服务器上进行操作
先更新 apt 相关源
sudo apt-get update
mysql安装
apt-get install mysql-server apt-get install libmysqlclient-dev
redis安装
sudo apt-get install redis-server
安装虚拟环境
pip install virtualenv pip install virtualenvwrapper
使得安装的virtualenvwrapper生效,编辑~/.bashrc文件,内容如下:
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
在安装 flask-mysqldb 的时候可能会报错,可能是依赖包没有安装,执行以下命令安装依赖包:
sudo apt-get build-dep python-mysqldb
nginx
- 采用 c 语言编写
- 实现分流、转发、负载均衡
相关操作
安装
$ sudo apt-get install nginx
运行及停止
/etc/init.d/nginx start #启动 /etc/init.d/nginx stop #停止
配置文件
编辑文件:/etc/nginx/sites-available/default
# 如果是多台服务器的话,则在此配置,并修改 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
- gunicorn(绿色独角兽)是一个python wsgi的http服务器
- 从ruby的独角兽(unicorn )项目移植
- 该gunicorn服务器与各种web框架兼容,实现非常简单,轻量级的资源消耗
- gunicorn直接用命令启动,不需要编写配置文件
相关操作
安装
pip install gunicorn
查看选项
gunicorn -h
运行
# -w: 表示进程(worker) -b:表示绑定ip地址和端口号(bind) gunicorn -w 2 -b 127.0.0.1:5000 运行文件名称:flask程序实例名
参考阅读: gunicorn相关配置:
其他操作
拷贝本地代码到远程
scp -r 本地文件路径 [email protected].198:远程保存路径