Sunday, October 18, 2009

Deploying a Django Application on Apache WSGI on Windows

Following are some notes I made during my last install of a Django based web application on an Apache web server running on Windows.

This assumes a Django application is being installed on a computer with no prior Django installs. I also describe a particular example, but the same steps can be applied to many applications, just replace my application specifics with your application specifics.

  • Install Python 2.6.
    • Run the MSI installer and install in C:\Programs.
    • Create the following environment variable: PYTHON_HOME=C:\Programs\Python26 (see System Properties/Advanced/Environment Variables).
    • Append the newly created variable to your PATH variable. Also append %PYTHON_HOME%\Scripts and %PYTHON_HOME%\Lib
    • Open a command line prompt, execute the command python. This should start the Python interactive interpreter.
  • Install Django 1.1.
    • Unpack the distribution (usually a tar.gz or .zip file) in some temporary directory. You might need to install (the free and excellent) 7-Zip for this.
    • Open a command line prompt in the directory Django was unpacked in, most likely Django-1.1.
    • Execute python setup.py install (or setup.py install).
    • To check if the install went correctly, you should be able to import the Django module in Python. Start Python from the command line, and execute:
        import django
        django.VERSION
                
  • Install Apache 2.2.
    • Runs the Windows MSI installer provided from the Apache web site.
    • From a web browser, enter the URL http://localhost. A valid web page hosted be Apache should be displayed.
  • Install the modwsgi module.
    • The module file will be named something like mod_wsgi-win32-ap22py26-2.6.so (http://code.google.com/p/modwsgi/downloads/list).
    • Copy it to the modules directory of the Apache installation. E.g., C:/Program Files/Apache Software Foundation/Apache2.2/modules.
    • Rename it to mod_wsgi.so.
    • Open Apache's http.conf file.
      • Add the line LoadModule wsgi_module modules/mod_wsgi.so before all the other LoadModule entries.
      • Configure Apache for your Django project by adding the following to end of http.conf:
        # Static content
        
        Alias /media/ C:/Programs/TestDjango/mysite/media/
        
        <Directory C:/Programs/TestDjango/mysite/media/>
        Order deny,allow
        Allow from all
        </Directory>
        
        # Django dynamic content
        
        WSGIScriptAlias / C:/Programs/TestDjango/mysite/apache/django.wsgi
        
        <Directory C:/Programs/TestDjango/mysite/apache>
        Order deny,allow
        Allow from all
        </Directory>
        

        Where TestDjango is the Django project root. The paths below TestDjango will be specific to your project. This configuration serves all static media via the URL space /media/ and all the rest via WSGI and Django.

      • You may need to create the django.wsgi script if it's not already created. To do so, create a file django.wsgi and add the following to it:
            import os
            import sys
        
            sys.path.append('C:/Programs/TestDjango')
            os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
        
            import django.core.handlers.wsgi
            application = django.core.handlers.wsgi.WSGIHandler()
                      

        Note that sys.path.append('C:/Programs/TestDjango') is only needed if your project (TestDjango) is not on Python's path, PYTHONPATH.

    • Restart Apache. For this you can use the Apache Service Monitor from the taskbar tray.
    • From a web browser, attempt to hit the Django application's web page, such as http://localhost/mysite.

Thursday, October 15, 2009

Software Engineering Code of Ethics and Professional Practice

Following is the short "preamble" version of the IEEE-CS and ACM endorsed Software Engineering Code of Ethics and Professional Practice (also found on the online ethics site, as a PDF, and at the ACM site):
Software Engineering Code of Ethics and Professional Practice

Software engineers shall commit themselves to making the analysis, specification, design, development, testing and maintenance of software a beneficial and respected profession. In accordance with their commitment to the health, safety and welfare of the public, software engineers shall adhere to the following Eight Principles:

1. PUBLIC - Software engineers shall act consistently with the public interest.
2. CLIENT AND EMPLOYER - Software engineers shall act in a manner that is in the best interests of their client and employer consistent with the public interest.
3. PRODUCT - Software engineers shall ensure that their products and related modifications meet the highest professional standards possible.
4. JUDGMENT - Software engineers shall maintain integrity and independence in their professional judgment.
5. MANAGEMENT - Software engineering managers and leaders shall subscribe to and promote an ethical approach to the management of software development and maintenance.
6. PROFESSION - Software engineers shall advance the integrity and reputation of the profession consistent with the public interest.
7. COLLEAGUES - Software engineers shall be fair to and supportive of their colleagues.
8. SELF - Software engineers shall participate in lifelong learning regarding the practice of their profession and shall promote an ethical approach to the practice of the profession.

IEEE-CS/ACM Joint Task Force on Software Engineering Ethics and Professional Practices (version 5.2)

Wednesday, October 14, 2009

The Duct Tape Programmer Posts (Joel and Bob)

What is the Duct Tape Programmer? He is the smart, pragmatic Keep It Simple Stupid (KISS) software developer, whose top priority is to ship as soon as possible a "good enough" product (but not garbage) that satisfies the users needs. At least that's my summary from the original "The Duct Tape Programmer" blog post, by Joel Spolsky and Uncle Bob's comments on that post.

Some main points from both of these posts:
  • Keep it simple. Using advanced, usually more complicated, tools and designs (e.g. COM, multithreading, C++, templates) only when absolutely necessary. Be careful of over using a tool and of over engineering the solution.
  • Good (smart) software developers know when the aforementioned tools are needed and when to avoid them, and are not afraid to make the decision to use or not use them, or assert there views on the matter.
  • Be pragmatic: "A 50%-good solution that people actually have solves more problems and survives longer than a 99% solution that nobody has" [Joel].
  • One of the most important, perhaps the most important, feature of a (software) product is to ship the product as soon as possible. "It's great to rewrite your code and make it cleaner and by the third time it'll actually be pretty. But that's not the point --- you're not here to write code; you're here to ship products" [Jamie Zawinski, from Joel]. Nevertheless, don't ship garbage: "The programmer who spends weeks building the perfect structure does just as much harm to the project as the programmer who hacks a bunch of crap together. Neither have struck the balance that's required" [Bob]

Here are some follow-ups by Bob:
Echoes from the Stone Age
TDD Derangement Syndrome