简体   繁体   English

Python Flask两个站点和使用Paramiko库错误的虚拟环境

[英]Python Flask two sites and Virtual Environment using Paramiko Library Error

i developed a Python Flask application that i would like to run in a server (RHEL 6) where another Flask application is running. 我开发了一个Python Flask应用程序,希望在运行另一个Flask应用程序的服务器(RHEL 6)中运行。 I am using httpd (apache) and mod_wsgi to achieve this. 我正在使用httpd(apache)和mod_wsgi来实现这一点。 The first application is working great without any issues. 第一个应用程序运行良好,没有任何问题。

I installed my app on /var/www/app2, and the structure of my directory is: 我在/ var / www / app2上安装了我的应用程序,目录结构为:

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

The permissions on /var/www/app2 is 755 throughout all the files and directories, and I am using a virtual environment to install with pip paramiko and flask, the user owning all this files and directories is called "user1" / var / www / app2上的所有文件和目录的权限均为755,并且我正在使用虚拟环境通过pip paramiko和flask安装,拥有所有这些文件和目录的用户称为“ user1”

The content of app2.wsgi is: 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

The virtual host on the apache server is configured as: 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>

I restarted httpd service on the Server, and when i try to connect to the new application with http://server1/app2 I get an Internal Server Error, by looking at the error.log for apache I see the following: 我重新启动了服务器上的httpd服务,当我尝试使用http:// server1 / app2连接到新应用程序时,通过查看apache的error.log出现了内部服务器错误,我看到以下内容:

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'

Again the first application does not have any problem, and if I login with the user "user1" and connect to the virtual environment and run the application with python __init__.py , i can connect without a problem on the port 5000. 同样,第一个应用程序没有任何问题,如果我使用用户“ user1”登录并连接到虚拟环境并使用python __init__.py运行该应用程序,则可以在端口5000上毫无问题地进行连接。

It seems that paramiko tries to access some libraries outside the virtual environment causing the error, i do not know why, I tried to disable SELinux and still the same result. 看来paramiko尝试访问虚拟环境之外的某些库,导致错误,我不知道为什么,我尝试禁用SELinux,结果仍然相同。

Any help will be greatly appreciated! 任何帮助将不胜感激!

Thanks 谢谢

When you use WSGIDaemonProcess directive with the intent to have a WSGI application run in daemon mode, you still need to mark what WSGI application is to run in that daemon process group. 当您使用WSGIDaemonProcess指令以使WSGI应用程序以守护程序模式运行时,您仍然需要标记要在该守护进程组中运行的WSGI应用程序。 You don't do that and so the WSGI applications are still running in embedded mode (Apache worker processes) as the Apache user. 您无需执行此操作,因此WSGI应用程序仍以Apache用户身份以嵌入式模式(Apache worker进程)运行。

You need to use either WSGIProcessGroup in appropriate context, or process-group option to WSGIScriptAlias to delegate a WSGI application to a daemon process group. 你需要为使用WSGIProcessGroup在适当的范围内,或process-group选项WSGIScriptAlias委托一个WSGI应用程序的守护进程组。

See the documentation for these directives at: 有关这些指令的信息,请参见:

Also review the documentation on best way to set up the Python virtual environment with mod_wsgi. 另请参阅有关使用mod_wsgi设置Python虚拟环境的最佳方法的文档。 You do not use the recommended way. 您没有使用推荐的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM