Thursday, August 27, 2009

How to setup Django & Apache with mod_python and static files.

Okay so it all started up when I thought I would develop my new project with Django. After having something that I was quite happy with, I thought it was time for people to have a look at it.
To run Django on development, I was using the built-in server with python manage.py runserver 0.0.0.0:8080 which ended up being www.martincozzi.com:8080 .. not very cool !
So I thought it would be better if I run it off Apache. Since I have several websites on my server, I use Apache's VirtualHost to set them up.
So first I assume that :
  1. You have Django Installed and that you have an idea of what Django is for.
  2. You have Apache installed.
If yes, then this is the plan ! :
  1. Get your Django site to run on Apache.
  2. Serve static files off Apache and understand Why you should do this. (eg: pictures,css etc .)
1. Get your Django site to run on Apache.

Getting Django to run on Apache was not and easy thing to find, but once I understood how to do it, it's not so bad !
I store my website in the /home/martin/code/martincozzi/ folder. This folder is the root folder of my django website, which means I have the settings.py file in there.
So let's say that I want to configure my site to run on www.martincozzi.com. I need to create a virtual host first, and tell that host I want it to run from my django site folder.
<VirtualHost *>
ServerAdmin admin_contact@martincozzi.com
DocumentRoot /home/martin/code/martincozzi/
ServerName martincozzi.com
ServerAlias www.martincozzi.com
<Location "/">
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE martincozzi.settings
PythonDebug Off
PythonPath "['/home/martin/code/'] + sys.path"
</Location>
</VirtualHost>

And this is enough to get Django to run off Apache ! Add these info at the bottom of your apache conf file and restart apache :
sudo vim /etc/apache2/sites-available/default
sudo /etc/init.d/apache2 restart

Et voila ! If I go to www.martincozzi.com I now have my django website running from Apache.
2. Serve static files off Apache.

Now that I have my website running from Apache, I need to have Apache to serve the static_files as well. Why ? Because your web page will load way quicker than if the static files are being served by Django.
I store my static files in a folder that i call static_media, inside my templates file:
martincozzi/
--> templates/
--> static_media/
--> style.css
In order to do this I edit the VirtualHost I just created, and add this piece of code after </Location>.
Alias /static_media/ /home/martin/code/martincozzi/templates/static_media/
<Location "/static_media">
SetHandler None
</Location>

I have to reload Apache now :
sudo /etc/init.d/apache2 restart

My Django website is now running of Apache. Apache is also serving my static_files. I can now go to www.martincozzi.com and reload the page. The files will be served by Apache.
If you want to know how to install a Development Environment, as well as a Production environment running with Subversion(SVN), follow this post.

Martin Cozzi

No comments:

Post a Comment