úterý 9. února 2010

Running Django on shared hosting with mod_wsgi

I have recently started working on a new hobby project (internet cookbook - only in Czech, nothing to see yet). This is written in Django and runs on shared webhosting (Web4ce). Django contains reasonable deployment documentation but unfortunately it deals only with configuring the site directly on Apache.

I can only modify .htaccess files so the deployment was a little harder than just copying a few lines of code. I had to actually google what to do :)

In the end the site is running, and this is what I did. After a few tries getting just 500 error (and not knowing where the problem was) I began with more simple steps then trying to set everything at once.

Simple WSGI script


First, to make sure that mod_wsgi is correctly supported and running, I created file hello_wsgi.wsgi:

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]


and .htaccess:

AddHandler wsgi-script .wsgi


Uploading these files to hosting and navigating to hello_wsgi.wsgi gave me my desired "Hello World!". At least something was working.
The scripts for this part were taken from http://www.rkblog.rk.edu.pl/w/p/mod_wsgi/.

Running django


Preparing django.wsgi seems simple enough once the WSGI is actually working, but the problem is when there are some other problems in configuration (as it was in my case :)).
To be able to debug these problems, I have modified the suggested WSGI configuration:

import os
import sys

os.environ['DJANGO_SETTINGS_MODULE'] = 'edesia.settings'

sys.path.append('/path/to/my/www')

import django.core.handlers.wsgi
app = django.core.handlers.wsgi.WSGIHandler()

def application(environ, start_response):
    status = '200 OK'
        app(environ, start_response)
        output = 'ok'
    except Exception, e:
        output = 'error: %s ' % str(e)

    response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]


Running on django.wsgi like this, I get all error messages and can fix the problems as they appear. After I got 'ok' as output, I only need to rename "def application" to something like "application_debug" and "app" to "application". And, voilà, I am running Django powered website.

Serving static files with Apache


The only problem remaining was to configure Apache to serve static files directly and not through Django (it's possible, but slow - see Big Fat Disclaimer here: http://docs.djangoproject.com/en/1.1/howto/static-files/#howto-static-files ). Again, the original documentation was not very helpful, so I digged into Apache mod_rewrite and this is what I came up with:


RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(site_media/.*)$ /edesia/$1 [QSA,PT,L]


These two lines put into .htaccess cause all the static content (stored under directory site_media) to be served directly by apache from /edesia/site_media directory.

The whole .htaccess file then for me (and for now) looks like this:


DirectoryIndex django.wsgi

AddHandler wsgi-script .wsgi
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(site_media/.*)$ /edesia/$1 [QSA,PT,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /django.wsgi/$1 [QSA,PT,L]


And that's it. The deployment took a little longer than I anticipated (plus I still have a lot more to do regarding deployment - see http://djangobook.com/en/2.0/chapter12/). But the site is running and that's fine with me now. Now I can concentrate on the development, find someone who will do the design and fill recipes. Hopefully by the beginning of spring I will have some reasonably complete version.

See also:
http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango
http://code.google.com/p/modwsgi/wiki/ConfigurationGuidelines

čtvrtek 21. ledna 2010

Running xubuntu 9.10 from USB

I have recently bought a new USB flash disc to replace my old 512MB one. Having it, I naturally wanted to try booting from USB to use the new free space :) I decided to try "new" xubuntu 9.10 because I did not yet upgrade my desktop computer.

I have created new flash drive by the article on Pendrivelinux (Windows install seemed easier than burning LiveCD and creating USB from that). I tried to boot up. Unexpectedly, nothing happened. I guess the booting from USB is not yet as easy as using CDs. In the end I was successful and I want to sum up the problems I had and solutions to them.
For instructions how to create bootable USB disc, see www.pendrivelinux.com.

The solutions may be specific for motherboard Gigabyte EP45-DSR and other related boards.

Problem: booting from USB does not start
Solution: Don't try to set the boot order differently. That does not work (at least for my board). When booting starts, press F12 to go to boot menu. Here, choose "Hard Drive" and select your USB with the system.
Make sure that the USB is marked as USB-HDD0, otherwise the system won't boot from it. It seems that the motherboard checks just the first USB device it finds and ignores the rest.

Problem: booting starts, but after a while, message "end_request: I/O error, dev fd0, sector 0" is displayed periodically.
Solution: The Live USB is trying to find the floppy drive. Switch off the floppy drive in BIOS. In my case, this is done in menu Standard CMOS Features. Set option "Drive A" to "None".

I hope this helps if you have similar problems.

úterý 22. prosince 2009

Simple ICQ Export script

Recently my girlfriend finally decided to stop using the original ICQ program, which she retained mostly because of the unimaginable loss of conversation history. Quick search did not reveal any good and simple means to convert the history data. Most of the software was outdated or for different versions.

We had two versions of history files - one was directory with XML files and the other seems to be Access mdb (created by ICQ version 6 I think). To sharpen my Python skills (which at the time being is a bit like trying to sharpen a club), I decided to write simple conversion script for the XML history files.

The script is published on http://code.google.com/p/icqexport/. For convenience, I have also prepared Windows build http://icqexport.googlecode.com/files/icqexport_01.zip. You are free to do anything with the script and welcome to send me notes and suggestions.

The usage is very simple, although nothing is guaranteed :) Copy the script to the directory where ICQ history files are stored and run it. It will traverse all subdirectories and convert history XML files to one readable TXT file.

The only version supported is 0x00140034 (tested on ICQ version 5.1). You're fine if there's something like this at the beginning of your files:

<root>
<version>0x00140034</version>
<event>
...

I didn't see the mdb structure yet but if I will get to it, I'll keep you informed.

pondělí 11. května 2009

Tomix - First level ready

