Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How deploy Flask application on Webfaction?

Tags:

python

flask

Anybody know how to deploy a simple Flask application on Webfaction? I know Webfaction support mod_wsgi and I read the guide on the Flask site but still I can't make my app working. Anybody have a working configuration?

UPDATE to answer a comment by Graham Dumpleton.

I get a 500 Internal server error. Apache does not show any error in the logs. The WSGI script is executed and seems to create the application correctly, but I keep getting a 500 error.

Thanks.

like image 728
esaurito Avatar asked Jul 18 '10 23:07

esaurito


2 Answers

I got it working with the following procedure:

  • create and app named 'myapp' of type mod_wsgi 3.3/Python 2.7. Webfaction will create the following folders:

    myapp
         |- apache2
         |- htdocs
    
  • Webfaction will also automatically create a simple script index.py in your htdocs directory. Check if the sample script work visiting the root of your newly created application (to do thin on Webfaction you need to "mount" the app on a website). If it's all OK modify the script deleting the content and adding:

    from myapp import app as application
    
  • In apache2/conf/httpd.conf add the follwing lines:

    WSGIPythonPath /home/username/webapps/myapp/htdocs/
    #If you do not specify the next directive the app *will* work but you will
    #see index.py in the path of all subdir
    WSGIScriptAlias / /home/username/webapps/myapp/htdocs/index.py
    
    
    <Directory /home/username/webapps/myapp/htdocs>
        AddHandler wsgi-script .py
        RewriteEngine on
        RewriteBase /
        WSGIScriptReloading On
    </Directory>
    
  • Restart apache2

like image 82
raben Avatar answered Sep 17 '22 00:09

raben


You need to set up a "Custom app (listening on port)" application. Make a note of the port that is assigned. Then in your Flask code, you need to put hardcode the port:

if __name__ == __main__:
    app.run(host='0.0.0.0' port=XXXXXXX)

Where you substitute XXXXXXX with the port that is randomly assigned to your custom app.

Hope that helps.

EDIT:

Please use Raben's Answer, this way should not to be used in Production.

like image 37
JoshFinnie Avatar answered Sep 17 '22 00:09

JoshFinnie