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

问题描述 投票:0回答:2

在树莓派 3 上,我试图在 apache 服务器上部署 Flask 应用程序。这个应用程序将可以通过一个名为 darts.loc 的自定义内部网络域访问。

应用程序结构如下:

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

在 /var/www/darts/darts 我有一个

__init__.py
文件,内容如下:

from dartsapp import create_app

app = create_app()

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

我应该在我的 darts.conf 和 darts.wsgi 文件中更新什么才能让应用程序运行?

当前结构如下:

飞镖会议:

<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 文件(在 /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
python linux apache flask mod-wsgi
2个回答
1
投票

我设法通过添加两个更改来运行所有内容。

darts.wsgi 文件现在看起来像这样:

#!/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()

在我添加的 darts.conf 文件中

WSGIPassAuthorization On

现在似乎在工作。


0
投票

我发现如果我在守护进程模式下运行 WSGI 那么 wsgi 文件真的可以 简单,而 apache 配置只是复杂得多。

假设您的应用程序文件夹名为飞镖,并且您使用

app=Flask(__name__)
来定义您的应用程序。

from darts import app as application

这是简化的 apache 配置

<VirtualHost *:443>
ServerName darts.loc
WSGIDaemonProcess darts threads=5 python-home=/var/www/darts/darts/venv python-path=/var/www/darts/
WSGIScriptAlias / /var/www/darts/dart.wsgi
WSGIApplicationGroup %{GLOBAL}
<Directory /var/www/darts/darts>
    WSGIProcessGroup darts
    Require all granted
</Directory>
</VirtualHost>
© www.soinside.com 2019 - 2024. All rights reserved.