django with apache2Computing · django · Linux · Software · Websites django
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:
apt-get install apache2
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:
Edit the ports file for apache2 to tell it to listen on more than one port:
Add this line to tell it to listen on port 8000:
Create a new sites file for this new virtual host:
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.
- Now save this file, and enable the site:
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
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.comments powered by Disqus