title: Deploying Flask app on EC2 for localhost access
body: <p>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 <a href="http://blog.garethdwyer.co.za/2013/07/getting-simple-flask-app-running-on.html" rel="nofollow">this tutorial</a>.</p>
  
  <p>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:</p>
  
  <pre><code>1) Moved all the code in to /var/www
  2) Changed it so that 
          if __name__=='__main__':
              app.run(debug = 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 sitename.com
      $ sudo /etc/init.d/apache2 restart
  </code></pre>
  
  <p>Here is the app.wsgi file:</p>
  
  <pre><code>import sys 
  sys.path.insert(0, '/var/www/my_app')
  
  from app import app as application
  </code></pre>
  
  <p>Here is the my_app file in <code>/etc/apache2/sites-available</code>:</p>
  
  <pre><code>&lt;VirtualHost *:5000&gt;
           WSGIDaemonProcess app 
       WSGIScriptAlias / /var/www/my_app/app.wsgi
  
       &lt;Directory /var/www/my_app&gt;
              WSGIProcessGroup app 
          WSGIApplicationGroup %{GLOBAL}
          Order deny,allow
          Allow from 127.0.0.1
       &lt;/Directory&gt;
  &lt;/VirtualHost&gt;
  </code></pre>
  
  <p>As you can see from the above file, I only want the flask app to be available on localhost. </p>
  
  <p>When I run apache and try to access the website at <code>my_site.com:5000</code> I get an "Unable to connect error". I can't really figure out why. Any help would be appreciated. </p>
  
  <p>Also, here is my directory structure for the Flask app itself if that's needed:</p>
  
  <pre><code>/var/www/my_app/
      app/
          __init__.py
          static/
              css/
                  bootstrap.css
              favicon.ico
              js/
                  bootstrap.js
          templates/
              base.html
              index.html
              search.html
          views.py
      app.wsgi
      flask/           #my virtualenv
          #Your typical virutalenv structure
      flask_util_js.py   #File that is a FLask entension for client-side generation of URLs
      requirements.txt
      run.py
      virtualenv.py    #Creates virutalenv flask
  </code></pre>
  
  <hr>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>So, I got the feeling that the way I had my code set up was being problematic. So I took everything in <code>run.py</code>, <code>__init__.py</code>, and <code>views.py</code> and made one big <code>main.py</code>. I've updated my <code>app.wsgi</code> to look like this:</p>
  
  <p><strong>app.wsgi</strong></p>
  
  <pre><code>import sys 
  
  activate_this = '/home/achumbley/flask/bin/activate_this.py'
  execfile(activate_this, dict(__file__=activate_this))
  
  sys.path.insert(0, '/home/achumbley/new_flask_app')
  
  from main import app as application
  </code></pre>
  
  <p>And now, my <code>/etc/apache2/sites-available/new_flask_app</code> looks like:</p>
  
  <pre><code>&lt;VirtualHost *&gt;
    ServerName dev.east.appliedminds.com
  
    WSGIDaemonProcess app 
    WSGIScriptAlias / /var/www/app.wsgi
  
   &lt;Directory /home/achumbley/new_flask_app&gt;
     WSGIProcessGroup main
     WSGIScriptReloading On
     WSGIApplicationGroup %{GLOBAL}
     Order deny,allow
     Allow from all
   &lt;/Directory&gt;
  </code></pre>
  
  <p></p>
  
  <p>Finally, he is my newest directory structure:</p>
  
  <pre><code>/home/my_username/new_flask_app/
      logging.log
      flask_util_js.py
      main.py
      static/
      templates/
  </code></pre>
  
  <p>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 <code>python main.py</code> right? It should just be automatic, at least that is what I assumed. </p>