質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
uWSGI

uWSGIは、PythonでWebサービスを動かすアプリケーションサーバの一つです。WSGI(Web Server Gateway Interface)アプリケーションコンテナの一種で、WSGIに則ったDjangoやFlaskなどで動かすことができます。

Flask

FlaskはPython用のマイクロフレームワークであり、Werkzeug・Jinja 2・good intentionsをベースにしています。

Q&A

0回答

2288閲覧

nginx + uWSGI + Flask が起動しない 

Mohrey_Y

総合スコア0

uWSGI

uWSGIは、PythonでWebサービスを動かすアプリケーションサーバの一つです。WSGI(Web Server Gateway Interface)アプリケーションコンテナの一種で、WSGIに則ったDjangoやFlaskなどで動かすことができます。

Flask

FlaskはPython用のマイクロフレームワークであり、Werkzeug・Jinja 2・good intentionsをベースにしています。

0グッド

0クリップ

投稿2022/06/17 17:17

編集2022/06/17 18:16

EC2(Amazon Linux)にて、nginx + uWSGI でFlaskを動かしたいと考えております。

ここまで行ったこと
■Nginxのインストール
■uWSGIのインストール
(ここまで Nginx + uWSGI + Flaskによるwebアプリの構築 の3.1.2 と3.1.4を参考。Pythonは仮想環境ではなくEC2にもともと入っているものを使用しています。そのためpip はpip3 のようにして打ちました。)

■アプリのディレクトリの作成
起動するアプリは
|-templates
|-app.py
|-requirements.txt

といった構成です。これを/var 下に /www/uwsgi を作成し、配置しました。

■socket通信用のディレクトリを作成
[第一部]これならわかる!Flask+Nginx+uWSGIをAWSに丁寧にデプロイ
こちらのディレクトリ名を参考に、uwsgi 下に run ディレクトリを作成しました。

■app.iniファイルの作成
Nginx + uWSGI + Flaskによるwebアプリの構築 の3.2を参考に、app.iniを作成しました。

app.ini

1[uwsgi] 2module = app 3callable = app 4master = true 5processes = 1 6socket = /var/run/uwsgi.sock 7chmod-socket = 666 8vacuum = true 9die-on-term = true 10wsgi-file = /var/www/uwsgi/app.py 11logto = /var/www/uwsgi/app.log 12http = 0.0.0.0:80

空のログファイルを合わせ、以下のような構成になっています。

/var/www/uwsgi/
|-app.py
|-app.ini
|-app.log
|-templates
|-run
|-requirements.txt

■WSGIとNginxの連携
Nginx + uWSGI + Flaskによるwebアプリの構築 の3.3を参考に、以下のnxinxのconfファイルを作成しました。

