User Tools

Site Tools


programming:python:flask

Flask

Python3 + Flask + WSGI + Apache

System

Used: Debian 9.5

apt install libapache2-mod-wsgi-py3

Directory Structure

/
├── etc/
│   └── apache2/
│       └── sites-enabled/
│           └── www.example.com.conf
└ opt/
  └── python/
      └── myapp/
          ├── app.wsgi
          ├── config.py
          ├── requirements.txt
          ├── run.py
          ├── app/
          │   ├── __init__.py
          │   ├── auth/
          │   │   ├── __init__.py
          │   │   └── routes.py
          │   ├── errors/
          │   │   ├── handlers.py
          │   │   └── __init__.py
          │   ├── main/
          │   │   ├── __init__.py
          │   │   └── routes.py
          │   ├── models.py
          │   ├── static/
          │   │   ├── favicon.ico
          │   │   ├── logo.png
          │   │   └── main.min.css
          │   └── templates/
          │       ├── base.html
          │       └── login.html
          ├── data/
          │   └── miscdata.xml
          ├── log/
          │   └── app.log
          └── venv/

Apache

/etc/apache2/sites-enabled/www.example.com.conf

<VirtualHost *:443>
    ServerName www.example.com
    ServerAdmin hostmaster@example.com

    DocumentRoot /var/www/example.com/www

    ErrorLog ${APACHE_LOG_DIR}/www.example.com-error.log
    LogLevel warn
    CustomLog ${APACHE_LOG_DIR}/www.example.com-access.log combined

    SSLEngine on
    SSLCertificateFile      /etc/dehydrated/certs/example.com/fullchain.pem
    SSLCertificateKeyFile   /etc/dehydrated/certs/example.com/privkey.pem

    WSGIDaemonProcess myapp user=www-data group=www-data threads=5 python-home=/opt/python/myapp/venv python-path=/opt/python/myapp/:/opt/python/myapp/venv/lib/python3.5/site-packages/
    WSGIProcessGroup myapp
    WSGIScriptAlias /myapp /opt/python/myapp/app.wsgi

    <Directory /opt/python/myapp>
        Require all granted
    </Directory>
</VirtualHost>

App

mkdir -p /opt/python/myapp/{data,log,app}
mkdir -p /opt/python/myapp/app/{static,templates}

Put your files to /opt/python/myapp/app/.

WSGI

/opt/python/myapp/myapp/app.wsgi

from app import create_app
application = create_app("production")

venv

cd /opt/python/myapp/
python3 -m venv venv
. venv/bin/activate
pip install wheel
pip install Flask
pip install Flask-Login

Create a requirements.txt for reinstalling the pip packages.

pip freeze > requirements.txt

Access

programming/python/flask.txt · Last modified: 2018-08-18 00:02 by root