本文介绍了我的 Apache WSGI Flask web 应用程序无法导入其内部 python 模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了 Flask-Web-App,如果我直接使用 python 命令行,它运行良好.但是,当我使用 mod-wsgi 部署到 Apache2 时,它无法导入其内部模块.我已经阅读了所有与我类似的相关帖子,但还没有弄清楚问题所在.首先,我尝试了以下所有内容:

I have developed Flask-Web-App which runs fine if I use python command-line directly. However, when I deployed to Apache2 with mod-wsgi it can't import its internal modules. I have read all the relevant posts similar to mine but couldn't figure out the issue yet. Firstly, I tried all below:

1 - 确保应用程序文件夹下的所有文件和子文件夹都具有 www-data(Apache2 服务帐户")的 rwx 权限.

1 - Made sure all files and sub-folder under the application folder have rwx permission for www-data ("the Apache2 service account").

2 - 使用以下命令将模块路径添加到 Apache 配置文件:WSGIPythonPath/var/www/FlaskApp/FlaskApp/Base/:/var/www/FlaskApp/FlaskApp/Base/Form/:/var/www/FlaskApp/FlaskApp/Base/Model/(见下文:)

2 - Added the module path to the Apache config file using: WSGIPythonPath /var/www/FlaskApp/FlaskApp/Base/:/var/www/FlaskApp/FlaskApp/Base/Form/:/var/www/FlaskApp/FlaskApp/Base/Model/ (see below:)

3 - 将模块路径添加到 wsgi 脚本中的 sys.path 变量.

3 - Added the module path to sys.path variable in wsgi script.

#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApp/")
sys.path.append('/var/www/FlaskApp/FlaskApp/Base/')
sys.path.append('/var/www/FlaskApp/FlaskApp/Base/Form/')
sys.path.append('/var/www/FlaskApp/FlaskApp/Base/Model/')

from FlaskApp import app as application
application.secret_key = 'as345kj34h5kljj34sy'

我所有的内部导入都在 init.py 中,它会像下面这样启动应用程序:

All my internal imports are in init.py which starts up the application like below:

# internal imports
import Base.Model as Model
from Base import Constant as cnst
from Base.Form import UserRegistrationForm, ProductForm
.
.
.

if __name__ == "__main__":
    app.run()

我只是不知道我可能还错过了什么.从命令行使用 python 解释器时,一切正常.

I just don't have any other idea what else I might have possibly missed. Again everything works just fine when using the python interpreter from command-line.

非常感谢您的帮助.

问候,迈赫迪/迈克

错误日志

[Fri Sep 09 22:59:43.068802 2016] [mpm_event:notice] [pid 10719:tid 139878768617344] AH00489: Apache/2.4.7 (Ubuntu) mod_wsgi/3.4 Python/2.7.6 configured -- resuming normal operations
[Fri Sep 09 22:59:43.068872 2016] [core:notice] [pid 10719:tid 139878768617344] AH00094: Command line: '/usr/sbin/apache2'
[Fri Sep 09 22:59:47.926252 2016] [:error] [pid 10720:tid 139878671300352] [client ::1:55866] mod_wsgi (pid=10720): Target WSGI script '/var/www/FlaskApp/flaskapp.wsgi' cannot be loaded as Python module.
[Fri Sep 09 22:59:47.926300 2016] [:error] [pid 10720:tid 139878671300352] [client ::1:55866] mod_wsgi (pid=10720): Exception occurred processing WSGI script '/var/www/FlaskApp/flaskapp.wsgi'.
[Fri Sep 09 22:59:47.926325 2016] [:error] [pid 10720:tid 139878671300352] [client ::1:55866] Traceback (most recent call last):
[Fri Sep 09 22:59:47.926343 2016] [:error] [pid 10720:tid 139878671300352] [client ::1:55866]   File "/var/www/FlaskApp/flaskapp.wsgi", line 10, in <module>
[Fri Sep 09 22:59:47.926403 2016] [:error] [pid 10720:tid 139878671300352] [client ::1:55866]     from FlaskApp import app as application
[Fri Sep 09 22:59:47.926414 2016] [:error] [pid 10720:tid 139878671300352] [client ::1:55866]   File "/var/www/FlaskApp/FlaskApp/__init__.py", line 22, in <module>
[Fri Sep 09 22:59:47.926848 2016] [:error] [pid 10720:tid 139878671300352] [client ::1:55866]     from Base.Form import UserRegistrationForm, ProductForm
[Fri Sep 09 22:59:47.926889 2016] [:error] [pid 10720:tid 139878671300352] [client ::1:55866]   File "/var/www/FlaskApp/FlaskApp/Base/Form/__init__.py", line 1, in <module>
[Fri Sep 09 22:59:47.926975 2016] [:error] [pid 10720:tid 139878671300352] [client ::1:55866]     from UserRegistrationForm import UserRegistrationForm
[Fri Sep 09 22:59:47.927007 2016] [:error] [pid 10720:tid 139878671300352] [client ::1:55866]   File "/var/www/FlaskApp/FlaskApp/Base/Form/UserRegistrationForm.py", line 2, in <module>
[Fri Sep 09 22:59:47.927098 2016] [:error] [pid 10720:tid 139878671300352] [client ::1:55866]     from Base.Model import db_session, User
[Fri Sep 09 22:59:47.927156 2016] [:error] [pid 10720:tid 139878671300352] [client ::1:55866] ImportError: No module named Base.Model

flaskapp.wsgi

#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApp/")
sys.path.append('/var/www/FlaskApp/FlaskApp/Base/')
sys.path.append('/var/www/FlaskApp/FlaskApp/Base/Form/')
sys.path.append('/var/www/FlaskApp/FlaskApp/Base/Model/')

from FlaskApp import app as application
application.secret_key = 'as345kj34h5kljj34sy'

FlaskApp.conf

WSGIPythonPath /var/www/FlaskApp/FlaskApp/Base/:/var/www/FlaskApp/FlaskApp/Base/Form/:/var/www/FlaskApp/FlaskApp/Base/Model/

<VirtualHost *:80>
    ServerName localhost
    ServerAdmin [email protected]
    WSGIScriptAlias / /var/www/FlaskApp/flaskapp.wsgi
    <Directory /var/www/FlaskApp/FlaskApp/>
        Order allow,deny
        Allow from all
    </Directory>
    Alias /static /var/www/FlaskApp/FlaskApp/static
    <Directory /var/www/FlaskApp/FlaskApp/static/>
        Order allow,deny
        Allow from all
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    LogLevel warn
    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

推荐答案

代替:

WSGIPythonPath /var/www/FlaskApp/FlaskApp/Base/:/var/www/FlaskApp/FlaskApp/Base/Form/:/var/www/FlaskApp/FlaskApp/Base/Model/

<VirtualHost *:80>
    ServerName localhost
    ServerAdmin [email protected]
    WSGIScriptAlias / /var/www/FlaskApp/flaskapp.wsgi
    <Directory /var/www/FlaskApp/FlaskApp/>
        Order allow,deny
        Allow from all
    </Directory>
    Alias /static /var/www/FlaskApp/FlaskApp/static
    <Directory /var/www/FlaskApp/FlaskApp/static/>
        Order allow,deny
        Allow from all
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    LogLevel warn
    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

尝试使用:

<VirtualHost *:80>
    ServerName localhost
    ServerAdmin [email protected]
    WSGIDaemonProcess myapp python-path=/var/www/FlaskApp/FlaskApp
    WSGIScriptAlias / /var/www/FlaskApp/flaskapp.wsgi process-group=myapp application-group=%{GLOBAL}
    <Directory /var/www/FlaskApp>
    <Files flaskapp.wsgi>
        Order allow,deny
        Allow from all
    </Files>
    </Directory>
    Alias /static /var/www/FlaskApp/FlaskApp/static
    <Directory /var/www/FlaskApp/FlaskApp/static/>
        Order allow,deny
        Allow from all
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    LogLevel warn
    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

所做的更改是:

  1. 使用 mod_wsgi 守护进程模式而不是嵌入模式.请参阅 http://blog.dscpl.com.au/2012/10/why-are-you-using-embedded-mode-of.html
  2. 正确的 Apache 目录访问授权.它像您一样工作表明您在 Apache 配置中的其他地方有松懈的安全设置,因为您不应该这样做.已将其更改为首选机制,但您应该弄清楚为什么您的 Apache 配置允许 Apache 提供文件系统中的任何文件.
  3. 设置正确的 Python 模块搜索路径.自从切换到守护进程组后,在守护进程组上完成.
  4. 用于在守护进程组中使用 Python 主(应用程序)解释器上下文,以避免在子解释器中不起作用的 Python 第三方扩展模块出现问题.

这篇关于我的 Apache WSGI Flask web 应用程序无法导入其内部 python 模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-27 11:12