Skip to content

Instantly share code, notes, and snippets.

@geetanjaligg
Last active August 29, 2015 14:11
Show Gist options
  • Save geetanjaligg/2730cc256a43de55dd20 to your computer and use it in GitHub Desktop.
Save geetanjaligg/2730cc256a43de55dd20 to your computer and use it in GitHub Desktop.
Deploy Flask app on AWS EC2

sudo apt-get install apache2 libapache2-mod-wsgi

sudo mkdir /var/www
cd /var/www
sudo git clone url://to.your.git.repo
cd yourgitrepo
sudo nano yourflaskapp.wsgi

The .wsgi file should match the name of the Python file containing the "yourflaskapp = Flask(name)" line. Make sure that in this file (the main python file, not your newly-created .wsgi file), you have any yourflaskapp.run() calls contained within the "if name == 'main':" clause. Otherwise, your app will be starting a local WSGI server instead of forwarding it through mod_wsgi and Apache.

The yourflaskapp.wsgi file is simple and should look like the following:

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

from yourflaskapp import yourflaskapp as application
cd /etc/apache2/sites-available/
sudo nano sitename.com

where app.conf is whatever you want your site to be called. The contents of this file should look like this:

<VirtualHost *:80>
         WSGIDaemonProcess yourflaskapp
     WSGIScriptAlias / /var/www/yourflaskapp/yourflaskapp.wsgi

     <Directory /var/www/yourflaskapp>
            WSGIProcessGroup yourflaskapp
        WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from all
     </Directory>
</VirtualHost>
sudo a2dissite 000-default.conf
sudo a2ensite app.conf
sudo /etc/init.d/apache2 restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment