4
3

More than 1 year has passed since last update.

flaskを本番環境で起動する uWSGI + Nginx

Last updated at Posted at 2022-02-02

はじめに

Flaskは簡単にテストサーバーで起動することができますが、これをNginxとuWSGIで本番起動するためのメモです。
下記サイトを参考にしていますので、詳細はこちらをご覧ください。
https://serip39.hatenablog.com/entry/2020/07/06/070000
また、ラズベリーパイで作成していますのでユーザー名がpiになっています。

uWSGIとFlaskをインストールして仮想環境を立ち上げ

pipをアップグレードしてpipenvをインストール

$ pip3 install --upgrade pip 
$ pip3 install pipenv
$ export PATH=$PATH:/home/pi/.local/bin 

ディレクトリを作成し、uWSGI・Flaskをインストール後、仮想環境を実行

$ mkdir ./flask-test
$ cd ./flask-test
$ pipenv install --python 3.9.2 #任意のバージョンで

$ pipenv install uwsgi    # 仮想環境にuwsgiをインストール
$ pipenv install flask    # 仮想環境にflaskをインストール
$ pipenv shell #仮想環境を実行

uWSGI起動テスト

ディレクトリ直下にtest.pyを作成

pi/flask-test/test.py
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello uWSGI"]

uWSGIを起動

(flask-test)pi$ uwsgi --http :8000 --wsgi-file test.py

http://127.0.0.1:8000 にアクセスして「Hello uWSGI」が表示されていればOK

Flask起動テスト

ディレクトリ直下にapp.pyを作成

pi/flask-test/app.py
from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello Flask"

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

Flaskテストサーバーを起動

(flask-test)pi$ python app.py

uWSGIを通してFlaskを表示

(flask-test)pi$ uwsgi --http :8000 --wsgi-file app.py --callable app

http://127.0.0.1:8000 にアクセスして 「Hello Flask」が表示されればOK

Nginx起動テスト

一旦仮想環境からログアウトしてNginxをインストール

(flask-test) pi $exit    #exitして仮想環境から出る
pi$ sudo apt install nginx
pi$ sudo /etc/init.d/nginx start 
[ ok ] Starting nginx (via systemctl): nginx.service.

http://127.0.0.1:80にアクセスして 「Welcome to nginx!」が表示されていればOK
ローカル内の他のPCからのアクセスしてみても良い
※その場合はhttp://(ここにラズパイのIPアドレス)でアクセス

Nginxのバーチャルホスト設定を行う

$ cd /etc/nginx/sites-available
$ sudo cp default ./app_nginx.conf

/var/www/に80番ポートでアクセスしたときに、uWSGIに接続するように設定

app_nginx.conf
# Virtual Host configuration for example.com
server {
        listen 80;
        server_name example.com;        
        location / {
          include uwsgi_params;
          uwsgi_pass unix:/tmp/uwsgi.sock;
        }
}

uWSGI設定ファイル作成

flask-test
 |- app.py
 |- test.py
 |- myapp.ini

myapp.iniディレクトリ直下に作成

myapp.ini
[uwsgi]
module = app
callable = app
master = true
processes = 1
socket = /tmp/uwsgi.sock
chmod-socket = 666
vacuum = true
die-on-term = true
chdir = /home/pi/flask-test

※/home/pi/flask-testは任意のディレクトリを指定

NginxとuWSGIを接続して、動作確認

uWSGIサーバー立ち上げ

$ uwsgi --ini myapp.ini

別のターミナルでNginxを起動

$ sudo /etc/init.d/nginx start

http://127.0.0.1:80 にアクセスして「Hello Flask」と表示されれば完成!

自動起動設定

/etc/systemd/system/内にuwsgi.serviceファイルを作成

/etc/systemd/system/uwsgi.service
[Unit]
Description = uWSGI
After = syslog.target
[Service]
ExecStart = [uwsgiのパス] --ini /home/pi/flask-test/myapp.ini
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all
[Install]
WantedBy=multi-user.target

※[uwsgiのパス]は仮想環境上で $ which flask と入力して表示されるパスを入れてください

下記を実行して自動起動設定

systemctl enable uwsgi

コマンドメモ

systemctlのコマンド

$ systemctl status uwsgi //status確認
$ systemctl start uwsgi // uWSGI開始
$ systemctl stop uwsgi // uWSGI停止
4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3