ʻApache` runs the flow to run the flask application.
With Werkzeug
, it's pretty easy to start, but
ʻApache+
mod_wsgi` raises the hurdle as soon as it becomes, so I summarized it roughly.
Since all are for mac, please read the path etc. as appropriate.
Enter virtualenv referring to this area. http://qiita.com/k2tanaka/items/5f111612ec1b6d7584a6
mkvirtualenv -p ~/.pythonz/pythons/CPython-2.7.6/bin/python2.7 v2.7.6
workon and pip
up to flask.
workon v2.7.6
pip install flask
The contents are very suitable. For the time being, with a minimum example.
Make sure to match the file path with the conf of ʻapache` after this.
/Users/kuryu/workspace/test/flask
├── app.wsgi
├── app_templates
│ └── index.html
└── main.py
app.wsgi
# -*- coding:utf-8 -*-
import sys, os
import logging
#Needed to log apache
logging.basicConfig(stream = sys.stderr)
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
from main import app as application
main.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import os
from flask import Flask, render_template
from jinja2 import FileSystemLoader
app = Flask(__name__)
#Try changing the template directory of jinja2
#It doesn't have to be separate.
#If omitted, the same hierarchy as this file"templates"become.
app.jinja_loader = FileSystemLoader(
os.path.join(os.path.abspath(os.path.dirname(__file__)), 'app_templates')
)
@app.route("/")
def index():
from flask import render_template
return render_template('index.html')
@app.route("/foo")
def foo():
return "foo"
@app.route("/foo/bar")
def foo_bar():
return "foobar"
if __name__ == "__main__":
app.run()
app_templates/index.html
hogehoge
mod_wsgi
Is omitted.
Maybe linux comes in with ʻapt or
yum, or it comes with ʻapache
from the beginning.
on mac
brew tap homebrew/apache
brew install mod_wsgi
do it
brew info mod_wsgi
If you do, you will know the pass.
Since vitualenv is used, specify the location of python
with WSGIPythonHome.
flask.conf
## mod_wsgi
LoadModule wsgi_module /usr/local/Cellar/mod_wsgi/3.5/libexec/mod_wsgi.so
WSGIPythonHome /Users/kuryu/.virtualenvs/v2.7.6
WSGIDaemonProcess test user=kuryu group=staff threads=5
WSGIScriptAlias /test /Users/kuryu/workspace/test/flask/app.wsgi
WSGISocketPrefix /var/run/wsgi
<Directory /Users/kuryu/workspace/test/flask>
Options +ExecCGI
SetHandler wsgi-script
AddHandler wsgi-script .wsgi
Order deny,allow
Allow from all
</Directory>
ʻApache` Reboot.
sudo apachectl restart
Since / test
is specified by WSGIScriptAlias, the url is like this.
curl localhost/test/
curl localhost/test/foo
curl localhost/test/foo/bar
that's all.
90% of the causes of annoyance are ʻapache` ...
Recommended Posts