WSGI脚本'/var/www/FlaskApp/flaskapp.wsgi'无法作为Python模块加载

时间:2018-02-05 12:49:09

标签: python apache flask mod-wsgi wsgi

我们正在尝试按照PythonProgramming的说明构建一个flaskapp。

请在下面找到文件夹结构。

username@server:/var/www/FlaskApp$ pwd
/var/www/FlaskApp
username@server:/var/www/FlaskApp$ ls
FlaskApp  flaskapp.wsgi

username@server:/var/www/FlaskApp/FlaskApp$ pwd
/var/www/FlaskApp/FlaskApp
username@server:/var/www/FlaskApp/FlaskApp$ ls
__init__.py  static  templates  xlogin.py

flaskapp.wsgi的内容

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

from FlaskApp import app as application
application.secret_key = 'Thisissupposedtobesecret!'

“/etc/apache2/sites-available/FlaskApp.conf”的内容

<VirtualHost *:80>
    ServerName <server IP>
    ServerAdmin <email>
    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>

当应用程序直接使用python2或python3运行时,它运行正常。

username@server:/var/www/FlaskApp/FlaskApp$ python3 __init__.py
/home/raj/.local/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py:794: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True or False to suppress this warning.
  'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
/home/raj/.local/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py:794: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True or False to suppress this warning.
  'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
 * Debugger is active!
 * Debugger PIN: 231-825-237

username@server:/var/www/FlaskApp/FlaskApp$ python __init__.py
/home/raj/.local/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py:794: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True or False to suppress this warning.
  'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
/home/raj/.local/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py:794: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True or False to suppress this warning.
  'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
 * Debugger is active!
 * Debugger PIN: 151-229-561

但是,应用程序无法在服务器上运行。在apache2服务器的error.log中观察到以下错误。

[Fri Feb 02 10:12:27.803660 2018] [wsgi:error] [pid 21512:tid 140205275080448] [remote 172.16.67.27:60575] mod_wsgi (pid=21512): Target WSGI script '/var/www/FlaskApp/flaskapp.wsgi' cannot be loaded as Python module.
[Fri Feb 02 10:12:27.803874 2018] [wsgi:error] [pid 21512:tid 140205275080448] [remote 172.16.67.27:60575] mod_wsgi (pid=21512): Exception occurred processing WSGI script '/var/www/FlaskApp/flaskapp.wsgi'.
[Fri Feb 02 10:12:27.803990 2018] [wsgi:error] [pid 21512:tid 140205275080448] [remote 172.16.67.27:60575] Traceback (most recent call last):
[Fri Feb 02 10:12:27.804065 2018] [wsgi:error] [pid 21512:tid 140205275080448] [remote 172.16.67.27:60575]   File "/var/www/FlaskApp/flaskapp.wsgi", line 7, in <module>
[Fri Feb 02 10:12:27.804078 2018] [wsgi:error] [pid 21512:tid 140205275080448] [remote 172.16.67.27:60575]     from FlaskApp import app as application
[Fri Feb 02 10:12:27.804109 2018] [wsgi:error] [pid 21512:tid 140205275080448] [remote 172.16.67.27:60575] ModuleNotFoundError: No module named 'FlaskApp'

使用“libapache2-mod-wsgi-py3”版本。我浏览了许多有相关错误的文章,但找不到解决方案。你能帮忙解决这些错误吗。谢谢。

1 个答案:

答案 0 :(得分:0)

你有:

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

from FlaskApp import app as application
application.secret_key = 'Thisissupposedtobesecret!'

尝试使用:

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

from FlaskApp import app as application
application.secret_key = 'Thisissupposedtobesecret!'

路径必须是FlaskApp包的父级,而不是包目录本身。