FlaskをUbuntu上のApacheにデプロイする方法
今後のためのメモ。Djangoのデプロイの仕方はなんとなく馴染んだけどFlaskのやり方がいまいち分からなかったので出来るようになってみた。
ついでにアプリごとにサブドメイン切ってみる。パフォーマンスとかは全然考えていない。
条件てきなもの
- app置き場
- /mnt/www/apps/myApp
- domain
- myapp.example.com
Apache設定
/etc/apache2/sites-available/myapp
<VirtualHost *:80> ServerName myapp.example.com ServerAdmin a2c@example.com Alias /static /mnt/www/apps/myApp/static WSGIScriptAlias / /mnt/www/apps/myApp.wsgi </VirtualHost>
/mnt/www/apps/myApp.wsgi
#!/usr/bin/env python
#cording:utf-8
import os, sys
sys.path.append('/mnt/www/apps')
sys.path.append('/mnt/www/apps/myApp')
from myApp import monitor
monitor.start(interval=3.0)
from myApp.main import app as application
myApp設定
- 直下に__init__.pyを作成する(空ファイルでOK)
- monitor.pyを作成する(以下ソース参照)
- main.pyにapp作る
フォルダー構成
myApp
|-- main.py
|-- monitor.py
|-- static
| |-- css
| | `-- core.css
| `-- js
| `-- myApp.js
| `-- jquery-1.4.2.js
`-- templates
|-- layout.html
`-- top.htmlmain.py
#!/usr/bin/env python # coding:utf-8 from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash, jsonify app = Flask(__name__) app.debug = 1 @app.route('/') def render_html(): ret = {'title':this is myApp'} return render_template('index.html', p=ret) if __name__ == '__main__': app.run(host='0.0.0.0', port=9091)
index.html
{% extends "layout.html" %}
{% block head %}
<script type="text/javascript" src="/static/js/myApp.js"></script>
{% endblock %}
{% block body %}
<body>
<div id=content>
{{ p.title }}
</div>
</body>
{% endblock %}
layout.html
<!doctype html><head> <title>{{ p.title }}</title> <link rel=stylesheet type=text/css href="{{ url_for('static', filename='css/core.css') }}"> <script type="text/javascript" src="/static/js/jquery-1.4.2.js"></script> {% block head %}{% endblock %} </head> {% block body %}{% endblock %} </html>
siteのenable
あとはサイトを有効にしてアパッチリロード
sudo a2ensite myapp
sudo /etc/init.d/apache2 reload
これで、終了
myapp.example.comにアクセスすれば開発サーバーと同じようなパスでアクセスできるし
ソースを書き換えれば3秒以内にサイトに反映される。快適快適
もっとこんな風にした方がええよ!みたいなの教えてくださいお願いします。
siteの書き方全然わからない・・・