刚开始学习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)
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
即可 在公网上 访问你的 项目