ApacheでPython Flaskのアプリを動かす(途中)

ApachePython Flaskのアプリを動かす

apacheにmod_wsgi入れるまでは別途。
windows xp でならここなど。

flaskのインストールは本家http://flask.pocoo.org/

文字コードutf-8。日本語もだしたいお!

flaskの簡単アプリを用意する

C:\python\flask\myapp フォルダ(場所はどこでもいい)

hoge.py
\static\styles.css
\templates\layout.html
\templates\list.html


hoge.py

# -*- coding:utf-8 -*-

from flask import Flask,render_template
app = Flask(__name__)

def get_resource_as_string(name, charset='utf-8'):
    with app.open_resource(name) as f:
        return f.read().decode(charset)

app.jinja_env.globals['get_resource_as_string'] = get_resource_as_string

@app.route("/list")
def list():
    books = ["book1","book2","book3",u"本4"]
    return render_template("list.html",items=books)

@app.route("/")
def hello():
    return u"Hello World! へろおぱいそん"

if __name__ == "__main__":
    app.debug = True
    app.run()

\static\styles.css

body{
  background-color:beige;
}

\templates\layout.html

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type=text/css>{{ get_resource_as_string('static/styles.css') }}</style>
{% block head %}{% endblock %}
</head>
<body>

{% block body %}{% endblock %} 
</body>
</html>

\templates\list.html

{% extends "layout.html" %}
{% block body %}
りすと
<ul>
{% for item in items %}
<li>{{ item }}</li>
{% endfor %}
</ul>
{% endblock %}

別途 hoge.wsgi というファイルをつくる
これはとりあえず C:\python\flask\myapp におく。
ここじゃないといけないのかよくわからない

# -*- coding:utf-8 -*-
 
import sys, os
sys.path.append('D:/python/flask/myapp')
 
from hoge import app as application

次はapache側の設定
やり方は色々あるのだろうけど
httpd.confの vhostsのところをコメントをはずす(先頭のシャープをはずす)

Include conf/extra/httpd-vhosts.conf

extra/httpd-vhosts.confに以下を追加

WSGIRestrictStdout Off
<virtualhost *:80>
    ServerName example.com
    
    #WSGIDaemonProcess example.com user=user1 group=group1 threads=5
    WSGIScriptAlias / D:/python/flask/myapp/hoge.wsgi
    
    <Directory D:/python/flask/myapp>
        #WSGIProcessGroup example.com
        WSGIApplicationGroup %{GLOBAL*}
        Order deny,allow
        Allow from all
        WSGIScriptReloading On
    </Directory>
    ErrorLog "logs/flask-error.log"
    CustomLog "logs/flask-access.log" common
</virtualhost>

apachehoge.wsgiを指定して、hoge.wsgi

sys.path.append('D:/python/flask/myapp')

で、アプリを指定する感じなのかな?

これで動くっちゃあ動く。
コメントしているのは、他のサンプルでみたけど何だかエラーになったのでとめた。

で。
ここが問題。

    WSGIScriptAlias / D:/python/flask/myapp/hoge.wsgi

これは要するにサイトのトップからの指定

http://127.0.0.1/
で、@app.route("/") の内容が動くし
http://127.0.0.1/list で@app.route("/list")

これを変更してみる

    #WSGIScriptAlias / D:/python/flask/myapp/hoge.wsgi
    WSGIScriptAlias /hoge/ D:/python/flask/myapp/hoge.wsgi

http://127.0.0.1/hoge/
で、トップが動くようにしたい。これはOK。
しかし
http://127.0.0.1/hoge/list
だと、動かない!


えええええええ???

ここまできてそれかい
みたいな感じ。

mod_rewrite?とか、proxyとかさがしたり、djangoもなんかありそうだけど
うごかないよー。
rubyapacheの連携みたいな感じで何かあいだにかますのかな?

便利なんだかどうなんだかよくわからねえよーぅ。