简体   繁体   中英

ConnectionFailure when connecting to MongoHQ URI via Flask-PyMongo on httpd

I have a Flask app with a Mongo backend working perfectly in my local dev environment. I moved it to an AWS-hosted RHEL instance and got the Flask app working via httpd/mod_wsgi. However, connecting to a DB on MongoHQ continues to fail.

The code for the connection is this:

from flask import Flask
from flask.ext.pymongo import PyMongo

app = Flask(__name__)
app.config["MONGO_URI"] = 'mongodb://myusername:mypasswd@myhost.mongohq.com:myport/mydb'
mongo = PyMongo(app)

A sample query:

@app.route('/books')
def books()
    all_books = mongo.db.listings.distinct("bookinfo")
    return all_books

The error message from Apache (edited for readability):

mod_wsgi (pid=5116, process='myProcess', application=''): Loading WSGI script '/route/to/myapp.wsgi'. 
mod_wsgi (pid=5116): Target WSGI script '/route/to/myapp.wsgi' cannot be loaded as Python module. 
mod_wsgi (pid=5116): Exception occurred processing WSGI script '/route/to/myapp.wsgi'. 
Traceback (most recent call last): 
File "/route/to/myapp.wsgi", line 10, in <module>
     from myApp import app as application   
File "/route/to/myapp.py", line 8, in <module>
     mongo = PyMongo(app)
File "/route/to/my/venv/lib/python2.6/site-packages/flask_pymongo/__init__.py", line 98, in __init__
    self.init_app(app, config_prefix)
File "/route/to/my/venv/python2.6/site-packages/flask_pymongo/__init__.py", line 230, in init_app
     cx = connection_cls(*args, **kwargs)
File "/route/to/my/venv/python2.6/site-packages/pymongo/mongo_client.py", line 352, in __init__
     raise ConnectionFailure(str(e))
ConnectionFailure: could not connect to myhost.mongohq.com:myport: [Errno 13] Permission denied

My thoughts/investigation:

  1. The app runs fine when I remove the DB Connection: So it's not a mod_wsgi, Apache, or permissions issue?
  2. I can connect to the mongo uri via the Mongo shell and Python shell but not through the app: So it's not a firewall issue?
  3. MongoHQ's ultraresponsive customer service (seriously, they're awesome) believes it's an issue with the Flask-PyMongo driver, but I have no idea how to debug that.

Any help/hints/possibilities would be appreciated.

SELinux was the root cause for this. I'd disabled the firewall but never turned off SELinux by editing the /etc/sysconfig/selinux file. After doing so, the app still had issues, but this logging code helped tremendously:

import logging
file_handler = logging.FileHandler(filename='/var/www/myapp/flaskerror.log')
file_handler.setLevel(logging.WARNING)
app.logger.addHandler(file_handler)

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