setup django on windows with apache and mysql in 15 mins

Upload: samdhathri

Post on 03-Apr-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/28/2019 Setup Django on Windows With Apache and MySQL in 15 Mins

    1/4

    Setup Django on Windows with Apacheand 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 applicationdevelopment 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 madestacks 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. Letssee 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 Apacheand 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 wishto create your project:

    C:\ cd djangoTest

    Type the following to create the dummy project:

    C:\djangoTest django-admin.py startproject testproject

    http://www.venkysblog.com/setup-django-on-windows-with-apache-and-mysqlhttp://www.venkysblog.com/setup-django-on-windows-with-apache-and-mysqlhttp://posterous.com/posts/edit/27981178http://posterous.com/posts/edit/27981178http://www.venkysblog.com/posts/destroy/27981178http://www.venkysblog.com/posts/destroy/27981178http://posterous.com/posts/27981178/tags/edit_multiplehttp://posterous.com/posts/27981178/tags/edit_multiplehttp://posterous.com/autopost/crosspost?post_id=27981178http://posterous.com/autopost/crosspost?post_id=27981178http://posterous.com/autopost/crosspost?post_id=27981178http://posterous.com/posts/27981178/tags/edit_multiplehttp://www.venkysblog.com/posts/destroy/27981178http://posterous.com/posts/edit/27981178http://www.venkysblog.com/setup-django-on-windows-with-apache-and-mysqlhttp://www.venkysblog.com/setup-django-on-windows-with-apache-and-mysql
  • 7/28/2019 Setup Django on Windows With Apache and MySQL in 15 Mins

    2/4

    Now lets go inside the created project and run the project:

    view plaincopy to clipboardprint?

    1.

    2.3. C:\djangoTest cd testproject4.5.6. C:\djangoTest\testproject python manage.py runserver

    You should get something like this on your screen:

    view plaincopy to clipboardprint?

    1.2.3. Validating models... 0 errors found.4.5.

    6. Django version 1.1.1, using settings 'testproject.settings'7.8.9. Development server is running at http://127.0.0.1:8000/10.11.12. Quit the server with CTRL-BREAK.13.14.15. [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 WAMPicon in your system tray. Left click on it and select "Start all Services". After 10 seconds, left click again and click put online. Apache andMySQL 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 binaryhere

    Run and install the file you just downloaded. Its a simple install.

    http://www.venkysblog.com/setup-django-on-windows-with-apache-and-mysqlhttp://www.venkysblog.com/setup-django-on-windows-with-apache-and-mysqlhttp://www.venkysblog.com/setup-django-on-windows-with-apache-and-mysqlhttp://www.venkysblog.com/setup-django-on-windows-with-apache-and-mysqlhttp://www.codegood.com/archives/4http://www.codegood.com/archives/4http://www.codegood.com/archives/4http://www.codegood.com/archives/4http://www.venkysblog.com/setup-django-on-windows-with-apache-and-mysqlhttp://www.venkysblog.com/setup-django-on-windows-with-apache-and-mysqlhttp://www.venkysblog.com/setup-django-on-windows-with-apache-and-mysqlhttp://www.venkysblog.com/setup-django-on-windows-with-apache-and-mysqlhttp://www.venkysblog.com/setup-django-on-windows-with-apache-and-mysqlhttp://www.venkysblog.com/setup-django-on-windows-with-apache-and-mysqlhttp://www.venkysblog.com/setup-django-on-windows-with-apache-and-mysqlhttp://www.venkysblog.com/setup-django-on-windows-with-apache-and-mysql
  • 7/28/2019 Setup Django on Windows With Apache and MySQL in 15 Mins

    3/4

    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. DATABASE_ENGINE = mysql4.5.6. DATABASE_NAME = 7.8.9. DATABASE_USER = root10.11.12. DATABASE_PASSWORD = ''13.14.15. DATABASE_HOST = ''16.17.18. DATABASE_PORT =

    Run the serverif 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 setour 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 thatsection:

    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

    http://www.venkysblog.com/setup-django-on-windows-with-apache-and-mysqlhttp://www.venkysblog.com/setup-django-on-windows-with-apache-and-mysqlhttp://www.venkysblog.com/setup-django-on-windows-with-apache-and-mysqlhttp://www.venkysblog.com/setup-django-on-windows-with-apache-and-mysqlhttp://www.venkysblog.com/setup-django-on-windows-with-apache-and-mysqlhttp://www.venkysblog.com/setup-django-on-windows-with-apache-and-mysqlhttp://www.venkysblog.com/setup-django-on-windows-with-apache-and-mysqlhttp://www.venkysblog.com/setup-django-on-windows-with-apache-and-mysqlhttp://www.venkysblog.com/setup-django-on-windows-with-apache-and-mysqlhttp://www.venkysblog.com/setup-django-on-windows-with-apache-and-mysqlhttp://www.venkysblog.com/setup-django-on-windows-with-apache-and-mysqlhttp://www.venkysblog.com/setup-django-on-windows-with-apache-and-mysqlhttp://www.venkysblog.com/setup-django-on-windows-with-apache-and-mysqlhttp://www.venkysblog.com/setup-django-on-windows-with-apache-and-mysqlhttp://www.venkysblog.com/setup-django-on-windows-with-apache-and-mysqlhttp://www.venkysblog.com/setup-django-on-windows-with-apache-and-mysqlhttp://www.venkysblog.com/setup-django-on-windows-with-apache-and-mysqlhttp://www.venkysblog.com/setup-django-on-windows-with-apache-and-mysql
  • 7/28/2019 Setup Django on Windows With Apache and MySQL in 15 Mins

    4/4

    4.5.6. sys.path.append('c:/wamp/www/')7.8.9. os.environ['DJANGO_SETTINGS_MODULE'] = 'testProject.settings'10.11.12. import django.core.handlers.wsgi

    13.14.15. 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.