我的Apache WSGI Flask web-app无法导入其内部python模块

时间:2016-09-09 18:49:07

标签: python-2.7 flask apache2 mod-wsgi

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

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

2 - 添加了Apache配置文件的模块路径:WSGIPythonPath / var / www / FlaskApp / FlaskApp / Base /:/ var / www / FlaskApp / FlaskApp / Base / Form /:/ var / www / FlaskApp / FlaskApp / Base / Model /(见下文:)

3 - 在wsgi脚本中添加了sys.path变量的模块路径。

#!/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中启动应用程序,如下所示:

# 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解释器时,一切正常。

你的帮助非常多。

此致 梅迪/麦克

error.log中

[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 admin@eynaksara.com
    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>

1 个答案:

答案 0 :(得分:1)

而不是:

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

<VirtualHost *:80>
    ServerName localhost
    ServerAdmin admin@eynaksara.com
    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 admin@eynaksara.com
    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的第三方扩展模块出现在子解释器中无法解决的问题。