Code


Version 6 (modified by captnswing, 8 years ago) (diff)

this should be it. for tonight anyway

Getting Django to work with Apache 1.3 and FCGI on Mac OS X

On OS X, using django with mod_python requires you to install Apache2, which involves quite a bit of extra effort to get it right. So for most of us Mac users, since Apache 1.3 is already installed, this is not an option. At the same time, the information contained here Django, Apache and FCGI and here django projects is a bit incomplete from a Mac users perspective.

So here is a summary of the steps needed to get Django working with FCGI and Apache 1.3 on Mac OS X: It assumes that your django project is called myproject. Adjust the following accordingly.

install django-fcgi.py

download the django-fcgi-py script and install it in /usr/local/bin

svn co https://simon.bofh.ms/django-projects/stuff stuff
chmod a+x stuff/trunk/bin/django-fcgi.py 
sudo mv stuff/trunk/bin/django-fcgi.py /usr/local/bin
rm -rf stuff

install mod_fastcgi

straightforward apache module install

curl -O http://www.fastcgi.com/dist/mod_fastcgi-2.4.2.tar.gz
tar xvfz mod_fastcgi-2.4.2.tar.gz 
cd mod_fastcgi-2.4.2
/usr/sbin/apxs -o mod_fastcgi.so -c *.c
sudo /usr/sbin/apxs -i -a -n fastcgi mod_fastcgi.so

install latest flup

again, this is straightforward

svn co http://svn.saddi.com/flup/trunk flup
cd flup/
sudo python setup.py install
cd ..
sudo rm -rf flup

change /etc/httpd/httpd.conf

use your favorite editor to add the following lines at the end:

FastCgiExternalServer /Library/WebServer/Documents/django.fcgi -host 127.0.0.1:8882
	
<VirtualHost *:80>
    Servername django.loc
    ErrorLog /var/log/httpd/django-error.log
    CustomLog /var/log/httpd/django-access.log combined
    DocumentRoot /Library/WebServer/Documents/
    RewriteEngine On
    RewriteRule ^(/admin.*)$ /django.fcgi$1 [L]
    RewriteRule ^(/myapp.*)$ /django.fcgi$1 [L]
</VirtualHost>

You'll need such a RewriteRule line for every application you want to access (if your application name is something else that myapp, make sure to change that above).

change /etc/hosts

add a 'django.loc' hostname, good for development

127.0.0.1	localhost	django.loc

give apache a restart with 'sudo apachectl restart'

link media files into docroot

so the django admin templates and style sheets are found. First, change into your django_src directory. And then:

ln -s `pwd`/django/contrib/admin/media /Library/WebServer/Documents/media

Now, change into your django project root (i.e. the directory that contains myproject), and link that into site-packages

sudo ln -s `pwd`/myproject /Library/Python/2.3/site-packages/myproject

Basically, import myproject should work now on the python prompt

starting and stopping

the following commands start the fcgi server, and stop it as well

django-fcgi.py --settings=myproject.settings --host 127.0.0.1 --port 8882 --daemon
kill `ps auxww | grep [d]jango-fcgi | awk '{print $2}'`

Note that the port-number in the call to django-fcgi.py has to match the number that you specified in /etc/http/http.conf.

finished!

with Apache and django-fcgi.py both running, you should be able to access the django admin at http://django.loc/admin. Enjoy.