次のページを参考にしました。
Flask + uWSGI + Nginx でハローワールドするまで @ さくらのVPS (CentOS 6.6)
私は、これを Arch Linux で行い、かつ uWSGI は手動で起動しました。
Flask のプログラム
hello.py
# ------------------------------------------------------------------
#
# hello.py
#
# Nov/09/2018
# ------------------------------------------------------------------
from flask import Flask
app = Flask(__name__)
# ------------------------------------------------------------------
@app.route("/")
def hello():
str_out = "<h2>Hello World!</h2>"
str_out += "こんにちは。<p />"
str_out += "Nov/09/2018 PM 16:51<p />"
return str_out
# ------------------------------------------------------------------
if __name__ == "__main__":
app.run()
#
# ------------------------------------------------------------------
これを、ビルトインサーバーで起動
python hello.py
ブラウザーで、http://127.0.0.1:5000/ にアクセス
uWSGI で起動
uwsgi --plugin http,python --http :9080 --wsgi-file hello.py --callable app
ブラウザーで、http://127.0.0.1:9080/ にアクセス
次に、uWSGI と Nginx をつなぎます。
Nginx の設定
/etc/nginx/nginx.conf
location / {
include uwsgi_params;
uwsgi_pass unix:///tmp/uwsgi.sock;
}
Nginx の起動
sudo systemctl start nginx
Nginx が起動しているか確認
sudo systemctl status nginx
次に uWSGI をマニュアルで起動
uwsgi --plugin http,python --socket /tmp/uwsgi.sock --wsgi-file hello.py --callable app --chmod-socket=666
ブラウザーで、http://127.0.0.1/ にアクセス
この次には、uWSGI をサービスにして、systemd で起動できるようにする必要があります。
ini ファイルを使って、uWSGI を起動するようにします。
/home/uchida/flask/hello/hello.ini
[uwsgi]
wsgi-file = /home/uchida/flask/hello/hello.py
callable = app
master = true
processes = 1
socket = /tmp/uwsgi.sock
chmod-socket = 666
vacuum = true
die-on-term = true
plugin = http,python
uWSGI の起動
uwsgi --ini /home/uchida/flask/hello/hello.ini
ブラウザーで、http://127.0.0.1/ にアクセス
サービスの作成
/etc/systemd/system/hello.service
[Unit]
Description=uWSGI instance to serve hello
After=network.target
[Service]
User=uchida
Group=wheel
WorkingDirectory=/home/uchida/flask/hello
Environment="PATH=/usr/bin"
ExecStart=/usr/bin/uwsgi --ini hello.ini
[Install]
WantedBy=multi-user.target
サービスの起動
sudo systemctl start hello
サービスの起動を確認
sudo systemctl status hello
ブラウザーで、http://127.0.0.1/ にアクセス
これで、完了です。
hello.py を編集したら、サーバーで
sudo systemctl daemon-reload
sudo systemctl restart hello