目的
他のページでこの構成でウェブアプリをデプロイする時の解説があったのですがよくわからなくて結局公式リファレンスを見たのでまとめます。
世界一簡単すぎて理解することができないかもしれませんが許してください。
nginx.confとuwsgi.iniくらいは参考になるかもしれません。
前提
・ nginxがインストールされている。
・ python3がインストールされている。
・ flaskがインストールされている。
・ uwsgiがインストールされている。
nginxの設定
nginxの設定に以下を追加
nginx.conf
server{
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:3031;
}
}
uWsgiの設定
uwsgiの設定ファイルを以下の通りにします。
uwsgi.ini
[uwsgi]
master = true
socket = 127.0.0.1:3031
wsgi-file = app.py
callable = app
processes = 4
threads = 2
stats = 127.0.0.1:9191
touch-reload=/path/to/application/reload.trigger
pidfile=/path/to/application/app.pid
flaskアプリの開発
pythonのプログラムを記述します。
app.py
from flask import Flask, render_template, url_for, request, Response, jsonify, redirect, send_from_directory
app = Flask(__name__)
@app.route('/')
def rootDir():
return render_template('index.html')
Flaskの起動
サーバの起動は以下のコマンドでできます。
$ uwsgi uwsgi.ini
バックグラウンドモードは以下です。
$ uwsgi uwsgi.ini &
また、バックグラウンドモードの際にapp.pyを編集した後などに再起動する場合は
$ touch reload.trigger
でできます。
nginxの起動
起動
$ sudo service nginx start
再起動
$ sudo nginx -s reload
停止
$ nginx -s stop