Python Flask使用Paramiko Library Error

时间:2016-12-21 15:45:21

标签: python apache flask mod-wsgi paramiko

我开发了一个Python Flask应用程序,我想在运行另一个Flask应用程序的服务器(RHEL 6)中运行。我使用httpd(apache)和mod_wsgi来实现这一点。第一个应用程序运行良好,没有任何问题。

我在/ var / www / app2上安装了我的应用程序,我的目录结构是:

app2
  |_ app2
  |   |_ config.ini
  |   |_ __init__.py
  |   |_ static
  |   |_ templates
  |   |_ venv
  |_ app2.wsgi

/ var / www / app2的权限在所有文件和目录中都是755,我使用虚拟环境来安装pip paramiko和flask,拥有所有这些文件和目录的用户称为“user1”< / p>

app2.wsgi的内容是:

import sys
import logging

activate_this = '/var/www/app2/app2/venv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/app2/")

from app2 import app as application

apache服务器上的虚拟主机配置为:

NameVirtualHost *:80
<VirtualHost *:80>
                ServerName server1
                ServerAdmin webmaster@example.org
                WSGIDaemonProcess app1 user=user1 group=user1 threads=5
                WSGIScriptAlias /app1 /var/www/app1/app1.wsgi
                <Directory /var/www/app1/app1/>
                                Order allow,deny
                                Allow from all
                </Directory>
                Alias /app1/static /var/www/app1/app1/static
                <Directory /var/www/app1/app1/static/>
                                Order allow,deny
                                Allow from all
                </Directory>

                WSGIDaemonProcess app2 user=sat_user group=sat_user threads=5
                WSGIScriptAlias /app2 /var/www/app2/app2.wsgi
                <Directory /var/www/app2/app2/>
                                Order allow,deny
                                Allow from all
                </Directory>
                Alias /app2/static /var/www/app2/app2/static
                <Directory /var/www/app2/app2/static/>
                                Order allow,deny
                                Allow from all
                </Directory>

                ErrorLog /var/log/httpd/error.log
                LogLevel warn
                CustomLog /var/log/httpd/access.log combined
</VirtualHost>

我重新启动了服务器上的httpd服务,当我尝试使用http://server1/app2连接到新应用程序时,我收到内部服务器错误,通过查看error.log获取apache,我看到以下内容:

mod_wsgi (pid=8205): Target WSGI script '/var/www/app2/app2.wsgi' cannot be loaded as Python module.
mod_wsgi (pid=8205): Exception occurred processing WSGI script '/var/www/app2/app2.wsgi'.
Traceback (most recent call last):
  File "/var/www/app2/app2.wsgi", line 10, in <module>
    from app2 import app as application
  File "/var/www/app2/app2/__init__.py", line 2, in <module>
    import paramiko
  File "/var/www/app2/app2/venv/lib/python2.6/site-packages/paramiko/__init__.py", line 30, in <module>
    from paramiko.transport import SecurityOptions, Transport
  File "/var/www/app2/app2/venv/lib/python2.6/site-packages/paramiko/transport.py", line 33, in <module>
    from cryptography.hazmat.backends import default_backend
  File "/var/www/app2/app2/venv/lib/python2.6/site-packages/cryptography/hazmat/backends/__init__.py", line 7, in <module>
    import pkg_resources
  File "/var/www/app2/app2/venv/lib/python2.6/site-packages/pkg_resources/__init__.py", line 3019, in <module>
    @_call_aside
  File "/var/www/app2/app2/venv/lib/python2.6/site-packages/pkg_resources/__init__.py", line 3003, in _call_aside
    f(*args, **kwargs)
  File "/var/www/app2/app2/venv/lib/python2.6/site-packages/pkg_resources/__init__.py", line 3032, in _initialize_master_working_set
    working_set = WorkingSet._build_master()
  File "/var/www/app2/app2/venv/lib/python2.6/site-packages/pkg_resources/__init__.py", line 646, in _build_master
    ws = cls()
  File "/var/www/app2/app2/venv/lib/python2.6/site-packages/pkg_resources/__init__.py", line 639, in __init__
    self.add_entry(entry)
  File "/var/www/app2/app2/venv/lib/python2.6/site-packages/pkg_resources/__init__.py", line 695, in add_entry
    for dist in find_distributions(entry, True):
  File "/var/www/app2/app2/venv/lib/python2.6/site-packages/pkg_resources/__init__.py", line 2012, in find_on_path
    if len(os.listdir(fullpath)) == 0:
OSError: [Errno 13] Permission denied: '/usr/lib64/python2.6/site-packages/tornado-4.4.1.dist-info'

同样第一个应用程序没有任何问题,如果我使用用户“user1”登录并连接到虚拟环境并使用python __init__.py运行应用程序,我可以在端口5000上连接而没有问题

似乎paramiko试图访问虚拟环境之外的一些库导致错误,我不知道为什么,我试图禁用SELinux并且仍然是相同的结果。

任何帮助将不胜感激!

由于

1 个答案:

答案 0 :(得分:0)

当您使用WSGIDaemonProcess指令以使WSGI应用程序以守护程序模式运行时,您仍需要标记要在该守护程序进程组中运行的WSGI应用程序。您不会这样做,因此WSGI应用程序仍以嵌入模式(Apache工作进程)作为Apache用户运行。

您需要在适当的上下文中使用WSGIProcessGroup,或者process-group使用WSGIScriptAlias选项将WSGI应用程序委派给守护程序进程组。

请参阅以下网址获取这些指令的文档:

另请查看有关使用mod_wsgi设置Python虚拟环境的最佳方法的文档。你没有使用推荐的方式。