Wednesday, June 17, 2009

Django, Apache 2.2, mod_ssl, and mod_wsgi (Part 1)

Part 1 | Part 2 | Part 3

It's funny how experience changes your perspective on things. 5 years ago I tried apache and thought it was too hard. "Why can't I just have a GUI to click the settings I want?!?" I would think in frustration. Now that I'm making the transition to Django, I thought I'd give Apache another try. Wow -- What a luxury it is to be able to just write a config file to do exactly what you want to do and not have to monkey around with where some stupid settings is in an extensive maze of windows and tabs. Hah... how times change indeed.

Anyways, here's a bit about setting up Django with Apache. Here's what will be achieved:

  • Serve up Django with mod_wsgi by default from the site root
  • Serve up static media (images, css, js, etc.) from a separate virtual host
  • Offer the Django app in both HTTP and HTTPS
  • Force the Django admin to always use HTTPS
  • Use django-grappelli for a slicker looking Django admin, as well as some other goodies
  • And lastly... do all this on a Windows Server (hah)
Installing Software
If you're following along at home, here's the list of things you'll need to download for a complete deployment:
Install all of the above, accepting the defaults (unless you otherwise know what you're doing). For mod_wsgi I'll cover installation in the next part, so hang onto it for now.

Now we'll need to do 2 SVN checkouts of code...

Installing the latest Django trunk from SVN
(note: this method will have the Django development trunk as your production copy of Django. If you don't like that, then just use the installer or the tar/zip from the Django site).
  • In your python install directory (default: C:\Python26) open Lib and then site-packages
  • Create a new folder named "django"
  • Right-click on that folder and choose "SVN Checkout" (this will launch TortoiseSVN)
  • Paste this URL into that window:
    http://code.djangoproject.com/svn/django/trunk/django
You've now got a fresh copy of Django, but we need to configure Windows to be able to find Python and the Django tools.
  • Right-click "My Computer" and choose Properties (this might be slightly different for Vista+)
  • "Advanced" tab
  • "Environment Variables" at the bottom
  • In the "System variables" list, find "Path"
  • Highlight it, and hit "Edit"
  • Add this value to the end of it (change the path to your python path if it's not default):
    C:\Python26;C:\Python26\Lib\site-packages\django\bin;
Now you'll want to test to make sure this works.

Open a command prompt and type "python" and hit enter. Python should run. Ctrl+Z out of that, and type "django-admin.py help" and hit enter. It also should run.

Django and python are good to go.

Installing grappelli
This next part uses my preferences. You can do this different if you want, but keep in mind you'll need to adjust paths later on to account for your differences.

I like to install my django re-usable apps into my project directory. This adds them to the SVN trunk and I don't have to worry about if others in the office have the app installed on their local machine or not.

Anyways, open your Django project (the site you've made), and create a new folder called "grappelli". Right-click that folder and choose "SVN Checkout". Paste this URL and hit ok:

http://django-grappelli.googlecode.com/svn/trunk/grappelli

It should download a copy of grappelli. We'll look at adding it to Django in the next part.

0 comments:

  © Blogger template 'Minimalist G' by Ourblogtemplates.com 2008

Back to TOP