Cache directory "/home/.quiterie/timc3/blog.timc3.com/wp-content/headlines" is not writable.

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:
    <VirtualHost *:8000>

    <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.

  5. Now save this file, and enable the site:
    a2ensite djangosite
  6. Now create the webhosting directory, and the symbolic link for the admin directory, and check the permissions:
    mkdir /var/www/djangosite/
    cd /var/www/djangosite/
    ln -s /usr/lib/python2.3/site-packages/django/contrib/admin/media/
    chmod -R 755 /var/www/djangosite
  7. The stop and start apache2 on your server (this is for Debian using init.d scripts, but whatever you do stopping then starting is better than a simple reload).
    /etc/init.d/apache2 stop; /etc/init.d/apache2 start

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.


10 Responses to “django with apache2”

  1. timc3 Says:

    Remember to make sure that your permissions are ok.

    Since writing this, I have decided that instead of symlinking, I am just going to checkout the python code into /usr/lib/python2.3/site-packages/django and to put all my development into /var/dev

    Perhaps I will change this in the future, I am not sure but for now that works.

  2. Dylan Swithin Says:

    *applauds*

  3. links for 2007-07-09 « PaxoBlog Says:

    [...] django with apache2 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 in (tags: django apache mod_python install howto Linux) [...]

  4. Patel Says:

    When I tried to following your steps, I am getting following error upon starting Apache2. FYI: I copy and paste your text for /etc/apache2/sites-available/djangosite file.
    —————–

    * Starting apache 2.0 web server… [Sun Aug 19 14:11:45 2007] [error] VirtualHost *:8000 — mixing * ports and non-* ports with a NameVirtualHost address is not supported, proceeding with undefined results
    ——————————–

  5. superbnerb Says:

    Just thought i’d say thanks. a year and a bit later, you’re tutorial is still used.

    thanks.

  6. pwet Says:

    You need to write “NameVirtualHost *:8000″ to the djangosite conf file :]

  7. timc3 Says:

    Is this tutorial still being used? Is it relevant?

  8. rene Says:

    Ive just used this tutorial and its still very relevant… thanks!

  9. tom Says:

    Yupp, still very useful, thanks!

  10. milo Says:

    It’s quite appreciated, I have a book that goes over how to set up Apache and Django in one part, but it doesn’t discuss the Debian/Ubuntu style of Apache conf. I could have figured it out, but having you explain how you did was much easier – thanks!

Leave a Reply