Version 9 (modified by anonymous, 10 years ago) (diff)


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

If you want to use django with mod_python on OS X, you need to install Apache2 first. This involves quite a bit of extra effort and can be difficult to get right. But since Apache 1.3 comes already installed on all OS X systems, for most Mac users, this won't be the best option. The alternative to mod_python and Apache2 is to use FCGI with Apache 1.3, but with the information provided here Django, Apache and FCGI and here django projects it isn't quite so simple to get going either.

Hence a quick 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.


Get the script and put it in /usr/local/bin.

svn co stuff
chmod a+x stuff/trunk/bin/ 
sudo mv stuff/trunk/bin/ /usr/local/bin
rm -rf stuff currently is setup to work with python 2.3 (the default installed on Mac OS X 10.3 and 10.4) When using it with python 2.3 it requires a package called python-eunuchs to provide 'socketpair'. I couldn't get this compile. However, 'socketpair' is supplied with python 2.4. To use with python 2.4 you need to change the shebang at the top of the file to point at python 2.4. YMMV

install mod_fastcgi

curl -O
tar xvfz mod_fastcgi-2.4.2.tar.gz 
cd mod_fastcgi-2.4.2
/usr/sbin/apxs -o -c *.c
sudo /usr/sbin/apxs -i -a -n fastcgi
cd ..; mod_fastcgi-2.4.2

install latest flup

svn co flup
cd flup/
sudo python install
cd ..; sudo rm -rf flup

change /etc/hosts and /etc/httpd/httpd.conf

Use your favorite editor to change /etc/hosts, adding a 'django.loc' alias for localhost. This is useful for development.	localhost	django.loc

Now, add the following lines at the end of httpd.conf:

FastCgiExternalServer /Library/WebServer/Documents/django.fcgi -host
<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]

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

Finally, give apache a restart with 'sudo apachectl restart' to make these changes effective.

link media files into docroot

This will make the django admin templates and style sheets available to Apache. 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

Use the following commands to start and stop the fcgi server:

start: --settings=myproject.settings --host --port 8882 --daemon

stop:	kill `ps auxww | grep [d]jango-fcgi | awk '{print $2}'`

Note that the port-number in the call to has to match the number that you specified in /etc/httpd/httpd.conf above. Also, the settings file argument must refer to your project settings (here: myproject).


With both Apache and running, you should now be able to access the django admin application at http://django.loc/admin. Enjoy.

Back to Top