sudo vi /etc/nginx/conf.d/uwsgi.conf
server { listen 80; location / { include uwsgi_params; uwsgi_pass unix:///var/run/uwsgi.sock; } }

以上です。これで、以下はできています。

□ pythonで直で実行

python3 app.py
$ python3 app.py * Serving Flask app 'app' (lazy loading) * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Debug mode: on * Running on http://127.0.0.1:5000 (Press CTRL+C to quit) * Restarting with stat * Debugger is active! * Debugger PIN: 287-234-209

□uWSGIで実行

uwsgi --http=0.0.0.0:5000 --wsgi-file=app.py --callable=app
$ uwsgi --http=0.0.0.0:5000 --wsgi-file=app.py --callable=app *** Starting uWSGI 2.0.20 (64bit) on [Fri Jun 17 16:10:01 2022] *** compiled with version: 7.3.1 20180712 (Red Hat 7.3.1-15) on 17 June 2022 08:56:32 os: Linux-X.XX.XXX-XXX.XXX.amzn2.x86_64 #1 SMP Wed May 25 22:12:19 UTC 2022 nodename: ip-10-0-1-162.ap-northeast-1.compute.internal machine: x86_64 clock source: unix detected number of CPU cores: 1 current working directory: /var/www/uwsgi detected binary path: /home/ec2-user/.local/bin/uwsgi !!! no internal routing support, rebuild with pcre support !!! *** WARNING: you are running uWSGI without its master process manager *** your memory page size is 4096 bytes detected max file descriptor number: 65535 lock engine: pthread robust mutexes thunder lock: disabled (you can enable it with --thunder-lock) uWSGI http bound on 0.0.0.0:5000 fd 4 spawned uWSGI http 1 (pid: 21006) uwsgi socket 0 bound to TCP address 127.0.0.1:45927 (port auto-assigned) fd 3 Python version: 3.7.10 (default, Jun 3 2021, 00:02:01) [GCC 7.3.1 20180712 (Red Hat 7.3.1-13)] *** Python threads support is disabled. You can enable it with --enable-threads *** Python main interpreter initialized at 0x2470f60 your server socket listen backlog is limited to 100 connections your mercy for graceful operations on workers is 60 seconds mapped 72904 bytes (71 KB) for 1 cores *** Operational MODE: single process *** unable to find "application" callable in file app.py unable to load app 0 (mountpoint='') (callable not found or import error) *** no app loaded. going in full dynamic mode *** *** uWSGI is running in multiple interpreter mode *** spawned uWSGI worker 1 (and the only) (pid: 21005, cores: 1)

しかし、uwsgiコマンドでiniを渡して動かそうとすると、すぐに止まって、エラーログがapp.logに残ります。
□nginx + uWSGIで実行

$ uwsgi --ini /var/www/uwsgi/app.ini
$ uwsgi --ini /var/www/uwsgi/app.ini [uWSGI] getting INI configuration from /var/www/uwsgi/app.ini

こちらの状態で、4000番ポートにアクセスすると見えるものの、nginxと連携できているようには見えません。
ctrl+Cで終了した後、app.logを確認すると以下のように残ります。

app.log

1$ cat app.log 2*** Starting uWSGI 2.0.20 (64bit) on [Fri Jun 17 17:04:38 2022] *** 3compiled with version: 7.3.1 20180712 (Red Hat 7.3.1-15) on 17 June 2022 08:56:32 4os: Linux-X.XX.XXX-XXX.XXX.amzn2.x86_64 #1 SMP Wed May 25 22:12:19 UTC 2022 5nodename: ip-10-0-1-162.ap-northeast-1.compute.internal 6machine: x86_64 7clock source: unix 8detected number of CPU cores: 1 9current working directory: /var/www/uwsgi 10detected binary path: /home/ec2-user/.local/bin/uwsgi 11!!! no internal routing support, rebuild with pcre support !!! 12chdir() to /var/www/uwsgi 13your memory page size is 4096 bytes 14detected max file descriptor number: 65535 15lock engine: pthread robust mutexes 16thunder lock: disabled (you can enable it with --thunder-lock) 17uWSGI http bound on 0.0.0.0:4000 fd 3 18uwsgi socket 0 bound to UNIX address /tmp/uwsgi.sock fd 6 19Python version: 3.7.10 (default, Jun 3 2021, 00:02:01) [GCC 7.3.1 20180712 (Red Hat 7.3.1-13)] 20*** Python threads support is disabled. You can enable it with --enable-threads *** 21Python main interpreter initialized at 0x147d350 22your server socket listen backlog is limited to 100 connections 23your mercy for graceful operations on workers is 60 seconds 24mapped 145808 bytes (142 KB) for 1 cores 25*** Operational MODE: single process *** 26WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x147d350 pid: 21379 (default app) 27mountpoint already configured. skip. 28*** uWSGI is running in multiple interpreter mode *** 29spawned uWSGI master process (pid: 21379) 30spawned uWSGI worker 1 (pid: 21382, cores: 1) 31spawned uWSGI http 1 (pid: 21383) 32SIGINT/SIGTERM received...killing workers... 33gateway "uWSGI http 1" has been buried (pid: 21383) 34worker 1 buried after 1 seconds 35goodbye to uWSGI. 36VACUUM: unix socket /tmp/uwsgi.sock removed.

権限の問題に見えますが、解決できておりません。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問