简体   繁体   中英

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. I am using httpd (apache) and mod_wsgi to achieve this. The first application is working great without any issues.

I installed my app on /var/www/app2, and the structure of my directory is:

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"

The content of app2.wsgi is:

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:

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:

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.

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.

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. You don't do that and so the WSGI applications are still running in embedded mode (Apache worker processes) as the Apache user.

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.

See the documentation for these directives at:

Also review the documentation on best way to set up the Python virtual environment with mod_wsgi. You do not use the recommended way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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