So, I actually haven't written any blog entry since my decision to do so 4 years ago.
Banging my head on my desk for a couple of hours, trying to understand wtf is going on with django and apache, actually made me decide to write my first entry today.
So here goes.
First thing first - why would you want to use apache HTTP web server with Django?
Two main reasons:
- Apache is one of the most successful and professional open source community, and their HTTP web server is one of the most powerful and used web servers in the world.
- Django's integrated web-server is intended to be used only in the development phase.
I was under the impression that hooking apache in to replace Django's web server is going to be easy.
I was wrong.
There are a couple of resources out there on doing this, but none helped me solve this; they all just provided another piece of the puzzle.
I'm sure that you can google and find all the stuff that I have, so I'll just skip the references part, and just write what I did to make it work.
Step 1 - install the needed software
For me, this meant installing apache (obviously) and mod_wsgi.
I'm using Fedora 16 as my workstation, so this was fairly easy:
sudo yum install httpd
sudo yum install mod_wsgi
At one point I was so frustrated from not being able to get this thing going, that I actually tried going with mod_python, only to find out later that it's
not supported anymore.
I'm stating this just because I'm not sure if installing mod_python has something to do with the all thing working, but just in case, I also did:
sudo yum groupinstall "Web Server"
sudo yum install mod_ssl mod_python
Again, I don't think that this is a necessary step.
Step 2 - configure apache
In Fedora, apache's configuration file is /etc/httpd/conf/httpd.conf.
Aside from the usual basic configuration that you may want to apply, the most notable change that I had to do is adding mod_wsgi to the list of loaded modules:
LoadModule wsgi_module modules/mod_wsgi.so
You need to restart apache after saving your changes:
sudo service httpd restart
Step 3 - RTFM
Read Django's
documentation on how to make apache work with Django.
Of course that this is not enough, or else I wouldn't have to write this post.
After reading some security-related articles, I've decided to put my django.wsgi in a new directory - /usr/local/www/wsgi-scripts/.
I've added this path to httpd.conf, like the documentation indicates:
WSGIScriptAlias / /usr/local/www/wsgi-scripts/django.wsgi
My django.wsgi looks like this:
import os
import sys
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
path = '/home/apache/myproject'
if path not in sys.path:
sys.path.append(path)
If you've followed Django's documentation, you might notice that I have two things that are different than the ones indicated there.
These two things are what made my setup eventually work.
The first thing is the line that set the DJANGO_SETTINGS_MODULE environment variable.
The documentation states that 'mysite.settings' should be used.
I tried wrapping my head around this mysite.settings thing, but I just couldn't understand what does it mean - is mysite should be replaced by my own site's name? Is it just a constant that should be used as-is?
Let me make things short - I still don't know why 'mysite' is being used in the example, but what worked for me was just 'settings'.
After trying to understand if 'settings' have anything to do with the file 'settings.py' in your site's directory, I've come to a conclusion that this "special" settings file is being generated by Django when it's starting to run, by using the information from the file 'settings.py'.
The other thing that is different, is the path to the application code.
I've giving up trying to put the code under my user's name, since apache kept shouting that it can't find stuff (even though my python path variable was set appropriately). I've came to a conclusion that this is a permission problem, and being the n00b system-admin that I am, I've decided to solve this by creating a home directory for the apache user.
When you install apache (httpd), an 'apache' user and an 'apache' group is created in the system, for all the web-servicing purposes.
I've simply copied my code into apache's home directory and changed the ownership to apache:
sudo cp -r /home/tomer/code/myproject /home/apache/
sudo chown -R apache /home/apache/myproject
sudo chgrp -R apache /home/apache/myproject
Tips and tricks
- You should always have apache's log tailed, so you can see all the error messages it outputs. In Fedora, the log files are by default in /etc/httpd/logs, so you should:
tail -f /etc/httpd/logs/error_log
- After moving the files to apache's home directory, you might need to remove all the .pyc files and re-run the app, so new settings (if there are any) will take effect.
I think that Django's documentation is top-notch. However, in this case, I felt like the specific subject was left for the more advanced sys-admins.
I hope that this article will help all the other n00bs out there.
Or at least one, cuz I know I'm not the only one... :)