librelist archives

« back to archive

Flask as mod_wsgi - webfaction

Flask as mod_wsgi - webfaction

From:
Paolo Galletto
Date:
2013-06-23 @ 09:12
Hi all,
I'm experiencing problems when running Flask in conjunction with 
flask-mongoengine and flask-login. The app is based on flask-admin 
examples found on github, but largely simplified.

A tentative (but failing) deployment has been done on Webfaction as 
mod_wsgi (following instructions on 
http://community.webfaction.com/questions/12718/installing-flask ).

Quite surprisingly,

  * app works ok when run as standalone on Webfaction (via command
    "python app.py"), with this line

    app.run('127.0.0.1',  54321)

  * It does not as mod_wsgi (after having started Apache), with this
    line (instead of previous one).

    app.run()

I get an Internal Server Error, no clue why.

Any help will be greatly appreciated (since I have been working on the 
problem for a while without much progress...). So far, I have been able 
to get functioning Flask apps via mod_wsgi on Webfaction, but no longer 
in this case.

I post here code+template, sorry it's quite long...
Kind Regards and many thanks
Paolo

___________________________________________________________________________
app.py:

---------------------------------------------------------------------------------------------------------------------------------------

from  flaskimport  Flask,url_for,  redirect,  render_template,  request
from  flask.extimport  admin,  login,  wtf
from  flask.ext.loginimport  current_user
from  flask.ext.mongoengineimport  MongoEngine

DB_NAME=  'admin'
DB_USERNAME=  'bob'
DB_PASSWORD=  'x12345x'
DB_HOST=  'localhost'
DB_PORT=  12345

app=  Flask(__name__)

app.config['SECRET_KEY']  =  '123456790'

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

db=  MongoEngine(app)

# Models
class  User(db.Document):
     login=  db.StringField(max_length=80,  unique=True)
     email=  db.StringField(max_length=120)
     password=  db.StringField(max_length=64)

     # Flask-Login integration
     def  is_authenticated(self):
         return  True

     def  is_active(self):
         return  True

     def  is_anonymous(self):
         return  False

     def  get_id(self):
         return  self.id

     # Required for administrative interface
     def  __unicode__(self):
         return  self.login
  
# Initialize flask-login
def  init_login():
     login_manager=  login.LoginManager()
     login_manager.setup_app(app)

     # Create user loader function
     @login_manager.user_loader
     def  load_user(user_id):
         return  User.objects(id=user_id).first()

@app.route('/')
def  index():
     return  render_template('index.html',  user=login.current_user)

if  __name__  ==  "__main__":
     init_login()
##    app.run('127.0.0.1', 54321) # as standalone
     app.run()                    # as mod_wsgi


___________________________________________________________________________
index.html

---------------------------------------------------------------------------------------------------------------------------------------



<!DOCTYPE>
<head>
     <body>
         <div>
         {% if user and user.is_authenticated() %}
                 <h2>
                 Hello {{ user.login }}!
                 </h2>
         {% else %}
                 <h2>
                 You're not logged ... Hello anyway ...
                 </h2>
         {% endif %}
         </div>
     </body>
</head>

___________________________________________________________________________

Re: [flask] Flask as mod_wsgi - webfaction

From:
Alex
Date:
2013-07-01 @ 19:50
On Sun, Jun 23, 2013 at 11:12 AM, Paolo Galletto <
paolo@pgal.webfactional.com> wrote:

> Any help will be greatly appreciated (since I have been wo


Hi Paolo,

If the app works when using the development server It's likely that the
problem is in the web server configuration.
Have you tried enabling debug mode on your flask app and looking at the
logs?

Alex

Re: [flask] Flask as mod_wsgi - webfaction

From:
Paolo Galletto
Date:
2013-07-05 @ 10:15
Hi,

thanks for the reply...Logs weren't useful (nothing shows up, strange ?!).
However I've seen the problem and managed to bypass it, although it's 
not clear to me why now Flask works as mod_wsgi.
Here below a simplified example based on Flask-admin (*not working*)

from  flaskimport  Flask
from  flask.extimport  admin
from  flask.ext.admin.contribimport  fileadmin

app=  Flask(__name__)

@app.route('/')
def  index():
     return  '<a href="/admin/">Click me to get to Admin!</a>'

if  __name__  ==  '__main__':
     admin=  admin.Admin(app)
     app.debug=  True
##    app.run('127.0.0.1', 21958) # as standalone
     app.run()                    # as mod_wsgi


With following modification, *it does work*:

admin=  admin.Admin(app)

if  __name__  ==  '__main__':     
     app.debug=  True
  ##   app.run('127.0.0.1', 21958) # as standalone
     app.run()                    # as mod_wsgi


Notice, that, once stopped Apache and run Flask as standalone (python 
__init__.py), both situations work....

Why is that ? It's probably due to my mod_wsgi settings, but I haven't 
figured out where the problem is located...
Best regards
Paolo

>
> On Sun, Jun 23, 2013 at 11:12 AM, Paolo Galletto 
> <paolo@pgal.webfactional.com <mailto:paolo@pgal.webfactional.com>> wrote:
>
>     Any help will be greatly appreciated (since I have been wo
>
>
> Hi Paolo,
>
> If the app works when using the development server It's likely that 
> the problem is in the web server configuration.
> Have you tried enabling debug mode on your flask app and looking at 
> the logs?
>
> Alex

Re: [flask] Flask as mod_wsgi - webfaction

From:
Steven Kryskalla
Date:
2013-07-05 @ 14:40
On Fri, Jul 5, 2013 at 3:15 AM, Paolo Galletto
<paolo@pgal.webfactional.com> wrote:
> Why is that ? It's probably due to my mod_wsgi settings, but I haven't
> figured out where the problem is located...

When using mod_wsgi you do not need to ".run()" or ".serve()" your
app, mod_wsgi handles that part. In fact the "if __name__ ==
"__main__"" code probably isn't being run at all.

Mod_wsgi searches for an object called "application" in the script
file you specify in the apache configs (WSGIScriptAlias). So try
adding this to your main file (above the __name__ == '__main__' part):

application = app

Or follow the instructions here:

http://flask.pocoo.org/docs/deploying/mod_wsgi/

-Steve

Re: [flask] Flask as mod_wsgi - webfaction

From:
Filipe Cifali
Date:
2013-07-05 @ 16:09
I use @webfaction w/ mod_wsgi-3.3/python2.7

Your Flask seems fine, how you did the configuration for the application in
the control panel?


On Fri, Jul 5, 2013 at 11:40 AM, Steven Kryskalla <skryskalla@gmail.com>wrote:

> On Fri, Jul 5, 2013 at 3:15 AM, Paolo Galletto
> <paolo@pgal.webfactional.com> wrote:
> > Why is that ? It's probably due to my mod_wsgi settings, but I haven't
> > figured out where the problem is located...
>
> When using mod_wsgi you do not need to ".run()" or ".serve()" your
> app, mod_wsgi handles that part. In fact the "if __name__ ==
> "__main__"" code probably isn't being run at all.
>
> Mod_wsgi searches for an object called "application" in the script
> file you specify in the apache configs (WSGIScriptAlias). So try
> adding this to your main file (above the __name__ == '__main__' part):
>
> application = app
>
> Or follow the instructions here:
>
> http://flask.pocoo.org/docs/deploying/mod_wsgi/
>
> -Steve
>



-- 
[ ]'s

Filipe Cifali Stangler

Re: [flask] Flask as mod_wsgi - webfaction

From:
Paolo Galletto
Date:
2013-07-05 @ 17:18
I used mod_wsgi-3.4/python2.7 @webfaction and followed instructions from WF:
http://community.webfaction.com/questions/12718/installing-flask

which show how to adapt  the ~/webapps/flask_app/apache2/conf/httpd.conf
and how the index.py should look like:

import  sys
sys.path.insert(0,  '/path/to/flask_app')
from  flask_app  import  app as application


In flask_app , is located the __init__.py that was reported in my 
previous mail (with the working and not working versions).
This procedure has been working with other Flask apps, but not with 
Flask-Admin.
Notice that this same issue has been cited in :

http://www.yamdablam.com/2012/11/15/flask-admin-apache2-mod_wsgi-virtualenv-issues/

Regards
Paolo
------------------------------------------------------------------------


> I use @webfaction w/ mod_wsgi-3.3/python2.7
>
> Your Flask seems fine, how you did the configuration for the 
> application in the control panel?
>
>
> On Fri, Jul 5, 2013 at 11:40 AM, Steven Kryskalla 
> <skryskalla@gmail.com <mailto:skryskalla@gmail.com>> wrote:
>
>     On Fri, Jul 5, 2013 at 3:15 AM, Paolo Galletto
>     <paolo@pgal.webfactional.com <mailto:paolo@pgal.webfactional.com>>
>     wrote:
>     > Why is that ? It's probably due to my mod_wsgi settings, but I
>     haven't
>     > figured out where the problem is located...
>
>     When using mod_wsgi you do not need to ".run()" or ".serve()" your
>     app, mod_wsgi handles that part. In fact the "if __name__ ==
>     "__main__"" code probably isn't being run at all.
>
>     Mod_wsgi searches for an object called "application" in the script
>     file you specify in the apache configs (WSGIScriptAlias). So try
>     adding this to your main file (above the __name__ == '__main__' part):
>
>     application = app
>
>     Or follow the instructions here:
>
>     http://flask.pocoo.org/docs/deploying/mod_wsgi/
>
>     -Steve
>
>
>
>
> -- 
> [ ]'s
>
> Filipe Cifali Stangler

Re: [flask] Flask as mod_wsgi - webfaction

From:
Filipe Cifali
Date:
2013-07-05 @ 18:18
I don't use Flask-Admin, but when I was creating my setup, I noticed that
mod_wsgi-3.4 wasn't working as I was expecting, so I moved to 3.3 and it
worked w/o changes.


On Fri, Jul 5, 2013 at 2:18 PM, Paolo Galletto
<paolo@pgal.webfactional.com>wrote:

>  I used mod_wsgi-3.4/python2.7 @webfaction and followed instructions from
> WF:
> http://community.webfaction.com/questions/12718/installing-flask
>
> which show how to adapt  the ~/webapps/flask_app/apache2/conf/httpd.conf
> and how the index.py should look like:
>
> import sys
> sys.path.insert(0, '/path/to/flask_app')from flask_app import app as application
>
>
> In flask_app , is located the __init__.py that was reported in my
> previous mail (with the working and not working versions).
> This procedure has been working with other Flask apps, but not with
> Flask-Admin.
> Notice that this same issue has been cited in :
>
> 
http://www.yamdablam.com/2012/11/15/flask-admin-apache2-mod_wsgi-virtualenv-issues/
>
> Regards
> Paolo
> ------------------------------
>
>
>  I use @webfaction w/ mod_wsgi-3.3/python2.7
>
>  Your Flask seems fine, how you did the configuration for the application
> in the control panel?
>
>
> On Fri, Jul 5, 2013 at 11:40 AM, Steven Kryskalla <skryskalla@gmail.com>wrote:
>
>> On Fri, Jul 5, 2013 at 3:15 AM, Paolo Galletto
>> <paolo@pgal.webfactional.com> wrote:
>> > Why is that ? It's probably due to my mod_wsgi settings, but I haven't
>> > figured out where the problem is located...
>>
>>  When using mod_wsgi you do not need to ".run()" or ".serve()" your
>> app, mod_wsgi handles that part. In fact the "if __name__ ==
>> "__main__"" code probably isn't being run at all.
>>
>> Mod_wsgi searches for an object called "application" in the script
>> file you specify in the apache configs (WSGIScriptAlias). So try
>> adding this to your main file (above the __name__ == '__main__' part):
>>
>> application = app
>>
>> Or follow the instructions here:
>>
>> http://flask.pocoo.org/docs/deploying/mod_wsgi/
>>
>> -Steve
>>
>
>
>
>  --
> [ ]'s
>
> Filipe Cifali Stangler
>
>
>


-- 
[ ]'s

Filipe Cifali Stangler

Re: [flask] Flask as mod_wsgi - webfaction

From:
Kundan Singh
Date:
2013-07-05 @ 16:49
"Your Flask seems fine, how you did the configuration for the application
in the control panel?" I din't get you? Please be specific


On Fri, Jul 5, 2013 at 9:39 PM, Filipe Cifali <cifali.filipe@gmail.com>wrote:

> I use @webfaction w/ mod_wsgi-3.3/python2.7
>
> Your Flask seems fine, how you did the configuration for the application
> in the control panel?
>
>
> On Fri, Jul 5, 2013 at 11:40 AM, Steven Kryskalla <skryskalla@gmail.com>wrote:
>
>> On Fri, Jul 5, 2013 at 3:15 AM, Paolo Galletto
>> <paolo@pgal.webfactional.com> wrote:
>> > Why is that ? It's probably due to my mod_wsgi settings, but I haven't
>> > figured out where the problem is located...
>>
>> When using mod_wsgi you do not need to ".run()" or ".serve()" your
>> app, mod_wsgi handles that part. In fact the "if __name__ ==
>> "__main__"" code probably isn't being run at all.
>>
>> Mod_wsgi searches for an object called "application" in the script
>> file you specify in the apache configs (WSGIScriptAlias). So try
>> adding this to your main file (above the __name__ == '__main__' part):
>>
>> application = app
>>
>> Or follow the instructions here:
>>
>> http://flask.pocoo.org/docs/deploying/mod_wsgi/
>>
>> -Steve
>>
>
>
>
> --
> [ ]'s
>
> Filipe Cifali Stangler
>