Configuring multiple Flask sites with uWSGI and nginx on an Amazon EC2 Instance

For the lazy, we have a shell script that will do all this for you on GitHub!

While trying to configure our new Amazon EC2 instance, it was a little too cumbersome and somewhat poorly documented how to setup Flask with uWSGI on nginx. While there were a few great writeups on how to configure this, they fell short to get a working configuration on Amazon’s Linux AMI, which we will try to describe here.

After creating a new EC2 instance and logging in, we will need to install nginx, uWSGI and required libraries.

Now we need a Flask project! Let’s just grab the latest simple repo from GitHub:

Alternatively, you can fork the project on GitHub and pull that one–this is probably the desirable option.

We will quickly setup a virtualenv in our new simple repository.

Now let’s give it a test, shall we?

Looking good, let’s move it real quick to where we want it and fix the permissions to our liking.

Next, let’s configure uWSGI. Following Tyler’s writeup, let’s make some directories.

Now let’s create the uWSGI configuration file with:sudo vim /etc/init/uwsgi.confWith the following content:

A few things to note here, we are setting up the general uWSGI emperor log and telling it to run as the nginx user for permission purposes.

With uWSGI configured, we can start it up withsudo start uwsgi

Now we can begin to configure our simple fork which runs this blog by creating a new file with:

And we will configure it with the following content:

Now we need to link our configuration file to the enabled sites folder:

Finally, we can configure nginx to serve up our new blog.

And we will configure nginx to serve up content from our uWSGI emperor like so:

Save the file and let’s fire up nginx, we are ready for launch!

Hope that you have found this post helpful. Later we hope to describe how to automate this on Amazon EC2 for automatic scaling of your server fleet.