WebFaction
Community site: login faq

Deploying a flask app in conjunction with MongoDB on Webfaction is keeping my days busy....

I have followed the usual procedure that works for standard Flask apps (which are not based on MongoDB), through mod_wsgi.

Mongod is started acccording to Webfaction instructions.

It happens that this procedure ultimately fails with MongoDB: although a connection is opened (connection accepted ... (1 connection now open with the database)), I get an Internal Server Error when pointing at the website.

I've managed to avoid the error by stopping the Apache instance and running the app on the assigned port, like this:

python app.py

where last line of app.py is:

app.run('127.0.0.1', xxxxx)

with xxxxx being the port assigned by webfaction control panel. I get an open connection and authentication now works (=1), while previously this last step had failed.

In other words, it seems that one has to avoid installing an Apache instance (via mod_wsgi) and should rather create a "Custom App Listening on Port" for Flask.

I'm not aware of why one has to proceed like this and if this is indeed the only option.

Any comment/hint on how one can combine Flask and MongoDB on Webfaction will be very appreciated.

Thanks in advance Regards pgal

asked 13 Jun '13, 06:26

pgal
113
accept rate: 0%

edited 13 Jun '13, 07:00


You don't need a 'custom app listening on port' application to run Flask and MongoDB. mod_wsgi works with no problems.

I just set up a test app following our Installing Flask instructions precisely, then took the following additional steps:

  • Installed mongoengine and flask_mongoengine. The versions that were installed were mongoengine-0.8.2-py2.7 and flask_mongoengine-0.7.0-py2.7.
  • Added a MongoDB connection to my Flask app's __init__.py

My __init__.py looks like this:

from flask import Flask
from flask.ext.mongoengine import MongoEngine

DB_NAME = 'mydb'
DB_USERNAME = 'mydbuser'
DB_PASSWORD = 'mydbpassword'
DB_HOST = 'localhost'
DB_PORT = 54321

app = Flask(__name__)

app.config['MONGODB_SETTINGS'] = {
    'NAME':DB_NAME,
    'USERNAME':DB_USERNAME,
    'PASSWORD':DB_PASSWORD,
    'HOST':DB_HOST,
    'PORT':DB_PORT
}

db = MongoEngine(app)

@app.route("/")
def hello():
    return "Hello World from flask!"

if __name__ == "__main__":
    app.run()

I then reload the app, and it works with no errors.

That said, I did run into one problem while testing this. The Flask-MongoEngine docs say that you should use "DB" to specify the database name in your MONGODB_SETTINGS dict. However, when I did that, I got errors like "ConnectionError: Cannot connect to database default". That error tells me that mongo isn't seeing the database name that I have specified in my code, so it's looking for the default db instead.

I looked at the connection code in mongoengine and saw that it's expecting a key named "name" for the database name, so I used "NAME" in my MONGODB_SETTINGS dict.

After I made that change, my app works with no problems. No errors when I load the page, and my Mongo console output clearly shows the connections happening.

None of this explains why your app would work as a standalone app and not as a mod_wsgi app, but if you want to run Flask on mod_wsgi, this is what worked for me.

permanent link

answered 13 Jun '13, 18:31

seanf
12.2k41836
accept rate: 37%

edited 13 Jun '13, 19:38

Your example works also for me (by using "DB" and not with "NAME"). Thanks ! By the way, I still have problems with my app in mod_wsgi, while it works as standalone ... Your working example indicates that I have to search in other directions.

(16 Jun '13, 01:31) pgal
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×55
×44
×29

question asked: 13 Jun '13, 06:26

question was seen: 4,561 times

last updated: 16 Jun '13, 01:34

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2019 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM