Amazon EC2, Flask, apache, mod_wsgi, virtualenv. One of the complete recipe of practical Python application deployment. And here is how you can install, setup and deploy your application.

Prepare your server

Check if your system has mod_wsgi installed or not.

If your module is found here something you should get in your console.

if wsgi_module is present, that means you already have wsgi module installed. You can skip this part.

To install mod_wsgi for apache. For, my case, on Amazon Linux Distro you can look up your module name by simply call:

This will give you the list of installable items. There you can pick the module appropriate to your build and just install it. For me …

apache, mod_wsgi, virtualenv

Before we dig down deeper, here is a very simple linkage between all these stuff.

In development, all you need to do is run your web application and call it directly on designated ports. But in production, you should have a web server process through requests first, and pass on those to your web application written in python. The web server such as apache will manage and handle requests, do the load balance, and much more. Question is how can we do that?

In python, mod_wsgi will handle this for you. So here is a big picture in production site.

Basically request will be intercepted by Apache, then mapped to appropriate virtual host. Relay the request through the given configuration and directives. Apache start daemon (if not yet started) as python instance to handle that request. Response is then produced from Python app and replied back to Apache, and eventually returns to client.

So now here is detailed instruction per each part.

Prepare your Python production virtual environment

Now depends on how your prep your code. In my case, I’ve developed my code in my localhost with its own virtualenv. So to transfer required modules we use requirements.txt or in short.

This requirements.txt is a snapshot of modules required in execution site packages. I have committed this file in my repo. So when deploy I can just clone this on production server and ready to install required modules by pip.

In your production server, deploy/clone your code to directory of your choice. For example: /var/www/flask_application/. Then create a virtualenv for execution environment.

That’s it. Don’t forget to test your application before doing anything else.

Note in flask, in your entry point of your code, don’t forget to do:

As this portion of code will get runs on development when you run this application manually. In production __name__ will not equals to __main__ as your application will be invoked by wsgi. So here your can configure your app to run as you like.

Prepare .wsgi file

Essentially *.wsgi is another python file that invokes a WSGI compliance application. This wsgi will be invoked and handled by apache. You can see this as an entry point to start your application, and that’s mean if you would like to run your app under virtualenv you need to handle it here as well.

Again, I created this file, name it deploy.wsgi, and commit it in my repository just for tracking sake. Here is the content of the file.

So this file resided here: /var/www/flask_application/deploy.wsgi

Configure Apache

Lastly it is time to configure apache. With mod_wsgi install, we can to relay requests from Apache to our python applications. So let’s edit virtual host file.

Here is the example of virtualhost configuration that bind your flask application located in /var/www/my_flask_application/ to subdomain called flask.

reference: flask-apache-wsgi, mod_wsgi