You are on page 1of 4

Setup Django on Windows with Apache and MySQL in 15 mins

Edit Delete Tags Autopost Django is an open source web application development framework, for python. I am totally impressed with it as it allows rapid application development and provides a brilliant auto generated administrative interface for the web app. Setting up Django on Windows without Apache & Mysql is fairly out of the box, but doing that in production is not recommended. Hence I tried to use existing portable and ready made stacks for Django on windows. Having tried them I would say, I wasted around 6 hours trying to get everything working. Bottom line: I decided to install Django on windows on my own using WAMP + mod_wsgi + mysql_python to run Django on windows. Lets see how:

Step 1: Install Python The first step is to get the latest version of Python installed. Head over to Python.org and download the stable installer version (2.6.X) for Windows. Double click and run it. The install is fairly simple, and involves clicking through the installer steps. For the rest of this post, the assumption is that installed Python in "C:\Python26\"

Step 2: Download and setup Django Download and extract the Django files. Copy the complete directory named "django", inside the extracted directory and paste it under "C:\Python26\Lib\site-packages\" Quick check: File "django-admin.py" should exist under the path "C:\Python26\Lib\site-packages\django\bin" Now to be able to run python and Django from windows command line lets add it into the path variable. Right click on My Computer and select Properties. Go to Advanced, then Environment Variables at the bottom.

Edit Path and at the end add the following: ";C:\Python26;C\Python26\scripts;C:\Python26\Lib\site-packages\django\bin\ On the Windows command line type python If you see an interactive python shell, Python is installed and running properly. To check Django installation, type import django You should see no errors.

Congratulations Django is successfully installed on your system. At this point, it would probably be a good idea to make sure that your Django install works before you attempt to get it working with Apache and mod_wsgi.

So move forward to create a basic Django project and test the setup. From the command prompt, enter into directory name where you wish to create your project: C:\ cd djangoTest

Type the following to create the dummy project: C:\djangoTest django-admin.py startproject testproject

Now lets go inside the created project and run the project: view plaincopy to clipboardprint? 1. 2. 3. 4. 5. 6.

C:\djangoTest cd testproject C:\djangoTest\testproject python manage.py runserver

You should get something like this on your screen: view plaincopy to clipboardprint? 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15.

Validating models... 0 errors found.

Django version 1.1.1, using settings 'testproject.settings'

Development server is running at http://127.0.0.1:8000/ Quit the server with CTRL-BREAK. [10/Dec/2009 20:45:50] GET / HTTP/1.1? 404 2063

Now, open your browser and type: 127.0.0.1:8000 and press enter.

If all was good, you should have your Django test page up.

Step 3. Install WAMP Download the WAMP stack. Double click and run the installer. Once again, the actual installation is simple and consists of clicking through the default installer steps. After it is installed, you will the WAMP icon in your system tray. Left click on it and select "Start all Services". After 10 seconds, left click again and click put online. Apache and MySQL should be running now.

To ensure Apache is running on port: 80, open the browser and type localhost.

You should see the WAMP page now. Congrats.

Step 4: Install mysql_python 2.6 for Mysql Download MySQL_Python2.6 module binary here Run and install the file you just downloaded. Its a simple install.

Now, lets test if the MySQL works currently with Django. Edit the database setting in testproject/settings.py. Heres what I fill in: (root is the default user) view plaincopy to clipboardprint? 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18.

DATABASE_ENGINE = mysql

DATABASE_NAME = <enter your db name here> DATABASE_USER = root DATABASE_PASSWORD = ''

DATABASE_HOST = '' DATABASE_PORT =

Run the server if it isnt started: C:\djangoTest\testproject python manage.py runserver

If you see no error messages(in the browser and the console), the database works correctly with python. To do a bit of testing, we can set our user name wrongly on purpose and watch Python throw out the errors then correct it. So far so good.

Phew now the final step...

Step 5:Install and enable mod_wsgi with Apache to run Django Download the correct mod_wsgi file based on your apache and python. If you have followed this tutorial step by step you have Apache 2.2 and Python 2.6. Rename the downloaded file as "mod_wsgi.so" Copy the renamed file mod_wsgi.so to "C:\wamp\bin\apache\Apache2.2\modules" folder. Now, open the Apache configuration file, located at "C:\wamp\bin\apache\Apache2.2\conf\httpd.conf" with notepad. Scroll down and look for a section that includes a bunch of lines that start with LoadModule. Add the following line towards the end of that section: view plaincopy to clipboardprint? 1. 2. 3.

LoadModule wsgi_module modules/mod_wsgi.so

Before we move further, copy your Django testProject folder into "C:/wamp/www". This is not mandatory, but I did this, and the remaining instructions below are based on that. Once youve got mod_wsgi installed and activated, edit your httpd.conf file and add: WSGIScriptAlias / "c:/wamp/www/testProject/django.wsgi" Next we'll need to actually create the django.wsgi WSGI application, so create a file "django.wsgi" and add: view plaincopy to clipboardprint? 1. 2. 3.

import os, sys

4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15.

sys.path.append('c:/wamp/www/') os.environ['DJANGO_SETTINGS_MODULE'] = 'testProject.settings'

import django.core.handlers.wsgi

application = django.core.handlers.wsgi.WSGIHandler()

Now restart Apache, and boom you should see your "It Worked" Page.

Congratulations!! Wasn't this easy, and just in time - 15 mins ... If this helps you, leave a comment, tweet or share this post.

PS: Updated this post with typos and other errors based on input from justin. Many thanks to him for pointing it out.