Code


Version 9 (modified by anonymous, 9 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.

install django-fcgi.py

Get the django-fcgi.py script and put 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

django-fcgi.py 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 django-fcgi.py 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 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
cd ..; mod_fastcgi-2.4.2

install latest flup

svn co http://svn.saddi.com/flup/trunk flup
cd flup/
sudo python setup.py 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.

127.0.0.1	localhost	django.loc

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

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 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:	django-fcgi.py --settings=myproject.settings --host 127.0.0.1 --port 8882 --daemon

stop:	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/httpd/httpd.conf above. Also, the settings file argument must refer to your project settings (here: myproject).

finished!

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