Last weekend I had to restart my melody.py live demo, as the EC2 instance it was on was outdated. In the process I had to figure out again how to set up Flask applications using mod_wsgi. As a relative newbie to both EC2 and Apache, the process took longer than I’d like, so I’m documenting the steps I took in detail, in part to make it easier for me next time I need to do something like this.
I’m going to break the process up into three bite-sized chunks:
- Launching an EC2 instance
- Setting up Flask and Apache
- Configuring mod_wsgi
Prerequisites
I assume you have a working Flask app that has a remote git repo, perhaps on GitHub.
Launching an EC2 Instance
First things first, let’s set up an EC2 instance running Ubuntu. I’m going to defer to Amazon’s documentation here, which is far more complete than anything I could write here.
A few things to keep in mind:
- Make sure that the security group you assign your instance to allows HTTP access (port 80) from everywhere (source 0.0.0.0/0) and allows SSH access (port 22) from your local machine’s IP.
- Make sure that your instance is assigned a Public IP (this setting may be unchecked by default).
Once the instance is created, let’s make sure that we can SSH into it:
If SSH complains about your private key file being insecure, chmod it to 0600 and try again:
Setting up Flask and Apache
Now that the EC2 instance is up and running and we can SSH into it, let’s install Apache, mod_wsgi, Flask, and git:
and restart Apache to complete the installation of mod_wsgi:
For more information on this step, see the DigitalOcean tutorial.
Configuration
First things first, we need to make a WSGI file for our application. This is the file that tells Python how to communicate with a web server. I use a very barebones WSGI file:
(for more information about this file, see the Flask mod_wsgi documentation). For convenience, I like to store this file directly in git, so save it as [appname].wsgi
in our app root directory and add it to our app’s git repo.
Now, let’s deploy this application to the server:
Now, we just need to configure Apache to use mod_wsgi
and point to our WSGI file.
Go to /etc/apache2/sites-available/
and make a new file – let’s call it amazonaws.com.conf
. This is the file that will tell Apache what to do when someone visits our site. A basic template that works for me is:
Now let’s enable the site we just configured:
Almost done! Now we just need to restart apache:
is what most tutorials suggest, but for some reason I kept seeing the default Apache loading page after this. Without digging too deeply, I solved this problem by doing:
instead.
For more information on this step, see this DigitalRiver tutorial.
If you’re made it this far, hopefully your Flask application is running correctly on EC2.
Comments
blog comments powered by Disqus