无法解决 Flask 设置中的 mod_wsgi 异常
Cannot solve mod_wsgi exception in Flask setup
我想使用 Python 3 部署 Flask 应用程序。我正在 运行ning Ubuntu 16.04,Apache2。
我运行 sudo apt-get install libapache2-mod-wsgi-py3 安装wsgi.
我遵循了说明 here。我在 /var/www/html/hxueh.net/finance 的 Linode 服务器上有一个 Flask 应用程序。在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 启用了让我们加密证书。
当我访问我的服务器时 return 错误 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 wsgi
和 sudo 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 帮助我部署了我的第一个 Web 应用程序。
你有:
WSGIProcessGroup finance
告知 mod_wsgi 向守护进程组中的 WSGI 应用程序 运行 发送请求,但尚未配置守护进程组。
在WSGIScriptAlias
指令前添加:
WSGIDaemonProcess finance
另请阅读:
了解有关守护进程组的更多信息。
顺便说一句,mod_wsgi 4.3.0 的版本很旧。您应该避免在 Debian/Ubuntu 上为 mod_wsgi 使用系统提供的软件包,因为它们通常已经过时且不受支持。建议您卸载系统包并使用 pip
安装方法自行从源代码安装。参见:
我想使用 Python 3 部署 Flask 应用程序。我正在 运行ning Ubuntu 16.04,Apache2。
我运行 sudo apt-get install libapache2-mod-wsgi-py3 安装wsgi.
我遵循了说明 here。我在 /var/www/html/hxueh.net/finance 的 Linode 服务器上有一个 Flask 应用程序。在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 启用了让我们加密证书。
当我访问我的服务器时 return 错误 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 wsgi
和 sudo 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 帮助我部署了我的第一个 Web 应用程序。
你有:
WSGIProcessGroup finance
告知 mod_wsgi 向守护进程组中的 WSGI 应用程序 运行 发送请求,但尚未配置守护进程组。
在WSGIScriptAlias
指令前添加:
WSGIDaemonProcess finance
另请阅读:
了解有关守护进程组的更多信息。
顺便说一句,mod_wsgi 4.3.0 的版本很旧。您应该避免在 Debian/Ubuntu 上为 mod_wsgi 使用系统提供的软件包,因为它们通常已经过时且不受支持。建议您卸载系统包并使用 pip
安装方法自行从源代码安装。参见: