博客
关于我
如何在 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/

    你可能感兴趣的文章
    python写txt文件
    查看>>
    python关于sorted排序函数详细解析 附实战代码(全)
    查看>>
    python3.8调用m文件,调用matlab
    查看>>
    python3.9新特性_Python 3.9 新特性速览
    查看>>
    Python3.x:遍历select下拉框获取value值
    查看>>
    python360百科_11月编程排行榜:Python超过Java成第二
    查看>>
    Python关于Paginator分页函数详细解析 附实战代码
    查看>>
    python3、ipython3、setup-tools、pip等环境搭建详细总结
    查看>>
    python3下使用cv2.imwrite存储带有中文路径图片或者绝对路径图片
    查看>>
    python3与django中@property详解
    查看>>
    python3中如何表示无穷大
    查看>>
    Python3中提示:no module named 'wxpy'
    查看>>
    python3中选择文件对话框的格式打开和保存图片
    查看>>
    python3为什么print永远都是地址_Python3 print 函数用法总结
    查看>>
    Python3之uuid模块
    查看>>
    Python3之写入Excel文档(xlsxwriter)
    查看>>
    Python3之打印操作打印流重定向(下)
    查看>>
    python3判断字典、列表、元组为空以及字典是否存在某个key的方法
    查看>>
    Python3利用pymysql操作 MySQL
    查看>>
    python3处理json文件中含有中文dumps的应用
    查看>>