Deploying Flask app on EC2 for localhost access

I have finished up a simple Flask app that I am trying to host on an AWS EC2 instance with Apache2. I've been following this tutorial.

The only changes I've made from the development process (in which the app runs totally fine when I run it and then try to access it via localhost) are:

1) Moved all the code in to /var/www
2) Changed it so that 
        if __name__=='__main__':
   = False) #Now False instead of True
3) Added a app.wsgi file
4) Added file my_app to /etc/apache2/sites-available
5) Ran these commands:
    $ sudo a2dissite default
    $ sudo a2ensite
    $ sudo /etc/init.d/apache2 restart

Here is the app.wsgi file:

import sys 
sys.path.insert(0, '/var/www/my_app')

from app import app as application

Here is the my_app file in /etc/apache2/sites-available:

<VirtualHost *:5000>
         WSGIDaemonProcess app 
     WSGIScriptAlias / /var/www/my_app/app.wsgi

     <Directory /var/www/my_app>
            WSGIProcessGroup app 
        WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from

As you can see from the above file, I only want the flask app to be available on localhost.

When I run apache and try to access the website at I get an "Unable to connect error". I can't really figure out why. Any help would be appreciated.

Also, here is my directory structure for the Flask app itself if that's needed:

    flask/           #my virtualenv
        #Your typical virutalenv structure   #File that is a FLask entension for client-side generation of URLs
    requirements.txt    #Creates virutalenv flask


So, I got the feeling that the way I had my code set up was being problematic. So I took everything in,, and and made one big I've updated my app.wsgi to look like this:


import sys 

activate_this = '/home/achumbley/flask/bin/'
execfile(activate_this, dict(__file__=activate_this))

sys.path.insert(0, '/home/achumbley/new_flask_app')

from main import app as application

And now, my /etc/apache2/sites-available/new_flask_app looks like:

<VirtualHost *>

  WSGIDaemonProcess app 
  WSGIScriptAlias / /var/www/app.wsgi

 <Directory /home/achumbley/new_flask_app>
   WSGIProcessGroup main
   WSGIScriptReloading On
   WSGIApplicationGroup %{GLOBAL}
   Order deny,allow
   Allow from all

Finally, he is my newest directory structure:


It still does not work. But, it's possible I don't understand how to run the whole thing. I should have to run a python right? It should just be automatic, at least that is what I assumed.


Moved all the code in to /var/www

This is wrong. You need to post your code in a non-web accessible directory. Only post your static files to /var/www

Please see the official deployment guide on how to set it up with Apache and mod_wsgi. If you are having problems, you might consider this AMI image which has flask, nginx and uwsgi installed.

The nginx+uwsgi stack is also detailed in the documentation.

Here are the steps (simplified) that you need to follow:

Assume that your application is:


Then follow these instructions:

  1. Upload all your code into /home/youruser/
  2. Upload the app.wsgi file to /var/www/
  3. Upload the contents of the static directory to /var/www/static
  4. In the app.wsgi file:

    import sys
    sys.path.append(0, '/home/youruser/my_app')
    from main import app as application

Need Your Help

How can I grab the current timestamp and add minutes to it in CakePHP?

php cakephp cakephp-2.0

I am trying to add minutes to a timestamp in CakePHP. I want the timestamp to be grabbed when the form is processed.

IOT vs Heap in Oracle. Help me make choice

oracle oracle10g indexing sql-execution-plan

I've read many information about IOT, and now in my head gruel...