在生产环境上 (ubuantu)flask +apache+mod_wsgi 运行你的flask

           刚开始学习flask  我直接是在我的自己的阿里云上做测试学习。由于阿里云是生产环境 app.run  只适合调试模式,官网介绍应该使用uWsgi或者 Gunicorn 。我的服务器由于已经运行的php 项目是 用apache做的服务器。需要做一些简单的配置 才能在外网能访问你的flask 测试程序

官网的 文档是:http://docs.jinkan.org/docs/flask/deploying/mod_wsgi.html


新建的hello.py内容如下:
#conding=utf-8
from flask import Flask
app=Flask(__name__)
@app.route('/')
def hello_world():
        return 'Hello world!'


if __name__=='__main__':
        #app.run(host=‘0.0.0.0’,port=9000)
        app.run(host='0.0.0.0',port=5000,debug=True)


 以下配置apche 
                                                  
1   apt-get install libapache2-mod-wsgi

2配置apache   /etc/apache2/sites-enabled/ 000-default.conf 加入如下

WSGIPythonPath /home/chenglong/pytest/flasktest                                                                               
<VirtualHost *:5000>
    ServerAdmin  webmaster@localhost
    DocumentRoot /home/chenglong/pytest/flasktest
    WSGIScriptAlias / /home/chenglong/pytest/flasktest/hello.wsgi                                                             
<Directory /home/chenglong/pytest/flasktest>                                                                                  
<Files app.wsgi>                                                                                                              
    Order deny,allow
    Allow from all
</Files>
</Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    LogLevel warn
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>


4 在你我的项目目录 (和hello.py 同级目录下)建立 hello.wsgi  文件

如果是用虚拟环境运行 virtualenv  需要加上(
activate_this = '/home/chenglong/pytest/flaskenv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
)这两行 

import sys
activate_this = '/home/chenglong/pytest/flaskenv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
sys.path.insert(0, '/home/chenglong/pytest/flasktest/')

from hello import app as application
                                     
5 service apache2 restart

6source '/home/chenglong/pytest/flaskenv/bin/activate 一下
 python hello.py

即可 在公网上 访问你的 项目   


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值