博客
关于我
如何在 CentOS 6.5 上部署 Flask
阅读量:687 次
发布时间:2019-03-17

本文共 2168 字,大约阅读时间需要 7 分钟。

#在CentOS 6.5中部署Flask

在实际开发过程中,Flask内置的Web服务器已经能够满足日常需求,但在移动到服务器上时,需要使用符合WSGI协议的专用Web服务器。Nginx + uWSGI是最常见的组合之一。网络上有大量教程,但大多仅提供命令而未深入解释关键点。本文将详细介绍在CentOS 6.5上部署Flask的完整过程,并强调一些关键要点。

##安装Python 3.6

CentOS 6.5自带的Python版本为2.6.6。为了避免与系统默认Python冲突,我们使用virtualenv来隔离Python 3.6的环境。

###安装步骤

  • 下载Python 3.6.1:

    wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tar.xz
  • 解压并安装:

    tar xJf Python-3.6.1.tar.xzcd Python-3.6.1./configure --prefix=/usr/local/python3makemake install
  • 创建软链接:

    ln -s /usr/local/python3/bin/python3 /usr/bin/python3ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
  • 确保创建了python3和pip3的软链接,以区分Python 2和Python 3。接下来需要使用Python 3的pip来安装virtualenv。

    ##安装virtualenv

    安装virtualenv以管理Python包:

    pip3 install virtualenv

    ##创建Python虚拟环境

    在指定目录下创建虚拟环境,例如~/py36venv

    /usr/local/python3/bin/virtualenv -p /usr/bin/python3 venv

    取得虚拟环境后,进入该目录并激活它:

    source venv/bin/activate

    激活后,关闭虚拟环境的方法是:

    deactivate

    ##安装Flask

    在虚拟环境中安装Flask:

    pip install flask

    ##安装Nginx

    Nginx有两种安装方式:使用yum和源码安装。这里使用yum安装:

    yum install nginx

    如果yum install nginx失败,请先安装EPEL:

    # CentOS Linux v6.x用户运行以下命令:wget http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpmrpm -ivh nginx-release-centos-6-0.el6.ngx.noarch.rpm

    ##查看Nginx配置

    确认Nginx版本和配置:

    nginx -V

    Nginx的主配置文件在/etc/nginx/nginx.conf,内容包括:

    include /etc/nginx/conf.d/*.conf;

    这表示Nginx会加载/etc/nginx/conf.d下的所有配置文件。接下来我们将创建支持WSGI的配置文件。

    ##启动和停止Nginx

    # 启动service nginx start# 停止service nginx stop# 重新启动service nginx restart# 重新加载service nginx reload

    启动后,访问http://localhost查看初始界面。

    ##在虚拟环境中安装uWSGI

    安装uWSGI:

    pip install uwsgi

    ##测试uWSGI

    创建一个测试文件test.py

    def application(env, start_response):    start_response('200 OK', [('Content-Type', 'text/html')])    return [b'Hello World']

    运行uWSGI测试:

    uwsgi --http :9090 --wsgi-file test.py

    在浏览器中输入http://localhost:9090,如果能显示Hello World,说明uWSGI配置正确。

    ###uWSGI启动配置

    config.ini中配置:

    [uwsgi]socket = 127.0.0.1:8001chdir=/root/flask_restful_mysqlwsgi-file=manage.pycallable=appprocesses=4threads=2buffer-size=32768stats=127.0.0.1:9191

    启动uWSGI并保持运行:

    nohup uwsgi config.ini &

    关闭SSH时,不要忘记终止uWSGI进程:

    killall -9 uwsgi

    查看进程:

    ps -ef | grep uwsgi

    ##源代码如何上传到Linux

    通过WinSCP等工具将源代码上传或使用自动化工具如Fabric来实现持续部署。这个部分不在本文详细展开。

    转载地址:http://qpthz.baihongyu.com/

    你可能感兴趣的文章
    Nokia5233手机和我装的几个symbian V5手机软件
    查看>>
    non linear processor
    查看>>
    Non-final field ‘code‘ in enum StateEnum‘
    查看>>
    none 和 host 网络的适用场景 - 每天5分钟玩转 Docker 容器技术(31)
    查看>>
    None还可以是函数定义可选参数的一个默认值,设置成默认值时实参在调用该函数时可以不输入与None绑定的元素...
    查看>>
    NoNodeAvailableException None of the configured nodes are available异常
    查看>>
    Vue.js 学习总结(16)—— 为什么 :deep、/deep/、>>> 样式能穿透到子组件
    查看>>
    nopcommerce商城系统--文档整理
    查看>>
    NOPI读取Excel
    查看>>
    NoSQL&MongoDB
    查看>>
    NoSQL介绍
    查看>>
    NoSQL数据库概述
    查看>>
    Notadd —— 基于 nest.js 的微服务开发框架
    查看>>
    NOTE:rfc5766-turn-server
    查看>>
    Notepad ++ 安装与配置教程(非常详细)从零基础入门到精通,看完这一篇就够了
    查看>>
    Notepad++在线和离线安装JSON格式化插件
    查看>>
    notepad++最详情汇总
    查看>>
    notepad++正则表达式替换字符串详解
    查看>>
    notepad如何自动对齐_notepad++怎么自动排版
    查看>>
    Notes on Paul Irish's "Things I learned from the jQuery source" casts
    查看>>