django with apache2

         · · · ·     

One thing that I feel is harder than it should be with django is getting it working with Apache2. Sure the built in webserver is nice for development, but there comes a time when you need to step up and use a webserver for development that you can also deploy the site upon.

I have used Apache a lot in the past, but recently I have decided to move over to installing everything the Debian way, and this will reflect that. I am going to use python2.3, Apache2 and the version of mod_python that is in stable. You can follow the instructions if you have installed Apache2 and mod_python in otherways after this step if you wish.

To get Apache2 on debian with mod_python, simply:

  1. apt-get install apache2
  2. apt-get install libapache2-mod-python

For the rest of the tutorial, I am assuming that you have Django setup correctly, and working with your database. If not, I recommend installing the development version. It is easy to update using svn and there are some features that are better than the stable version I find. Once you have done this you can start to build your site. From then on you can create your apache virtualhost file:

I have decided to run my django site on a different port, but of course you can run it on different ipaddresses, and with different hostheaders. All you need to do is follow the apache 2 documentation for this. I feel this is already well documented so I will just show how I did it by running on different ports:

  1. Edit the ports file for apache2 to tell it to listen on more than one port:

    vi /etc/apache2/ports.conf

  2. Add this line to tell it to listen on port 8000:

    Listen 8000

  3. Create a new sites file for this new virtual host:

    vi /etc/apache2/sites-available/djangosite

  4. Now we need to put in the configuration details:

    <location "/">
    SetHandler mod_python
    PythonHandler django.core.handlers.modpython
    PythonPath "['/home/tim/dev/projects/'] + sys.path"
    SetEnv DJANGO_SETTINGS_MODULE djangosite.settings
    PythonDebug On
    </location>
    
    Alias /media "/var/www/djangosite/media"
    <location "/media/">
    SetHandler None
    </location>
    
    <locationmatch "\.(jpg|gif|png)$">
    SetHandler None
    </locationmatch>
    
    </virtualhost>
    

    This gives us several things:

    • Sets up a virtualhost to listen on port 8000.
    • For the virtual host use mod_python.
    • PythonHandler is told to use Django.
    • Our PythonPath is updated with our site files, in my case my django projects are located in /home/tim/dev/projects/ so this is what I use. Point this to where your django files are kept.
    • Let django know to use the settings file for this project
    • The we setup an alias to serve the media files, as they don’t need to be served through mod_python we turn the handler off for the /media/ location. I will setup my media files in /var/www/djangosite/media
    • Finally tell apache2 not to use mod_python to serve image files.

Once you have done that, check your site by point a webbrowser to the the machine that you have apache2 running on, but at port 8000 for instance if it is a localhost, tryhttp://127.0.0.1:8000.

One thing that I haven’t talked about is where it is best to place all the files used in creating a django site, and I have yet to read any documentation about this. This is why in the above I am point it to a dev subdirectory from my home directory. I don’t think this is a good idea for a production site, and perhaps it should go in somewhere off /var.

One thing is for sure, creating seperate directories for media and the actual files is a good idea, and you if you follow the django instructions you can take them up on the recommendation of using a seperate lightweight webserver to just serve these files.

comments powered by Disqus