腾讯云部署flask

1.环境配置及hello

1.安装nginx

  • 安装
$ 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;
        }
}

2.安装gunicorn

  • 安装
pip install gunicorn
  • 查看选项
gunicorn -h
  • 运行
# -w: 表示进程(worker) -b:表示绑定ip地址和端口号(bind)
gunicorn -w 2 -b 127.0.0.1:5000 运行文件名称:Flask程序实例名

3.代码拷贝

  • 拷贝本地代码到远程
scp -r 本地文件路径 root@39.106.21.198:远程保存路径

2.实例代码

1.hello.py

from flask import Flask

# 创建Flask的应用程序
# 第一个参数指代Flask所对应的模板,其可以决定静态文件从哪个位置开始找
app = Flask(__name__)

# 使用装饰器路由去与视图函数进行关联
@app.route('/')
def index():
    return 'hello 2019'

if __name__ == '__main__':
    # 运行当前Flask应用程序
    app.run()

2.启动gunicorn

gunicorn -w 2 -b 127.0.0.1:5000 hello:app

启动之后显示如下:

(flask_hello) ubuntu@VM-0-5-ubuntu:~$ gunicorn -w 2 -b 127.0.0.1:5000 hello:app
[2019-03-09 16:06:10 +0800] [19417] [INFO] Starting gunicorn 19.9.0
[2019-03-09 16:06:10 +0800] [19417] [INFO] Listening at: http://127.0.0.1:5000 (19417)
[2019-03-09 16:06:10 +0800] [19417] [INFO] Using worker: sync
[2019-03-09 16:06:10 +0800] [19420] [INFO] Booting worker with pid: 19420
[2019-03-09 16:06:10 +0800] [19421] [INFO] Booting worker with pid: 19421

3.访问

此时打开你的公网ip就可以正常显示return的数据了,ok

下面来部署一个真实的flask项目

2.部署项目

1.先创建config.py文件里的数据库

注意指定数据库utf8

2.在manage.py同级目录下迁移数据

$ python manage.py db init
$ python manage.py db migrate -m"initial"
$ python manage.py db upgrade

3.登录数据库,source

4.启动nginx,gunicorn

/etc/init.d/nginx stop  #停止
/etc/init.d/nginx start #启动
gunicorn -w 2 -b 127.0.0.1:5000 manage:app
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值