在Flask设置中无法解决mod_wsgi异常

时间:2018-01-21 03:15:47

标签: python apache flask mod-wsgi

我想使用Python 3部署Flask应用程序。我正在运行Ubuntu 16.04,Apache2。

我运行了sudo apt-get install libapache2-mod-wsgi-py3来安装wsgi。

我遵循了here指令。我的Linode服务器上有一个Flask App,位于/var/www/html/hxueh.net/finance。在 finance 文件夹中,有一个文件和一个文件夹。结构看起来像这样。

|--------finance
|----------------finance
|-----------------------static
|-----------------------templates
|-----------------------venv
|-----------------------application.py
|----------------finance.wsgi

在venv / bin中:

activate activate_this.py flask pip3.5 python3.5
activate.csh easy_install pip python python-config
activate.fish easy_install-3.5 pip3 python3 wheel

finance.wsgi是:

#!/usr/bin/python3
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/html/hxueh.net/finance/")

from finance import app as application

我的Apache2配置是:

<VirtualHost *:80>
        ServerName finance.hxueh.net
        ServerAdmin hxueh1996@gmail.com
        WSGIScriptAlias / /var/www/html/hxueh.net/finance/finance.wsgi
        <Directory /var/www/html/hxueh.net/finance/finance/>
                Order allow,deny
                Allow from all
        </Directory>
        <Directory /var/www/html/hxueh.net/finance>
                WSGIProcessGroup finance
                WSGIApplicationGroup %{GLOBAL}
                Order deny,allow
                Allow from all
        </Directory>
        Alias /static /var/www/html/hxueh.net/finance/finance/static
        <Directory /var/www/html/hxueh.net/finance/finance/static/>
                Order allow,deny
                Allow from all
        </Directory>
        ErrorLog ${APACHE_LOG_DIR}/error.log
        LogLevel warn
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

我在同一台服务器上运行了一个Wordpress应用程序,Certbot启用了Let的加密证书。

当我访问我的服务器时,它返回错误500.在error.log中显示:

[Sun Jan 21 10:40:56.310304 2018] [mpm_prefork:notice] [pid 26281] AH00169: caught SIGTERM, shutting down
[Sun Jan 21 10:41:21.236671 2018] [ssl:warn] [pid 26747] AH01909: hxueh.net:443:0 server certificate does NOT include an ID which matches the server name
[Sun Jan 21 10:41:21.276195 2018] [ssl:warn] [pid 26748] AH01909: hxueh.net:443:0 server certificate does NOT include an ID which matches the server name
[Sun Jan 21 10:41:21.276370 2018] [wsgi:warn] [pid 26748] mod_wsgi: Compiled for Python/3.5.1+.
[Sun Jan 21 10:41:21.276378 2018] [wsgi:warn] [pid 26748] mod_wsgi: Runtime using Python/3.5.2.
[Sun Jan 21 10:41:21.278888 2018] [mpm_prefork:notice] [pid 26748] AH00163: Apache/2.4.18 (Ubuntu) OpenSSL/1.0.2g mod_wsgi/4.3.0 Python/3.5.2 configured -- resuming normal operations
[Sun Jan 21 10:41:21.278910 2018] [core:notice] [pid 26748] AH00094: Command line: '/usr/sbin/apache2'
[Sun Jan 21 10:44:02.826408 2018] [wsgi:error] [pid 26751] [client xxx.xxx.xxx.xxx:xxx] No WSGI daemon process called 'finance' has been configured: /var/www/html/hxueh.net/finance/finance.wsgi

更新:问题已解决。

我重写了结构并将wsgi文件放在项目中。

|--------Finance
|----------------static
|----------------templates
|----------------venv
|----------------application.py
|----------------finance.wsgi

我也重写了Apache 2文件。我禁用了WSGIProcessGroup,因为我不需要它。

<VirtualHost *:80>
        ServerName finance.hxueh.net
        ServerAdmin hxueh1996@gmail.com
        WSGIScriptAlias / /var/www/html/hxueh.net/Finance/finance.wsgi
        <Directory /var/www/html/hxueh.net/Finance/>
                Order allow,deny
                Allow from all
        </Directory>
        <Directory /var/www/html/hxueh.net/Finance>
                Order deny,allow
                Allow from all
        </Directory>
        Alias /static /var/www/html/hxueh.net/Finance/static
        <Directory /var/www/html/hxueh.net/Finance/static/>
                Order allow,deny
                Allow from all
        </Directory>
        ErrorLog ${APACHE_LOG_DIR}/error.log
        LogLevel warn
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

确保在/etc/apache2/mods-available/mod.load中启用mod_wsgi。只需运行mod_wsgi-express module-config并将输出放入其中。然后运行sudo a2enmod wsgisudo service apache2 restart

我的finance.wsgi是:

#!/usr/bin/python3
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/html/hxueh.net/Finance/")

from application import app as application

最后,感谢Graham Dumpleton帮我部署了我的第一个网络应用程序。

1 个答案:

答案 0 :(得分:1)

你有:

WSGIProcessGroup finance

告诉mod_wsgi将请求发送到在守护程序进程组中运行但尚未配置守护进程组的WSGI应用程序。

WSGIScriptAlias指令之前添加:

WSGIDaemonProcess finance

同时阅读:

了解有关守护进程组的更多信息。

BTW,mod_wsgi 4.3.0的版本很老了。你应该避免在Debian / Ubuntu上为mod_wsgi使用系统提供的软件包,因为它们通常是过时的并且也是不受支持的。建议您使用pip安装方法自行卸载系统软件包并从源代码安装。参见: