My Apache WSGI Flask web-app cannot import its int

2020-04-20 06:45发布

问题:

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 - Made sure all files and sub-folder under the application folder have rwx permission for www-data ("the Apache2 service account").

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 - 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'

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()

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.

You help is MUCH MUCH appreciate it.

Regards, Mehdi/Mike

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:

Instead of:

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>

try using:

<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>

Changes made are:

  1. Use mod_wsgi daemon mode not embedded mode. See http://blog.dscpl.com.au/2012/10/why-are-you-using-embedded-mode-of.html
  2. Correct Apache directory access authorisation. That it worked as you had it suggests you have lax security settings elsewhere in Apache configuration as what you had shouldn't have worked. Have changed it to preferred mechanism, but you should work out why your Apache configuration has it allowing Apache to serve up any file in the file system.
  3. Set the correct Python module search path. Done on the daemon process group since switched to that.
  4. For use of Python main (application) interpreter context in daemon process group to avoid issues with third party extension modules for Python that will not work in sub interpreters.