Skip to main content
 首页 » 编程设计

python之使用 Plesk 在 Apache 下运行 Flask 应用程序

2024年10月17日44leader

使用 Plesk 在 Apache 下运行 Flask 的问题无穷无尽。真的一直在为这个问题挠头。

当我使用 Plesk 时,我意识到我需要使用/var/www/vhosts 下的 vhosts conf 文件而不是虚拟主机配置(位于典型的“sites-available”目录下在 Apache )。

当我尝试运行我的 Flask 应用程序时,我在我的 Apache 错误日志中收到错误:

Timeout when reading response headers from daemon process 'unifica': /apps/start.wsgi

我的 wsgi 应用程序文件在这里:

/apps/start.wsgi

内容如下:

activate_this = '/apps/unifica/venv/bin/activate_this.py' 
execfile(activate_this, dict(__file__=activate_this)) 
from unifica import app as application 

如您所见。我正在使用虚拟环境。 随后,我的实际应用程序在这里:

apps/unifica/main.py

内容如下:

from flask import Flask 
app = Flask(__name__) 
 
@app.route('/') 
def hello_world(): 
    return 'Hello, World!' 
 
if __name__ == "__main__": 
    app.run(host='0.0.0.0') 

因为我的网站是通过 ssl 保护的;我有以下两个虚拟主机配置:

vhost.conf and vhost_ssl.conf

我的 vhost.conf 包含:

RewriteEngine On 
RewriteCond %{HTTPS} !on 
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} 

(重定向不安全的请求)

我的 vhost_ssl.conf 包含:

WSGIDaemonProcess unifica user=flaskuser group=flaskgroup threads=5 
WSGIScriptAlias / /apps/start.wsgi 
 
<Directory /apps > 
    WSGIProcessGroup unifica 
    WSGIApplicationGroup %{GLOBAL} 
    Require all granted 
    WSGIScriptReloading On 
</Directory> 

flaskuser 存在并且属于 flaskgroup。我已将 0755 权限分配给/apps/文件夹中的 flaskuser(作为所有者)。

知道我哪里出错了吗?我想我在某个地方犯了一个相当明显的错误......

请您参考如下方法:

如果您还没有解决这个问题,我可以通过在 CentOS7 上的 Plesk 12.5 中编辑自定义 vhost 文件来让 Flask 工作。它位于此处;

/var/www/vhosts/system/{域}/conf/vhost.conf

添加的示例;

<VirtualHost *:80> ServerName example.com ServerAdmin me@example.com WSGIScriptAlias /var/www/vhost/{domain}/{subdomain}/flaskapp.wsgi <Directory /var/www/vhost/{domain}/{subdomain}/static> Order allow,deny Allow from all </Directory> Alias /static /var/www/vhost/{domain}/{subdomain}/static <Directory /var/www/vhost/{domain}/{subdomain}/static> Order allow,deny Allow from all </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log LogLevel warn CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>

在我的设置中,我使用权限 705。请记住,其中一些编辑是针对子域进行的。希望您的问题得到解决!