您的位置:首页 > 运维架构 > Apache

Apache + Flask + mod_wsgi部署

2016-07-22 16:56 716 查看
原文地址:http://www.isaced.com/post-238.html


安装 mod_wsgi

如果服务器是用的Apache,那么Flask官方推荐用
mod_wsgi
,文档可以戳这,其实Flask官方文档已经写的很清楚了,我还是贴一下吧。

Ubuntu or Debian:
# apt-get install libapache2-mod-wsgi


修改Apache配置:

然后修改
/etc/apache2/sites-enabled/000-default

WSGIPythonPath /home/isaced/test
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /home/isaced/test/
WSGIScriptAlias / /home/isaced/test/app.wsgi
<Directory /home/isaced/test/>
<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>


添加.wsig文件

然后在isaced目录下新建项目目录”
test
“,其中新建文件
app.wsgi
,内容如下:
def application(environ,start_response):
status='200 OK'
output='Hello wsgi!'
response_headers=[('Content-type','text/plain'),
('Content-Length',str(len(output)))]
start_response(status,response_headers)
return[output]


赶紧试试

重启Apache试试,看看效果吧:
sudo /etc/init.d/apache2 restart


浏览器打开“
http://xxx.kd.io/
”,就会输出“Hello
wsgi!”。


启动Flask

按耐住小鸡动,我们继续来启动一个flask实例。

新建
test.py
文件,作为flask入口文件,内容如下:
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')


然后修改
app.wsgi
文件内容为:
from test import app as application


这里的test就是当前目录的test.py文件,看到网上很多文章还要import sys,再append当前目录,其实如果在同一目录下的话就不需要了。
Flask官方配置文档:http://dormousehole.readthedocs.io/en/latest/deploying/mod_wsgi.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  apache flask