Have a look at Nominatim ( http://wiki.openstreetmap.org/wiki/Nominatim ) It is the geocoder used by OpenStreetMap on the main map and the sourcecode for it is available in the OSM svn repository. The readme has somewhat of a step by step guide of how to set it up.