码迷,mamicode.com
首页 > 其他好文 > 详细

Centos7部署Flask

时间:2019-07-17 16:46:55      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:public   imp   端口   vat   默认   out   mod_wsgi   load   strong   

一、安装Apache

# yum install -y httpd httpd-devel
# systemctl start httpd.service        # 启动
# systemctl stop httpd.service         # 关闭
# systemctl restart httpd.service      # 重启
# systemctl enable httpd.service       # 开机自启

防火墙开放80端口

# firewall-cmd --zone=public --add-port=80/tcp --permanent
# firewall-cmd --reload

开启Apache,外网已经可以通过ip访问Apache的默认页面了

二、安装Python36、pip3、virtualenv

# yum install -y epel-release
# yum install -y python36
# python36 -V
Python 3.6.6
# yum install -y python36-setuptools
# easy_install-3.6 pip
# pip3 -V
pip 18.1 from /usr/local/lib/python3.6/site-packages/pip-18.1-py3.6.egg/pip (python 3.6)
# pip3 install virtualenv

三、创建项目

创建flask项目(最简单的,一个项目文件夹、一个启动文件)

# mkdir /var/www/oneflask        # 项目文件夹
# vi /var/www/oneflask/app.py    # 启动文件

启动文件例子: 

from flask import Flask, request

app = Flask(__name__)
 
@app.route(/)
def hello_world():
    return Hello World
 

@app.route(/hello)
def hello():
    name = request.args.get(name,‘‘)
    return Hello  + name + !

if __name__ == __main__:

    app.run()

在项目文件夹下创建虚拟环境,安装flask

# cd /var/www/oneflask
# virtualenv py3env            # 创建虚拟环境
# source py3env/bin/activate   # 进入虚拟环境
(py3env) # pip install flask   # 安装flask
(py3env) # deactivate          # 退出虚拟环境

四、在虚拟环境中用pip安装mod_wsgi

# yum install -y python36-devel.x86_64     # 一个依赖,不安装的话,下面pip会报错。。
# source py3env/bin/activate               # 进入虚拟环境
(py3env) # pip install mod_wsgi            # 安装mod_wsgi
(py3env) # mod_wsgi-express install-module # 执行该命令,把输出内容复制下来
LoadModule wsgi_module "/usr/lib64/httpd/modules/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so"
WSGIPythonHome "/var/www/oneflask/py3env"

(py3env) # deactivate                      # 退出虚拟环境

修改Apache的配置

# vi /etc/httpd/conf/httpd.conf
复制上面得到的这行内容,添加到配置文件的最后
LoadModule wsgi_module "/usr/lib64/httpd/modules/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so"

五、配置mod_wsgi

 

# vi /var/www/oneflask/app.wsgi

 

写入以下内容

activate_this = /var/www/oneflask/py3env/bin/activate_this.py
with open(activate_this) as file_:
    exec(file_.read(), dict(__file__=activate_this))

import sys
sys.path.insert(0, /var/www/oneflask)
from app import app as application

六、配置Apache

# vi /etc/httpd/conf/httpd.conf

写入以下内容

<VirtualHost *:80>
    ServerName example.com
    WSGIScriptAlias / /var/www/oneflask/app.wsgi 
    <Directory /var/www/oneflask>
        Require all granted
    </Directory>
</VirtualHost>

重启Apache

service httpd restart

配置完成

 

Centos7部署Flask

标签:public   imp   端口   vat   默认   out   mod_wsgi   load   strong   

原文地址:https://www.cnblogs.com/hanbinglengxie/p/11201841.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!