从烧瓶应用程序-apache wsgi mod

时间:2019-07-05 11:42:00

标签: python flask apache2 mod-wsgi paramiko

我一直在尝试使用apache作为服务器(wsgi_mod)来部署我的flask应用程序。我创建了所有必需的文件(Apache的.wsgi和.conf文件)。

但是当我请求页面时,出现500错误。查看日志显示

[Fri Jul 05 12:39:18.746453 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450] mod_wsgi (pid=74305): Target WSGI script '/var/www/html/ExampleFlask/ExampleFlask/my_flask_app.wsgi' cannot be loaded as Python module.
[Fri Jul 05 12:39:18.746912 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450] mod_wsgi (pid=74305): Exception occurred processing WSGI script '/var/www/html/ExampleFlask/ExampleFlask/my_flask_app.wsgi'.
[Fri Jul 05 12:39:18.750486 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450] Traceback (most recent call last):
[Fri Jul 05 12:39:18.750808 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450]   File "/var/www/html/ExampleFlask/ExampleFlask/my_flask_app.wsgi", line 6, in <module>
[Fri Jul 05 12:39:18.750995 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450]     from my_flask_app import app as application
[Fri Jul 05 12:39:18.751224 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450]   File "/var/www/html/ExampleFlask/ExampleFlask/my_flask_app.py", line 2, in <module>
[Fri Jul 05 12:39:18.751411 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450]     import paramiko
[Fri Jul 05 12:39:18.751600 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450]   File "/usr/lib/python3/dist-packages/paramiko/__init__.py", line 30, in <module>
[Fri Jul 05 12:39:18.751819 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450]     from paramiko.transport import SecurityOptions, Transport
[Fri Jul 05 12:39:18.752023 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450]   File "/usr/lib/python3/dist-packages/paramiko/transport.py", line 32, in <module>
[Fri Jul 05 12:39:18.752224 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450]     from cryptography.hazmat.backends import default_backend
[Fri Jul 05 12:39:18.752446 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450]   File "/usr/lib/python3/dist-packages/cryptography/hazmat/backends/__init__.py", line 7, in <module>
[Fri Jul 05 12:39:18.752625 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450]     import pkg_resources
[Fri Jul 05 12:39:18.752834 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450]   File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3019, in <module>
[Fri Jul 05 12:39:18.753009 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450]     @_call_aside
[Fri Jul 05 12:39:18.753215 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450]   File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3003, in _call_aside
[Fri Jul 05 12:39:18.753405 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450]     f(*args, **kwargs)
[Fri Jul 05 12:39:18.753597 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450]   File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3032, in _initialize_master_working_set
[Fri Jul 05 12:39:18.753806 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450]     working_set = WorkingSet._build_master()
[Fri Jul 05 12:39:18.754000 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450]   File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 646, in _build_master
[Fri Jul 05 12:39:18.754175 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450]     ws = cls()
[Fri Jul 05 12:39:18.754390 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450]   File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 639, in __init__
[Fri Jul 05 12:39:18.754590 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450]     self.add_entry(entry)
[Fri Jul 05 12:39:18.754782 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450]   File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 695, in add_entry
[Fri Jul 05 12:39:18.754981 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450]     for dist in find_distributions(entry, True):
[Fri Jul 05 12:39:18.755178 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450]   File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2012, in find_on_path
[Fri Jul 05 12:39:18.755382 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450]     if len(os.listdir(fullpath)) == 0:
[Fri Jul 05 12:39:18.755643 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450] PermissionError: [Errno 13] Permission denied: '/usr/local/lib/python3.5/dist-packages/Werkzeug-0.15.4.dist-info'

我正在尝试将此应用程序部署在debian 9服务器中。我已经在我的kali盒子上对其进行了测试,仅通过安装flask和paramiko并配置apache即可正常工作。

我尝试重新安装一些软件包,例如paramiko,flask和Werkzeug,但没有成功。

我什至创建了一个带有简单的hello world消息的新应用。直到我导入paramiko,然后出现“权限被拒绝”错误,它才能正常工作。

我的测试应用包含三个文件。

  • my_flask_app.py
from flask import Flask
import paramiko
app = Flask(__name__)
@app.route("/")
def hello():
    return "Hello world!"
if __name__ == "__main__":
    app.run()

  • my_flask_app.wsgi
import logging
import sys
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0, '/var/www/html/ExampleFlask/ExampleFlask/')
from my_flask_app import app as application
application.secret_key = 'thisisasecretkey'
  • my_flask_app.conf
<VirtualHost *:80>
     # Add machine's IP address (use ifconfig command)
     ServerName 10.1.1.50
     # Give an alias to to start your website url with
     WSGIScriptAlias /testFlask /var/www/html/ExampleFlask/ExampleFlask/my_flask_app.wsgi
     WSGIDaemonProcess my_flask_app user=www-data group=www-data threads=5
     WSGIProcessGroup my_flask_app
     #WSGIScriptAlias /testFlask /var/www/html/benchmarkerPHP/benchmarkerPHP.wsgi
     #<Directory /var/www/html/benchmarkerPHP/>
     <Directory /var/www/html/ExampleFlask/ExampleFlask/>
                # set permissions as per apache2.conf file
            Options FollowSymLinks
            AllowOverride None
            Require all granted
     </Directory>
     ErrorLog ${APACHE_LOG_DIR}/error.log
     LogLevel warn
     CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

1 个答案:

答案 0 :(得分:0)

apache用户没有从该目录读取所需的权限。我们仅使用以下命令为另一组添加读取权限:

{{1}}