During this weekend I planned to finish the first version of first level of our puzzle game. I had kinda slacking mood (caused partially by this great blog), but I forced myself to overcome it and in the end I was able to finish the first level as planned. There are still no graphics so I'm reusing arrows from Heartbeat, but the basic "engine" is ready. There is still a lot of work to do but being able to actually play the level is really motivational.

I handed it over to Mitsuki to try the game logic and she quickly found a solution - although not the one I had originally envisioned :) But it was possible because of a bug in checking of the final position so in the next version it won't count. But at first glance it seems that the idea is viable and I have a lot of great feedback for the game from Mitsuki. We only have to stay focused on the fact that we create puzzle game, because some ideas would lead us to completely different genre :)

So now Mitsuki will start working on the graphics to incorporate to the game. In the evening I also checked some sites with free game development resources so we could re-use something already created, but my search did not finish yet. But I have a few good sources I want to check so expect another post covering those I think are most useful.

For the next work I hope to finally start work on the editor. I kinda didn't get to that yet, but I already know how to serialize objects in Python :) And it has not that high priority for me - worst case we can easily define the levels in source code.

I also checked some libraries to create menus in pygame but unfortunately none of them seem appropriate to what I want to do so I'll probably have to write my own solution. KezMenu looks fine but it can't center the text and unfortunately it's GPL'd - and I did not decide on the license to code yet, so I may not be able to use that. Well, we'll see.

sobota 2. května 2009

Device identification problem during boot

Since installing xubuntu last autumn, it sometimes happened to me that my machine required several restarts to boot up. I have identified the problem but being linux n00b I was unable to find some solution to it for some time.

The problem was following:
I have two harddiscs in my computer - one is old IDE disc and the other is new SATA II disc. The Linux system is installed on the new disc which is usually identified as /dev/sda (where /dev/sda5 is the root partition). The other disc is usually on /dev/sdb. What sometimes happened was that the devices where identified the other way round - IDE being /dev/sda and SATA being /dev/sdb. After that the computer of course did not boot and ended in some sort of fallback console (sorry, I forgot the mode's name).

Sadly, asking administrators at work did not help me (most probably because I was not able to describe the problem correctly - the only thing I found out was that the IDE disc should be /dev/hd something and not /dev/sd something - very helpful).

Before some time I found what I thought will lead to solution, but I was hesitant to change the fstab because I didn't want to break it any more than it was :) What I needed was to identify the mount points by partition label instead of device (see http://tldp.org/HOWTO/html_single/Partition/#labels).

Today, I finally got to fixing it (at least, it appears to be fixed now :)). The whole process involved:
  • installing GParted which unfortunately did not help me with setting the partition labels and thus was not that useful (it provided me with nice overview of the partitions :))
  • setting label to ext3 partitions using tune2fs (e.g. tune2fs -L pubsw /dev/hdb1 sets label pubsw to par
  • setting labels to NTFS partitions using ntfslabel (part of ntfsprogs package)
  • modifying fstab to use the labels (see http://tldp.org/HOWTO/html_single/Partition/#labels for example fstab)
To sum it up - if you have this problem with identifying devices during boot up, use labels to identify them in fstab. This seems like a good idea generally because as I understand it the device identification can change when switching hardware (imagine adding shiny new HDD to your computer and not being able to boot up as the result - ouch!)

In the end the changes where really not that hard and if I knew a little about Linux I could've fixed this long time ago. But I'm a little smarter now and maybe after some time I will know Linux as good as Windows. Most probably after Windows 7 comes out - because last version I had is Windows XP, I will know very little about both Windows and Linux.

pondělí 20. dubna 2009

Development update

I did find a few hours to quickly whip up the first prototype of Tomix (which is the codename of the game I'm working on) and it's ready for me to play with :) Fortunately pygame is really very easy to use for what I'm planning to do and I'm also slowly getting used to vim and Python. But I'm still not very good with either of them.

Back to the prototype - now I can move the player around on the screen and push things (nothing groundbreaking yet, but what would you want for a few hours worth of programming).

I plan now try to design one level to see if my idea of combining Atomix and Sokoban is at least partially viable. Afterward I'd like to start work on the prototype of level editor which will probably take me more time - I need to learn how to work with mouse in pygame and think about reasonable way to save the edited levels (not sure how to serialize things in Python). Hopefully I can find at least one free weekend to actually implement something (all the codes till now were written on train and that is unfortunately not the ideal development environment).

Also, Mitsuki volunteered to help me with the graphics (so the size of development team has doubled :)). This means that if we eventually manage to have some game ready it will look a lot nicer than if I did it on my own. On the other hand, this also reduced number of readers of this blog that are not working on the game from 1 to 0. Maybe I should start looking for some interesting topics to write about :) (Well, I have a few, I just need to find time and mood to try something new)

středa 8. dubna 2009

Slowing down quickly

It seems that with the spring beginning, I will have less time than I originally expected. Nevertheless, I thought about the story of the puzzle game, which is planned to be roughly based on Atomix and Sokoban.

The story is not very important for this kind of game but it gave me important ideas on which to base the following puzzle. I'm not planning it to have some big impact like in PuzzleQuest (how could I compete with that?) so I will be creating (almost) pure puzzle game.

With the story boiling in my mind, I can now start looking for some free resources to use in the game (mainly music, sounds and probably some textures) and sketch the basic look & feel of the game.

I will also start working on some early prototypes with pygame because I'm still not very good with it (or Python to be honest). I hope to get good insights to the techniques I should use in the final game.

But, as I said, I expect the work to go kinda slowly for now, mainly because I won't have time during this and next weekend. On weekdays I tend to be a little too tired from regular work, but I'll try to do something when on train to work, so maybe I will move forward at least a little.