This is an update to the very useful although outdated post found here. I was able to use the post there to build updated step-by-step instructions on how to successfully install Django on a Ubuntu 11.10 Linux server using mod_wsgi that I set up on a Rackspace cloud server.
PART 1 – Prepare the server
Update the server
> sudo apt-get update > sudo apt-get upgrade
Install Apache and mod_wsgi
> sudo apt-get install apache2 libapache2-mod-wsgi
Install setup tools and pip
> sudo apt-get install python-setuptools > sudo apt-get install python-pip
Install Django
> sudo pip install django
Select the folder where you will store your sites
For example, I placed our sites in the /var/www directory.
PART 2 – Add host entries for testing
Set up two domains to test the configuration:
- one to test that WSGI is working, and
- one to test that Django is working.
My test virtual machine’s IP address is 50.56.182.71 Set up the following in your local hosts file (not on the server).
> sudo nano /etc/hosts
And add the following (using your actual IP address):
50.56.182.71 djangoserver 50.56.182.71 wsgi.djangoserver 50.56.182.71 hello.djangoserver
PART 3 – Test WSGI
Create your wsgi test site
> sudo mkdir /var/www/wsgi > sudo nano /var/www/wsgi/app.wsgi
Then add content:
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]
Create a new apache site
> sudo nano /etc/apache2/sites-available/wsgi
Then add content:
<VirtualHost *:80>
ServerName wsgi.djangoserver
DocumentRoot /var/www/wsgi
<Directory /var/www/wsgi>
Order allow,deny
Allow from all
</Directory>
WSGIScriptAlias / /var/www/wsgi/app.wsgi
</VirtualHost>Now it’s time to test. First, activate the site:
> sudo a2ensite wsgi > sudo /etc/init.d/apache2 reload
Then open your web browser and browse to
http://wsgi.djangoserver
You should see a ‘Hello World!’ message
PART 4 – Test Django
Create a new Django site
> cd /var/www > sudo django-admin.py startproject hello
Create a wsgi file for the site
> sudo mkdir /var/www/hello/apache > sudo nano /var/www/hello/apache/django.wsgi
Then add content
import os import sys path = '/var/www' if path not in sys.path: sys.path.insert(0, '/var/www') sys.path.insert(0,os.sep.join(os.path.abspath(__file__).split(os.sep)[:-2]))
os.environ['DJANGO_SETTINGS_MODULE'] = 'hello.settings'
import django.core.handlers.wsgi application = django.core.handlers.wsgi.WSGIHandler()
Create a new apache site
> sudo nano /etc/apache2/sites-available/hello
Then add content
<VirtualHost *:80>
ServerName hello.djangoserver
DocumentRoot /var/www/hello
<Directory /var/www/hello>
Order allow,deny
Allow from all
</Directory>
WSGIDaemonProcess hello.djangoserver processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup hello.djangoserver
WSGIScriptAlias / /var/www/hello/apache/django.wsgi
</VirtualHost>Activate the site
> sudo a2ensite hello > sudo /etc/init.d/apache2 reload
Then open your web browser and browse to
http://hello.djangoserver
You should see the Django default installation message.
Notes:
NOTE 1 – Running in daemon mode
The test Django site we used for this demonstration is configured to run in daemon mode – because of these two lines:
WSGIDaemonProcess hello.djangoserver processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup hello.djangoserverYou can modify the code to avoid having to restart apache. After this modification you can just touch the wsgi file and the changes will be updated automatically:
> sudo touch /var/www/hello/apache/django.wsgi
NOTE 2 – Specify the application module name
It’s a good idea to specify the application module when specifying the DJANGO_SETTINGS_MODULE. So rather than writing this:
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
write the following:
os.environ['DJANGO_SETTINGS_MODULE'] = 'hello.settings'
Useful Commands
Error log file
If you encounter errors, check the apache log file
> tail /var/log/apache2/error.log
Test using development mode
If your app does not seem to be working using wsgi, then check whether it is working via the development server. Enter the following:
> cd /var/www/hello > python manage.py runserver 0:8080
Then in your web browser go to
http://hello.djangoserver:8080
References
An improved WSGI script for use with Django.
http://blog.dscpl.com.au/2010/03/improved-wsgi-script-for-use-with.html
Modwsgi – Integration With Django
http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango
How do I stop getting ImportError: Could not import settings ‘mofin.settings’ when using django with wsgi?
Configuration problems with django and mod_wsgi
http://stackoverflow.com/questions/2587251/configuration-problems-with-django-and-mod-wsgi













{ 2 comments… read them below or add one }
there is a “new line character” missing in the content of /var/www/hello/apache/django.wsgi. This line:
sys.path.insert(0,os.sep.join(os.path.abspath(__file__).split(os.sep)[:-2])) os.environ['DJANGO_SETTINGS_MODULE'] = ‘hello.settings’
should be 2 lines (new one starting at os.environ).
For the rest, perfect, it works at first try. thanx
thanks David! I’ll make the update in the post. It worked for me without the newline, but I’m sure it can’t hurt!