'How to structure apache virtual host and wsgi file for Flask application with python 3.10 on raspbian

On a raspberry pi 3 I am trying to deploy a Flask application on apache server. This application is going to be accessible by a custom internal network domain called darts.loc.

The structure of the application is as following:

/var/www/darts
└── darts
    ├── dartsapp
    │   ├── api
    │   │   └── resources
    │   │  
    │   ├── static
    │   │   ├── css
    │   │   ├── fonts
    │   │   ├── images
    │   │   └── js
    │   ├── templates
    │   │   
    │   │      
    │   │          
    │   │           
    │   │           
    │   │           
    │   ├── uploads
    │   └── website
    └── venv

In /var/www/darts/darts I have an __init__.py file with the following content:

from dartsapp import create_app

app = create_app()

if __name__ == '__main__':
    app.run(debug=True)

What should I update in my darts.conf and darts.wsgi files so that I can get the application running?

The current structure is as following:

darts.conf:

<VirtualHost *:443>
                ServerName darts.loc
                ServerAdmin [email protected]

WSGIScriptAlias / /var/www/darts/darts.wsgi
SSLEngine on
SSLCertificateFile  /etc/ssl/certs/apache-selfsigned.crt
SSLCertificateKeyFile /etc/ssl/private/apache-selfsigned.key
<Directory /var/www/darts/darts>
                         AddDefaultCharset utf-8
                         SetEnv PYTHONIOENCODING utf8

                        Order allow,deny
                        Allow from all
                        Options Indexes FollowSymLinks MultiViews
                        AllowOverride All
                        Require all granted
                </Directory>
                 Alias /dartsapp /var/www/darts/darts/dartsapp
                <Directory /var/www/darts/darts/dartsapp>
                        Order allow,deny
                        Allow from all
                        Options Indexes FollowSymLinks MultiViews
                        AllowOverride All
                        Require all granted
                </Directory>

                Alias /templates /var/www/darts/darts/dartsapp/templates
                <Directory /var/www/darts/darts/dartsapp/templates/>
                        Order allow,deny
                        Allow from all
                        Options Indexes FollowSymLinks MultiViews
                        AllowOverride All
                        Require all granted
                </Directory>
                 Alias /static /var/www/darts/darts/dartsapp/static
                <Directory /var/www/darts/darts/dartsapp/static/>
                        Order allow,deny
                        Allow from all
                        Options Indexes FollowSymLinks MultiViews
                        AllowOverride All
                        Require all granted
                </Directory>
                 Alias /uploads /var/www/darts/darts/dartsapp/uploads
                 <Directory /var/www/darts/darts/dartsapp/uploads/>
                        Order allow,deny
                        Allow from all
                        Options Indexes FollowSymLinks MultiViews
                        AllowOverride All
                        Require all granted
                </Directory>

                ErrorLog ${APACHE_LOG_DIR}/error.log
                LogLevel warn
                CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

darts.wsgi file (under /var/www/darts):

#!/var/www/darts/darts/venv/bin/python3.10/
import sys
#sys.path.append('/var/www/darts/darts/venv/lib/python3.10/site-packages/')

import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/darts/darts/")

from darts import app as application


Solution 1:[1]

I managed to run everything by adding two changes.

The darts.wsgi file now looks like this:

#!/var/www/darts/darts/venv/bin/python3.10
import sys
sys.path.append('/var/www/darts/darts/venv/lib/python3.10/site-packages/')

import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/darts/darts/")
sys.path.insert(0, "/var/www/darts/darts/venv/lib/python3.10/site-packages")

from dartsapp import create_app

application = create_app()

and in the darts.conf file I have added

WSGIPassAuthorization On

Seems to be working for now.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 D.Angelov