目次
install flask & uwsgi
$ cat /etc/redhat-release Red Hat Enterprise Linux release 9.1 (Plow) $ python --version Python 3.9.14 $ pip install flask <略> Successfully installed Jinja2-3.1.2 MarkupSafe-2.1.2 Werkzeug-2.2.3 click-8.1.3 flask-2.2.3 importlib-metadata-6.0.0 itsdangerous-2.1.2 zipp-3.15.0 $ pip install uwsgi <略> Successfully installed uwsgi-2.0.21
簡易アプリ作成と、Flask付属サーバでの実行
mkdir
$ mkdir myproj $ cd myproj
vi main.py
from flask import Flask app = Flask(__name__) @app.route('/') def index(): return "<p>Hello world!</p>"
Flask付属サーバで実行
$ cd /home/end0tknr/proj/flask/myproj ## 以下で実行対象のmoduleを指定しています $ export FLASK_APP=main $ flask run -h 0.0.0.0 -p 5050 * Debug mode: off * Running on all addresses (0.0.0.0) * Running on http://127.0.0.1:5050 * Running on http://10.0.2.15:5050
直接uWSGI で実行
先程のFlask付属サーバと同様の実行は以下の通りです
$ uwsgi --http=0.0.0.0:5050 --wsgi-file=./main.py --callable=app
nginx + uWSGI で実行
vi wsgi.py
from main import app if __name__ == '__main__': app.run()
vi wsgi.ini
[uwsgi] wsgi-file=/home/end0tknr/proj/flask/myproj/wsgi.py # wsgi実行時に移動 chdir=/home/end0tknr/proj/flask/myproj # アプリのオブジェクト変数名 callable=app socket=/tmp/uwsgi.sock chmod-socket=666 master=true processes=5 uid=end0tknr gid=end0tknr enable-threads=true threads=4 http=0.0.0.0:5000 logto=/home/end0tknr/log/uwsgi.log
vi nginx.conf
nginxは、defaultでuwsgiをサポートしていますので、 uwsgi_params と uwsgi_pass を記載するだけでOKです
server { listen 8080; location / { include uwsgi_params; uwsgi_pass unix:///tmp/uwsgi.sock; } }
uwsgi と nginx の実行
$ cd /home/end0tknr/proj/flask/myproj $ uwsgi --ini=./wsgi.ini
$ cd /home/end0tknr/local/nginx $ ./sbin/nginx
$ tail -f /home/end0tknr/log/uwsgi.log *** Starting uWSGI 2.0.21 (64bit) on [Sun Mar 5 19:00:07 2023] *** compiled with version: 11.3.1 20220421 (Red Hat 11.3.1-2) on 26 February 2023 23:00:22 os: Linux-5.14.0-162.6.1.el9_1.x86_64 #1 SMP PREEMPT_DYNAMIC Fri Sep 30 07:36:03 EDT 2022 nodename: rhel9.a5.jp machine: x86_64 clock source: unix pcre jit disabled detected number of CPU cores: 2 current working directory: /home/end0tknr/proj/flask/myproj detected binary path: /home/end0tknr/proj/flask/bin/uwsgi chdir() to /home/end0tknr/proj/flask/myproj your processes number limit is 14451 your memory page size is 4096 bytes detected max file descriptor number: 1024 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 3 uwsgi socket 0 bound to UNIX address /tmp/uwsgi.sock fd 6 Python version: 3.9.14 (main, Nov 7 2022, 00:00:00) [GCC 11.3.1 20220421 (Red Hat 11.3.1-2)] Python main interpreter initialized at 0x11bda60 python threads support enabled your server socket listen backlog is limited to 100 connections your mercy for graceful operations on workers is 60 seconds mapped 625728 bytes (611 KB) for 20 cores *** Operational MODE: preforking+threaded *** WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x11bda60 pid: 2255 (default app) *** uWSGI is running in multiple interpreter mode *** spawned uWSGI master process (pid: 2255) spawned uWSGI worker 1 (pid: 2256, cores: 4) spawned uWSGI worker 2 (pid: 2257, cores: 4) spawned uWSGI worker 3 (pid: 2258, cores: 4) spawned uWSGI worker 4 (pid: 2262, cores: 4) spawned uWSGI worker 5 (pid: 2263, cores: 4) spawned uWSGI http 1 (pid: 2270)