LoginSignup
1
1

More than 1 year has passed since last update.

FlaskをGunicornで稼働させてみた

Posted at

はじめに

こちらの記事の続きで書いてます。
Gunicornメインで、Flaskにはほぼ触れない内容かも。

本番サーバーで稼働させる

本番稼働させるには、gunicornuWSGIといったWEBサーバーを使います。
今回は、gunicornを使ってWEB公開してみます。

gunicornをインストール

gunicornはpipでインストールできます。

# pip install gunicorn

gunicornで起動させる

実行させたいflaskのファイルがあるディレクトリで、以下のコマンドを実行します。
-wは、--workersの省略形で、ワーカープロセス数を指定できる。
-bは、--bindの省略形で、gunicornのアドレスと待受け(Listen)ポートを指定できる。
index.pyのappというアプリケーションを実行します。

# gunicorn -w 1 -b 0.0.0.0:80 index:app
2022-09-18 15:52:49 +0000] [30] [INFO] Starting gunicorn 20.1.0
[2022-09-18 15:52:49 +0000] [30] [INFO] Listening at: http://0.0.0.0:80 (30)
[2022-09-18 15:52:49 +0000] [30] [INFO] Using worker: sync
[2022-09-18 15:52:49 +0000] [31] [INFO] Booting worker with pid: 31

バックグラウンド起動(デーモン起動)させる場合はこちら
※dockerfileのCMDやENTRYPOINTでこれを実行させる場合は、--daemonは書かないこと。
 原因追求まで出来てないですが、CMDやENTRYPOINTでデーモン起動させるとコンテナが落ちます。
 (restart設定にしている場合は、restartingを繰り返します)

# gunicorn --workers 1 --bind 0.0.0.0:80 --daemon index:app

なんかまだ書き足りてない気がするので、気が向いたときに追記します。

1
1
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
1
1