How to install and configure Django with mod_wsgi on FreeBSD 8.2

This is a follow up on my previous post: How to install and configure mod_wsgi on FreeBSD 8.2

I will assume you have followed all steps in the previous blog post and have FreeBSD 8.2 with Apache, Python and mod_wsgi installed and configured properly. We will use the same directory as we used for the hello world python WSGI web app in the previous article. However, we will re-create it with Django so let’s delete if first:

#rm -rf /user/local/www/myapp

Let’s install Django:

#cd /usr/ports/devel/py-pip
#make
#make install

Either refresh your shell path or reboot your virtual machine and install Django via pip:

#pip install django

Test Django installation:

#python
>>>import django

Next, I used UNIX find comman to find django-admin.py file:

#find / -name django-admin.py
/usr/local/bin/django-admin.py
/usr/local/lib/python2.7/site-packages/django/bin/django-admin.py

Let’s go to the /usr/local/www directory:

#cd /usr/local/www

And type:

#/usr/local/bin/django-admin.py startproject myapp

This will create a myapp directory with couple of files inside it, basically a standard Django project.

The only thing remaning is to configure mod_wsgi. First, edit the Apache configuration file:

#ee /usr/local/etc/apache22/httpd.conf

It should look exactly like the one in my previous blog post, the only thing changed is that I have moved the virtual host into a separate file. So delete the virtual host configuration and add an Include directive:

Include /usr/local/www/myapp/httpd.conf

I am basically telling the Apache to include contents of another httpd.conf file which will be inside the /usr/local/www/myapp folder. This way the virtual host configuration for our app will be inside the app home folder which makes way more sense. So create a new httpd.conf file:

#cd /usr/local/www/myapp
#ee httpd.conf

Configure an Apache virtual  host for our Django app:

WSGIPythonPath /usr/local/www
<VirtualHost *:80>
  ServerName localhost.home
  ServerAlias localhost.home

  DocumentRoot /usr/local/www/myapp/public

  <Directory /usr/local/www/myapp/public>
  Order allow,deny
  Allow from all
  </Directory>

  WSGIScriptAlias / /usr/local/www/myapp/wsgi.py

  <Directory /usr/local/www>
  Order allow,deny
  Allow from all
  </Directory>
</VirtualHost>

Finally, you might have noticed that our Django app is missing the wsgi.py file mentioned in the httpd.conf. Let’s create it:

#ee wsgi.py

The simplest Django app will look like this:

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

That’s it. Open the app in your browser and you should see a page like this:

4 comments

  1. Pingback: Django missing admin CSS stylesheets | Zend Framework Blog
  2. Pingback: Install PostgreSQL on FreeBSD 8.2 and make it work with Django | Zend Framework Blog
  3. Bill · January 21, 2012

    Thanks Richard, another very useful page which saved me a lot of time!

    Where you talk about the Apache httpd.conf file and say “delete the virtual host configuration and add an Include directive” – in the previous page where this is configured you have already put the Include directive into this file.

    Also the very last thing to do is restart Apache:
    # /usr/local/etc/rc.d/apache22 restart

    You will get a warning (to be expected as we have not created it yet):
    Warning: DocumentRoot [/usr/local/www/myapp/public] does not exist
    But of course the Python WSGI app works and we get the Django welcome page.

    Again, thanks for sharing all this work!

    Bill

  4. Iain · January 21

    One small problem to fix if you decide to update this site.

    I tried this with FreeBSD 9.2 and by default sqlite3 is not installed, so you get a misconfigured server error, examining the apache error log you find out django cant find sqlite3

    To fix this you need to install sqlite3, I found this could be done by installing the port /usr/ports/databases/py-sqlite3.

    Other than that a very very helpful website.