如何在Apache2.2上正确部署Flask应用程序与mod_wsgi

2 投票
1 回答
2722 浏览
提问于 2025-04-18 11:33

我一直在尝试把我的应用程序部署到Ubuntu的网络服务器上,但效果不太好。我定义的虚拟主机文件是:

    #WSGIRestrictStdout Off
    <VirtualHost *:80>
    ServerName demo.engineerinme.com
    WSGIDaemonProcess fedoracollege  user=engineer group=www-data threads=5
    WSGIScriptAlias / /home/engineer/fedora-college/fedora_college.wsgi
    <Directory /home/engineer/fedora-college/>
                    WSGIProcessGroup fedoracollege
                    WSGIApplicationGroup %{GLOBAL}
                    WSGIScriptReloading On
                    Options All ExecCGI Indexes FollowSymLinks
                    Order allow,deny
                    Allow from all

    </Directory>

    Alias /static /home/engineer/fedora-college/fedora_college/static
    <Directory /home/engineer/fedora-college/fedora_college/static/>
                    Order allow,deny
                    Allow from all
    </Directory>

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

我遇到的问题是,静态内容可以正常工作,但Flask应用程序却无法运行。比如http://demo.engineerinme.com显示的是找不到的错误。但是http://demo.engineerinme.com/static却可以正常访问。

用于部署的wsgi脚本是:

#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/home/engineer/fedora-college/")

from fedora_college import app as application
application.secret_key = 'Add your secret key'

错误日志:

[Mon Jun 30 23:01:31 2014] [debug] mod_deflate.c(615): [client 59.177.114.30] Zlib: Compressed 233 to 180 : URL /
[Mon Jun 30 23:01:32 2014] [debug] mod_deflate.c(615): [client 59.177.114.30] Zlib: Compressed 233 to 180 : URL /
[Mon Jun 30 23:01:51 2014] [debug] mod_deflate.c(615): [client 59.177.114.30] Zlib: Compressed 233 to 180 : URL /

访问日志:

        59.177.114.30 - - [30/Jun/2014:23:01:32 +0400] "GET / HTTP/1.1" 404 441 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36"
        59.177.114.30 - - [30/Jun/2014:23:01:51 +0400] "GET / HTTP/1.1" 404 442 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36"

应用程序的代码在这里可以找到:https://github.com/hammadhaleem/fedora-college/

如果有人能帮忙解决部署的问题,我将非常感激。

谢谢

1 个回答

1

我遇到了这个问题,解决办法在虚拟主机文件里。

有时候,Apache的日志会显示这个错误:

[Mon Oct 17 15:24:24 2011] [error] [client 90.181.85.69] (13)Permission denied: mod_wsgi (pid=21805): Unable to connect to WSGI daemon process 'fedoracollege' on '/var/run/apache2/wsgi.16282.4.1.sock' after multiple attempts.

这个问题在这里解释得很清楚。我只是重新构建了wsgi_mod。

https://serverfault.com/questions/322131/wsgi-says-permissions-denied-on-my-ubuntu-server-no-wsgisocketprefix-setting

另外,把虚拟主机文件改成这个格式。(去掉了virtualhost和directory标签)

Alias /static  /home/engineer/fedora-college/fedora_college/static
WSGIDaemonProcess fedoracollege user=engineer group=www-data threads=5
#WSGIDaemonProcess fedoracollege maximum-requests=1000 display-name=fedora-college processes=4 threads=4
WSGISocketPrefix /var/run/wsgi
WSGIRestrictStdout Off
WSGIRestrictSignal Off
WSGIPythonOptimize 1
WSGIScriptAlias /  /home/engineer/fedora-college/fedora_college.wsgi
<Location />
    WSGIProcessGroup fedoracollege
</Location>


ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel debug
CustomLog ${APACHE_LOG_DIR}/access.log combined

同时,设置

application = True 

以便能正确报告错误。

希望这能帮到某个人。(别忘了点赞 :P)

感谢大家的帮助。

撰写回答