Introduction
Mod_wsgi is a simple tool which can host any python application from Apache server. This module is common in high performance websites and personal web hosting services. In this tutorial, you will learn how to use mod_wsgi to server applications on Ubuntu 12.04.
Prerequisites
For this tutorial, you have to install mod_wsgi on your system. Refer to this tutorial to install mod_wsgi module. Also I assume that you have already installed Django.
Creation of Django Application
From your home directory, create a new directory and switch into it. For simplicity, I have used the name domain here. You can replace it with your actual values:
mkdir -p ~/public_html/domain1.com cd ~/public_html/domain1.com
Once you have created the directory, create a project using django-admin.py tool as given below:
django-admin.py startproject MyTestProject
Creation of Virtual Host and WSGI File
For the Django application to function properly, Apache should send certain requests to mod_wsgi. Also mod_wsgi should be set so that it knows how to handle such requests. For accomplishing it, we will set up a virtual host which will inform Apache about the location of wsgi file.
Open the new virtual host file using the command:
sudo nano /etc/apache2/sites-available/domain1.com
Below the definition of virtual host, enter the following lines of code:
<VirtualHost *:80> ServerName domain1.com ServerAlias www.domain1.com WSGIScriptAlias / /home/username/public_html/domain1.com/MyTestProject.wsgi </VirtualHost>
Apache will use this file to pass the request to mod_wsgi.
Let’s go ahead and create the mod_wsgi file. Run the following command:
nano ~/public_html/domain1.com/MyTestProject.wsgi
Add the following lines of code to it:
import os import sys sys.path.append('~/public_html/domain1.com/') os.environ['DJANGO_SETTINGS_MODULE'] = 'MyTestProject.settings' import django.core.handlers.wsgi application = django.core.handlers.wsgi.WSGIHandler()
This basically sets up the configuration settings such as importing required modules, setting up necessary variables and setting path for mod_wsgi to work.
After the changes, enable the virtual host and restart apache:
sudo a2ensite domain1.com sudo /etc/init.d/apache2 reload
To verify the set up, view the new application from your domain in the browser.
Setting Up Static Content
Static content is not supported in virtual host definitions. As a work around, you will need to update a few settings in the file MyTestProject/settings.py:
<VirtualHost *:80> ServerName domain1.com ServerAlias www.domain1.com WSGIScriptAlias / /home/username/public_html/domain1.com/MyTestProject.wsgi Alias /static/ /home/username/public_html/domain1.com/static/ <Location "/static/"> Options -Indexes </Location> </VirtualHost>
In this, we have set the alias directives so that apache knows it shouldn’t allow Django or mod_wsgi to process anything under /static/ of your domain.
Update the settings.py file to set values for variables MEDIA_ROOT and MEDIA_URL.
nano /home/username/public_html/domain1.com/MyMyTestProject/settings.py
MEDIA_ROOT = '/home/username/public_html/domain1.com/static/' MEDIA_URL = '/static/'
Restart Apache to bring in your changes:
sudo /etc/init.d/apache2 reload
Whenever you make any changes to your project or settings, make sure you restart apache to incorporate the changes.
If you wish to access anything under MEDIA_ROOT, you can do that by accessing through http://www.domain1.com/static/pathtofile.