Skip to main content
 首页 » 编程设计

python之如何在Apache2.2上使用mod_wsgi正确部署flask应用

2025年05月04日19exmyth

我一直在尝试在 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/

非常感谢任何可能的部署帮助。

谢谢

请您参考如下方法:

我遇到了这个问题,同样的解决方案在虚拟主机文件中。

有时 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 和 direcory 标签)

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 )

感谢您的帮助。