We are writing Django app right now. Our dev machine is OS X 10.6.8.
Our dev team decided that we should have similar environment as production so I started to set up mod_wsgi on my macbook pro.
There are a few steps that was pain to figure out hence I decided to post hoping to help others.
[APACHE]
Apache is loaded already. It should be no pain unless your home is set up for FileVault and your application directory is located under your home directory. The error I was getting due to FileVault and having my app under home directory was “Forbidden Error”.
This should not be the case for Production environment since we won’t be using OS X nor FileVault. The fix is not recommended but I think it is acceptable on Dev environment. If you know the better solution, comments would be appreciated.
The fix is to change the User and Group from www to your user name.
User www
Group www
then problem solved. How to find out your user name and group? Issue $ id from the terminal.
Oh and every time you change your httpd.conf, don’t forget to restart apache
$ sudo /usr/sbin/apachectl -k restart
[Apache - enable virtual host]
Just uncomment the line below in httpd.conf
Include /private/etc/apache2/extra/httpd-vhosts.conf
Then specify your DocumentRoot there in httpd-vhosts.conf
Drop index.html under the DocumentRoot you specified. You should see your page as you point browser to http://localhost
[Download and install mod_wsgi module]
# download
$ curl -O http://modwsgi.googlecode.com/files/mod_wsgi-macosx106-ap22py26-3.3.so
# rename
$ mv mod_wsgi-macosx106-ap22py26-3.3.so mod_wsgi.so
# copy
$ sudo cp mod_wsgi.so /usr/libexec/apache2/
Modify tthpd.conf: add the following line as last line of LoadModule
LoadModule wsgi_module libexec/apache2/mod_wsgi.so
[Install mod_wsgi]
$ curl -O http://modwsgi.googlecode.com/files/mod_wsgi-3.3.tar.gz
$ tar xvfz mod_wsgi-3.3.tar.gz
$ rm mod_wsgi-3.3.tar.gz
$ cd mod_wsgi-3.3
# make sure you will specify your default python interpreter you are using.
$ ./configure –with-python=/Library/Frameworks/Python.framework/Versions/2.7/bin/python
$ sudo make install
if ALL is good then in /private/var/log/apache2/error_log, you should see:
Apache/2.2.17 (Unix) mod_ssl/2.2.17 OpenSSL/0.9.8r DAV/2 mod_wsgi/3.3 Python/2.7.1 configured — resuming normal operations
Do some cleanup
$ make clean
add “django.wsgi” to your path_to_your_application/apache/
the script should look like
import os
import sys
path = ‘/Users/user_name/app_name‘
if path not in sys.path:
sys.path.append(path)
os.environ['DJANGO_SETTINGS_MODULE'] = ‘settings’
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
def application(environ, start_response):
status = ’200 OK’
output = “Hello World!”
response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
Then add the following line to your httpd-vhosts.conf if you setup for virtual host otherwise httpd.conf
WSGIScriptAlias / /Users/user_name/app_name/apache/django.wsgi
If you point your browser to http://localhost, you should see ”Hello World!”
Now, you want to run your app. Comment out or remove application function from django.wsgi. So it will look like this:
import os
import sys
path = ‘/Users/user_name/app_name‘
if path not in sys.path:
sys.path.append(path)
os.environ['DJANGO_SETTINGS_MODULE'] = ‘settings’
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Now refresh your browser, you should see your Django app home page! (i hope)