Tuesday, August 5, 2014

Using mod_wsgi to Serve Applications on Ubuntu 12.04

Using mod_wsgi to Serve Applications on Ubuntu 12.04

Prerequisites

Before starting on this article, be sure that you have gone through the previous 2 in this series. You can find them here:

Creating the Django Application:

First of all, we will navigate to the home directory. Create a new directory and switch into it:
mkdir  -p ~/public_html/domain1.com
cd ~/public_html/domain1.com
After that, go ahead and create a project with the help of the django-admin.py tool.
django-admin.py startproject MyTestProject

Creating the Virtual host & WSGI file:

To serve a Django app properly, it is important for Apache to know that it is supposed to forward certain types of requests to mod_wsgi. It is also important to create wsgi file that tells mod_wsgi how to handle these requests. We will setup a virtual host to accomplish these tasks. It will tell Apache the location of wsgi file and setup the file accordingly.
Open up the new virtual host file.
sudo nano /etc/apache2/sites-available/domain1.com
Next, enter below definition for the virtual host:

        ServerName domain1.com
        ServerAlias www.domain1.com
        WSGIScriptAlias / /home/username/public_html/domain1.com/MyTestProject.wsgi
Once we have instructed apache to use the wsgi file specified above and pass the receiving request to mod_wsgi, we will create the mod_wsgi file itself.
nano ~/public_html/domain1.com/MyTestProject.wsgi
Type in the following configuration:
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 definition ensures that the necessary modules will be imported. Moreover, it appends the Django project’s path to Python’s path and sets up a number of variables that helps mod_wsgi to work. Once you are done with it, you will need to enable the virtual host and restart Apache.
sudo a2ensite domain1.com
sudo /etc/init.d/apache2 reload
If everything goes as expected, you will be able to see your domain (droplet IP) in the browser, and get the newly created application. Reload Apache in case you receive any NameVirtualHost or port errors.

Static Content

There is a caveat to virtual host definitions. Static content is not supported. To serve the static content properly, you can update few settings in the file MyTestProject/settings.py and use following definition of virtual host.

        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/
        
            Options -Indexes
        
The Alias Directive lets Apache know that it should not allow Django or mod_wsgi to handle anything located under the /static/ directory of your domain. You can use any directory but make sure that it is available under /home/username/public_html/domain1.com/. In our example, the name of the directory is static. Update settings.py by setting the variables for MEDIA_ROOT and MEDIA_URL.
 nano /home/username/public_html/domain1.com/MyMyTestProject/settings.py
Find and update the settings below.
MEDIA_ROOT = '/home/username/public_html/domain1.com/static/'
MEDIA_URL = '/static/'
Finally, restart Apache to the changes into effect.
 sudo /etc/init.d/apache2 reload
You can access any items that have been placed in the MEDIA_ROOT through http://www.domain1.com/static/path/to/file.

Changes to Django App

It is a good idea to get into the habit of restarting apache every time you make changes to the project.

No comments:

Post a Comment