0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

nginx unitをmacで使う。【python(flask)編】

Posted at

ちょっとCERNが作成しているindicoというCMS?を立ち上げる機会があったのでメモ
macだといろいろと手こずったので。

インストール

https://pypi.org/project/indico/
から、pip2 install indicoでインストール
調べてみるとこのindico、flaskが使われている。
(python2系で動くので、pip2 ここは環境によりかなり違うと思う)

あとは公式を参照しながら、brewでpostgresなどインスト&設定を済ます。
https://docs.getindico.io/en/stable/installation/production/centos/apache/
(Apacheで動かさないので、Apacheの設定はひとまず不要)

uWSGIでまずは動かす

iniファイルは以下の通り。socketもvirtualenvもまずは使わない。

uwsgi.ini
[uwsgi]
processes = 4
enable-threads = true
http=0.0.0.0:8089

master = true
auto-procname = true
procname-prefix-spaced = indico
disable-logging = true

plugin = python
single-interpreter = true

touch-reload = /path/to/indico/web/indico.wsgi
wsgi-file = /path/to/indico/web/indico.wsgi

vacuum = true
buffer-size = 20480
memory-report = true
max-requests = 2500
harakiri = 900
harakiri-verbose = true
reload-on-rss = 2048
evil-reload-on-rss = 8192

上記で、uwsgi --http=0.0.0.0:8088 --wsgi-file=web/indico.wsgi で起動を確認。
(ほんとはceleryも起動してないといけないが、まずは確認だけ。)

無事に起動が確認できたら、nginx-unitの設定をすすめる。

nginx-unit インストール

https://unit.nginx.org/installation/#configuring-python
公式を参照しながら、./configure pythonmakeする前に入れる。
問題なければpythonのsoができる。

nginx-unitで起動

nginx-unitでWebアプリを起動するときはJSONをPUTするのだが、今回のindicoの場合は

indico.json
{
  "listeners": {
    "*:3009": {
      "application": "indico"
    }
  },

  "applications": {
    "indico": {
      "type": "python",
      "user": "user",
      "group": "staff",
      "module": "start",
      "environment": {
        "PYTHONPATH": "/usr/local/lib/python2.7/site-packages(pip2でインストールされるパス)"
      },
      "path": "/path/to/indico/web"
    }
  }
}

ここでハマったのが2点ほど

  1. JSON内のmodule部分にはファイル名を指定できない。
  2. indicoがインストールされているパスを読み込んでくれず、indicoが起動できない。

1. moduleの指定

最初はwsgiの設定ファイルがweb/配下にindico.wsgiというファイル名であるが、これをそのままmoduleに入れて設定を上げるとエラーになる。
なので、indico.wsgistart.pyとかにリネーム
(ここでindico.pyというファイル名にしてしまい、さらにハマる。モジュールの名前とファイル名が同じになると競合してライブラリがロードできなくなる。)
これで、moduleの部分にstartとかで起動できるようになる。

2. pip2のパスを追加

pip2のフォルダ確認。

$ pip2 -V
pip 19.2.3 from /usr/local/lib/python2.7/site-packages/pip (python 2.7)

上記フォルダをPYTHONPATHに追加してやる。(JSON参照)
これでJSONをPUTしてもエラーが出なくなった。

思ったこと

普段python使わないので、いろんなところで躓いた。
nginx-unitの情報もまだまだ少ないので、トライ・アンド・エラーばかりとなって結構時間がかかった。
ただ、nginx-unitで複数言語、複数アプリの起動ができるので今後は楽になるかも。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?