FlaskのアプリをuWSGI+Nginxで動かす(Cent OS 6.8)

試した手順。

Flask + uWSGI + Nginx でハローワールドするまで @ さくらのVPS (CentOS 6.6) - Qiita

一応動いた。 ただ、これはforegroundで動かすまでの解説。 backgroundで動かすか自動起動とかになると別途情報が必要。

ここまでで、一応FlaskアプリケーションをNginx経由で動かすことはできた。あとは、起動時に自動実行されるよう設定するなどすればOKだろう(そのためには/etc/rc.localをいじればいい)。

Flask公式の奴

uWSGI — Flask Documentation (0.11)

以下、ちょい訳


uWSGIはプロトコルでもアプリケーションサーバでもある。

uWSGI is both a protocol and an application server

もっとも一般的なuWSGIサーバはuwsgi。ややこしいな。

The most popular uWSGI server is uwsgi,

以下略


uWSGIの公式?

Managing the uWSGI server — uWSGI 2.0 documentation

以下、ちょい訳。


uWSGIを起動するのはシスアドの役目、webサーバみたいに。webサーバにやらせる仕事じゃない、とはいえ自分のアプリのアーキテクチャにフィットするならできるけど。

一番いいのはOSの起動時にuWSGIサービス起動するやりかた。

ダンなOSなら下記のやつが有効。「クラシック」なOSならinit.d/rc.dスクリプトとかSupervisor、Daemontoolsかinetd/xinetdが使える。

System Method
Ubuntu Running uWSGI via Upstart (the official uwsgi package, available since Ubuntu 12.04 provides an init.d based solution. Read the README.)
Debian Systemd
Arch Linux Systemd
Fedora Systemd
OSX launchd
Solaris SMF

uWSGIを制御するシグナル

uWSGIにsafe-pidfile optionをつければmaster process PIDをファイルに書かせることができる。

uWSGIが応じるシグナルは下記の通り。

Signal Description Convenience command
SIGHUP gracefully reload all the workers and the master process --reload
SIGTERM brutally reload all the workers and the master process (use --die-on-term to respect the convention of shutting down the instance)
SIGINT immediately kill the entire uWSGI stack --stop
SIGQUIT immediately kill the entire uWSGI stack
SIGUSR1 print statistics
SIGUSR2 print worker status or wakeup the spooler
SIGURG restore a snapshot
SIGTSTP pause/suspend/resume an instance
SIGWINCH wakeup a worker blocked in a syscall (internal use)
SIGFPE generate C traceback
SIGSEGV generate C traceback

インスタンスを管理するにはシグナルよりも良い方法があって、例えばmaster-fifoみたいなやり方がしっかりとしたやり方。

サーバのリロード

以下略


上記の記述だと、素直に読めばUbuntu以外のLinuxは「Systemd」使えば良いように読める。

で、下記のページ読むと、

Systemd — uWSGI 2.0 documentation

uWSGI is a new-style daemon for systemd.

とか

Adding the Emperor to systemd

とか言ってる。強そう。

Emperorに関しては下記のページ。

The uWSGI Emperor – multi-app deployment — uWSGI 2.0 documentation

Master FIFOに関しては下記のページ。

The Master FIFO — uWSGI 2.0 documentation

リロードに関しては下記のページ。

The Art of Graceful Reloading — uWSGI 2.0 documentation

シグナルの送信に関しては下記のページ。

CodeZine(コードジン)

Man page of SIGNAL

しかしCentOS6.8はモダンではない模様。Emperorは使えるかな?

保存版 CentOS 7とCentOS 6との違いまとめ | urashita.com

CentOS6(CentOS5)と7の差分対応 (SystemVinit/Upstart/systemd) - Opensourcetechブログ(ZeusITCamp裏BLOG)

RHEL6→RHEL7で変更された主要コマンドまとめ — | サイオスOSS | サイオステクノロジー

CentOS6, CentOS7 システムコマンド対応表

第1回 CentOS7の変更点について(CentOS6.6との違い) | Nedia What's up!

ではinit.d/rc.dスクリプトか、Supervisor、Daemontoolsかinetd/xinetdを使うと。

Windowsユーザーに教えるLinuxの常識(最終回):Linux起動の仕組みを理解しよう[rcスクリプト編] - @IT

[ThinkIT] 第4回:システム起動スクリプトとランレベルの比較 (1/3)

linux起動時に自動的に実行するコマンド(プログラム)の設定 - Qiita

とりあえず、バックグラウンド動かすには--daemonizeオプションを付けてログファイルの保存場所を指定すれば良い模様。

便利で超強力なWSGIサーバー uWSGI を使ってみよう - 檜山正幸のキマイラ飼育記

とりあえず、ここで保留。


その他参考ページ:

CentOS6.5でPython3.4+Nginx+uWSGIでバーチャルホスト運用 - Life is Really Short, Have Your Life!!

How to Deploy Python WSGI Applications Using uWSGI Web Server with Nginx | DigitalOcean

uWSGI で Django を使う方法 — Django 1.4 documentation

[Python]Flaskアプリをnginx + uWSGIでさくらVPSにデプロイする - Unresolved

uWSGI — flask-docs-ja 0.10-dev documentation

Apacheにかわるwebサーバ: uWSGIパフォーマンスチューニング - Qiita

ちゃんと運用するときのuWSGI設定メモ - Qiita

Ubuntu 12.04でpyenvを利用して速攻でPython3.4 + Nginx + uWSGI + FlaskなWebアプリケーション実行環境を作る - Qiita

nginx コマンド超シンプル早見表 - Qiita

nginxとuwsgiの連携方法 - Qiita