Flask セットアップ メモ


■Flask インストール
kokaki@skynew:~$ sudo apt update -y
kokaki@skynew:~$ sudo apt upgrade -y
kokaki@skynew:~$ sudo apt install -y python3 python3-pip
kokaki@skynew:~$ sudo pip install Flask

  Collecting Flask
    Downloading flask-3.0.2-py3-none-any.whl (101 kB)
        ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 101.3/101.3 KB 1.9 MB/s eta 0:00:00
  Collecting Werkzeug>=3.0.0
    Downloading werkzeug-3.0.1-py3-none-any.whl (226 kB)
        ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 226.7/226.7 KB 5.0 MB/s eta 0:00:00
  Collecting blinker>=1.6.2
    Downloading blinker-1.7.0-py3-none-any.whl (13 kB)
  Collecting itsdangerous>=2.1.2
    Downloading itsdangerous-2.1.2-py3-none-any.whl (15 kB)
  Collecting click>=8.1.3
    Downloading click-8.1.7-py3-none-any.whl (97 kB)
        ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 97.9/97.9 KB 2.2 MB/s eta 0:00:00
  Collecting Jinja2>=3.1.2
    Downloading Jinja2-3.1.3-py3-none-any.whl (133 kB)
        ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 133.2/133.2 KB 5.0 MB/s eta 0:00:00
  Collecting MarkupSafe>=2.0
    Downloading MarkupSafe-2.1.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (25 kB)
  Installing collected packages: MarkupSafe, itsdangerous, click, blinker, Werkzeug, Jinja2, Flask
    Attempting uninstall: blinker
      Found existing installation: blinker 1.4
      Not uninstalling blinker at /usr/lib/python3/dist-packages, outside environment /usr
      Can't uninstall 'blinker'. No files were found to uninstall.
  Successfully installed Flask-3.0.2 Jinja2-3.1.3 MarkupSafe-2.1.5 Werkzeug-3.0.1 blinker-1.7.0 click-8.1.7 itsdangerous-2.1.2
  WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. 
    It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv  !!!
  警告: pip を「root」ユーザーとして実行すると、アクセス許可が失われ、システム パッケージ マネージャーと競合する動作が発生する可能性があります。
   代わりに仮想環境を使用することをお勧めします: https://pip.pypa.io/warnings/venv
        
kokaki@skynew:~$ mkdir -p /home/kokaki/www/flask/hello
kokaki@skynew:~$ cd /home/kokaki/www/flask/hello
kokaki@skynew:~/www/flask/hello$ vi hello.py

  from flask import Flask
  
  app = Flask(__name__)
  
  @app.route("/")
  def hello_world():
      return "<p>Hello world!</p>"
        
kokaki@skynew:~/www/flask/hello$ export FLASK_APP=hello
kokaki@skynew:~/www/flask/hello$ flask run
  * Serving Flask app 'hello'
  * Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
  * Running on http://127.0.0.1:5000
Press CTRL+C to quit

http://localhost:5000/

■Apache2,Flask連携
kokaki@skynew:~$ sudo apt install apache2 apache2-utils ssl-cert libapache2-mod-wsgi-py3 -y
kokaki@skynew:~/www/flask$ mkdir testwsgi
kokaki@skynew:~/www/flask$ cd testwsgi/
kokaki@skynew:~/www/flask/testwsgi$ vi testwsgi.py

  def application(environ, start_response):
      status = '200 OK'
      output = b'Hello Howtoforge!\n'
      response_headers = [('Content-type', 'text/plain'),
                          ('Content-Length', str(len(output)))]
      start_response(status, response_headers)
      return [output]
        
kokaki@skynew:~/www/flask/testwsgi$ sudo vi /etc/apache2/sites-available/flask.conf

  WSGIDaemonProcess testwsgi user=kokaki group=kokaki threads=5
  WSGIScriptAlias /testwsgi /home/kokaki/www/flask/testwsgi/testwsgi.py

  WSGIProcessGroup testwsgi
  WSGIApplicationGroup %{RESOURCE}
  WSGIScriptReloading On
        
kokaki@skynew:~/www/flask/testwsgi$ sudo a2ensite flask
Enabling site flask.
To activate the new configuration, you need to run:
  systemctl reload apache2

kokaki@skynew:~/www/flask/testwsgi$ sudo systemctl restart apache2

http://localhost/testwsgi/

■Apache2,Flask+SQLAlchemy連携
kokaki@skynew:~$ pip install flask_sqlalchemy

  Defaulting to user installation because normal site-packages is not writeable
  Collecting flask_sqlalchemy
    Downloading flask_sqlalchemy-3.1.1-py3-none-any.whl (25 kB)
  Collecting sqlalchemy>=2.0.16
    Downloading SQLAlchemy-2.0.26-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.1 MB)
        ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.1/3.1 MB 3.8 MB/s eta 0:00:00
  Requirement already satisfied: flask>=2.2.5 in /usr/local/lib/python3.10/dist-packages (from flask_sqlalchemy) (3.0.2)
  Requirement already satisfied: Werkzeug>=3.0.0 in /usr/local/lib/python3.10/dist-packages (from flask>=2.2.5->flask_sqlalchemy) (3.0.1)
  Requirement already satisfied: itsdangerous>=2.1.2 in /usr/local/lib/python3.10/dist-packages (from flask>=2.2.5->flask_sqlalchemy) (2.1.2)
  Requirement already satisfied: blinker>=1.6.2 in /usr/local/lib/python3.10/dist-packages (from flask>=2.2.5->flask_sqlalchemy) (1.7.0)
  Requirement already satisfied: click>=8.1.3 in /usr/local/lib/python3.10/dist-packages (from flask>=2.2.5->flask_sqlalchemy) (8.1.7)
  Requirement already satisfied: Jinja2>=3.1.2 in /usr/local/lib/python3.10/dist-packages (from flask>=2.2.5->flask_sqlalchemy) (3.1.3)
  Collecting typing-extensions>=4.6.0
    Downloading typing_extensions-4.9.0-py3-none-any.whl (32 kB)
  Collecting greenlet!=0.4.17
    Downloading greenlet-3.0.3-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (616 kB)
        ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 616.0/616.0 KB 4.2 MB/s eta 0:00:00
  Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from Jinja2>=3.1.2->flask>=2.2.5->flask_sqlalchemy) (2.1.5)
  Installing collected packages: typing-extensions, greenlet, sqlalchemy, flask_sqlalchemy
  Successfully installed flask_sqlalchemy-3.1.1 greenlet-3.0.3 sqlalchemy-2.0.26 typing-extensions-4.9.0
      
kokaki@skynew:~/www/flask/testwsgi$ sudo vi /etc/apache2/sites-available/flask.conf

  WSGIDaemonProcess testwsgi user=kokaki group=kokaki threads=5
  WSGIScriptAlias /testwsgi /home/kokaki/www/flask/testwsgi/testwsgi.py
  WSGIScriptAlias /testapp /home/kokaki/www/flask/testapp.wsgi
  WSGIScriptAlias /todo /home/kokaki/www/flask/todo/todo.wsgi

  WSGIProcessGroup testwsgi
  WSGIApplicationGroup %{RESOURCE}
  WSGIScriptReloading On        
        
kokaki@skynew:~/www/flask/testwsgi$ sudo a2ensite flask
kokaki@skynew:~/www/flask/testwsgi$ sudo systemctl restart apache2

http://localhost/testapp