Swaroop's...

Hey! I am Swaroop SM. I enjoy writing code. Read more...

Deploying Python Flask on Apache using mod_wsgi

Sunday, 02 December 2012

Flask is an amazing framework for developing web applications. It is so simple that you could develop web apps on the fly. I’ve been working on Crackpot that is built using Flask and under constant development. The sole purpose of this web app was to learn and use Flask. I built a couple of cool stuff in this app, ran on the local development server and was pretty happy. But then, I wondered how do I deploy this to the Apache web server. So, I began googling about posts and all my trials were unsuccessful at the beginning and after a week long I finally got it working on my Apache web server. :)

I am currently running Ubuntu 12.04 OS and will be demonstrating with respect to it. I am also assuming that you have Apache installed in your machine. If not follow the instructions in this post: Installing LAMP on Ubuntu

Installing mod_wsgi for Apache:

Web Server Gateway Interface(wsgi) an interface between web app and web server for python. and mod_wsgi is the module that enables Apache to serve our Flask application. Open your terminal and install it using the following command:

$ sudo apt-get install libapache2-mod-wsgi

Enabling mod_wsgi

$ sudo a2enmod wsgi

Create a sample Flask app:

Assuming the following is your Application’s directory structure: Write some sample code in __init__.py. Similar to this.

FlaskApp/
	FlaskApp/
		static/
		templates/
		__init__.py

Configure a new virtual host:

Open your terminal and issue the following command:

$ sudo gedit /etc/apache2/sites-available/FlaskApp

Copy, paste the following lines into the file that you created previously:

	<VirtualHost *:80>
			ServerName localhost
			ServerAdmin webmaster@example.com

			WSGIScriptAlias / /path/to/FlaskApp/flaskapp.wsgi

			<Directory /path/to/FlaskApp/FlaskApp/>	
		  		Order allow,deny
		  		Allow from all
			</Directory>

			Alias /static /path/to/FlaskApp/FlaskApp/static

			<Directory /path/to/FlaskApp/FlaskApp/static/>
	  			Order allow,deny
	  			Allow from all
			</Directory>

			ErrorLog ${APACHE_LOG_DIR}/error.log
			LogLevel warn
			CustomLog ${APACHE_LOG_DIR}/access.log combined
	</VirtualHost>

Enabling your Virtual host:

$ sudo a2ensite FlaskApp

Create the .wsgi file:

This is the file that Apache looks for and based on this file, Apache serves your FlaskApp. Create a file called flaskapp.wsgi.

$ touch flaskapp.wsgi

Now your directory structure will look like this:

FlaskApp/
	FlaskApp/
		static/
		templates/
		__int__.py
	flaskapp.wsgi

Copy, paste the following lines of code into your flaskapp.wsgi:

#!/usr/bin/python

import sys
import logging

logging.basicConfig(stream=sys.stderr)

sys.path.insert(0,"/path/to/FlaskApp/")

from FlaskApp import app as application

Restart Apache:

$ sudo service apache2 restart

View your Application:

Open your favourite browser and view your application at: localhost

Possible Errors:

Session Error while user login or logout to your application

Flask Application requires a secret key to be defined in order to create sessions. The important thing here is you need to define your secret key in your .wsgi file. You need to define your secret key at last line in your wsgi file as follows:

#!/usr/bin/python

import sys
import logging

logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/path/to/FlaskApp/")

from FlaskApp import app as application
application.secret_key = 'Your application secret key'

comments powered by Disqus