User:Kodapan/Howto mod tile

From OpenStreetMap Wiki
Jump to navigation Jump to search

This is old stuff, kept for historic reasons. Since the switch2osm-packages it makes very little sense to attempt this installation.

This is a composite of several howtos I followed and things I had to do in order to setup a slippy map using OSM data served by mod_tile. I'm using specific revisions of everything for your convenience.

In my environment this is running on a VirtualBox with 2TB dynamically growing disk, 4096MB RAM and 12 CPUs.


You need a 64 bit distro for this to work!

Download ubuntu-11.10-desktop-amd64.iso and install from scratch. My first user is called osm, you call it whatever you want but if you do you can't copy and paste from the texts below without encountering problems.


sudo apt-get install git-core subversion


sudo apt-get install postgresql-9.1-postgis postgresql-contrib-9.1 postgresql-server-dev-9.1

sudo sysctl -w kernel.shmmax=268435456
cat > 30-postgresql-shm.conf.diff 
< #kernel.shmmax = 33554432
> kernel.shmmax = 268435456  # Max shared memory 256MB
sudo patch -p0 /etc/sysctl.d/30-postgresql-shm.conf < 30-postgresql-shm.conf.diff

cat > postgresql.conf.diff
< shared_buffers = 24MB			# min 128kB
> shared_buffers = 128MB			# min 128kB
< #maintenance_work_mem = 16MB		# min 1MB
> maintenance_work_mem = 256MB		# min 1MB
< #checkpoint_segments = 3		# in logfile segments, min 1, 16MB each
> checkpoint_segments = 20		# in logfile segments, min 1, 16MB each
< #autovacuum = on			# Enable autovacuum subprocess?  'on'
> autovacuum = off			# Enable autovacuum subprocess?  'on'
sudo patch -p0 /etc/postgresql/9.1/main/postgresql.conf < postgresql.conf.diff

/etc/init.d/postgresql restart
sudo su postgres
createuser -s osm
createdb -O osm osm

echo "CREATE EXTENSION hstore;"  | psql -d osm
psql -d osm -f /usr/share/postgresql/9.1/contrib/postgis-1.5/postgis.sql 
echo "ALTER TABLE geometry_columns OWNER TO osm; ALTER TABLE spatial_ref_sys OWNER TO osm;"  | psql -d osm

psql -f 900913.sql osm


This is Mapnik 2.n

sudo apt-get install -y g++ cpp \
  libicu-dev \
  libboost-filesystem-dev \
  libboost-program-options-dev \
  libboost-python-dev libboost-regex-dev \
  libboost-system-dev libboost-thread-dev \
  python-dev libxml2 libxml2-dev \
  libfreetype6 libfreetype6-dev \
  libjpeg-dev \
  libltdl7 libltdl-dev \
  libpng-dev \
  libgeotiff-dev libtiff-dev libtiffxx0c2 \
  libcairo2 libcairo2-dev python-cairo python-cairo-dev \
  libcairomm-1.0-1 libcairomm-1.0-dev \
  ttf-unifont ttf-dejavu ttf-dejavu-core ttf-dejavu-extra \
  git build-essential python-nose \
  libgdal1-dev python-gdal libsqlite3-dev

I used a revision from march 2012, a later one might work.

git clone
cd mapnik
git checkout -b kodapan 926404d9aead # checkout version from 2012-03-12 
make # -j 12
sudo make install

No errors should be reported when importing mapnik from python!

Install file: "fonts/unifont-5.1.20080907.ttf" as "/usr/local/lib/mapnik/fonts/unifont-5.1.20080907.ttf"
scons: done building targets.

osm@osm:~/mapnik$ python
Python 2.7.2+ (default, Oct  4 2011, 20:06:09) 
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mapnik;


sudo apt-get install build-essential libxml2-dev libgeos-dev libpq-dev libbz2-dev proj libtool automake libprotobuf-c0-dev

When documenting this I was at revision 28086.

svn co -r 28086
cd osm2pgsql

Optionally, you can configure the compiler to produce a faster binary that can only run on CPUs that have the same capabilities as yours.

sed -i 's/-g -O2/-O2 -march=native -fomit-frame-pointer/' Makefile

Finally make it.

make # -j 12 # CPUs

OSM data

svn co -r 28086 osm-mapnik
cd osm-mapnik

Download the osm.bz2 data that you want to support from cloudmade or what not.


Finally import data to database. (This will bunzip file using one thread and import data using another.)

cd ~/osm2pgsql
bunzip2 -c ../osm-mapnik/sweden.osm.bz2 | ./osm2pgsql -U osm -d osm /dev/stdin

Reading in file: /dev/stdin
Processing: Node(10744k 124.9k/s) Way(887k 10.08k/s) Relation(10112 306.42/s)  parse time: 207s
All indexes on  planet_osm_line created  in 57s
Completed planet_osm_line

Osm2pgsql took 328s overall
sudo apt-get install python-lxml
./ --dbname osm --user osm --accept-none osm.xml osm-mapnik2.xml

Apache and mod_tile

sudo apt-get install apache2 apache2-threaded-dev apache2-mpm-prefork apache2-utils

sudo echo ServerName tile.domain.tld > /etc/apache2/http.conf

sudo apt-get install libagg-dev

svn co -r 28086
cd mod_tile
cat > gen_tile.cpp.patch
> #include <mapnik/config_error.hpp>

patch -p0 gen_title.cpp < gen_title.cpp.patch
sudo make install

For me "nothing happends" as I executed make install, however it produced file .libs/ which I copied to /usr/lib/apache2/modules

sudo cp .libs/ /usr/lib/apache2/modules

After successful make install of mod_tile:

sudo bash -c "echo 'LoadModule tile_module /usr/lib/apache2/modules/' > /etc/apache2/mods-available/tile.load"
sudo a2enmod tile
sudo /etc/init.d/apache2 restart

cat > default.patch
> 	LoadTileConfigFile /etc/renderd.conf
> 	ModTileRenderdSocketName /tmp/osm-renderd
> 	# Timeout before giving up for a tile to be rendered
> 	ModTileRequestTimeout 3
> 	# Timeout before giving up for a tile to be rendered that is otherwise missing
> 	ModTileMissingRequestTimeout 30
sudo patch -p0 /etc/apache2/sites-available/default < default.patch

cat > /etc/renderd.conf 



sudo mkdir /var/run/renderd
sudo chown osm /var/run/renderd

sudo mkdir /var/lib/mod_tile
sudo chown osm /var/lib/mod_tile

./renderd -f
sudo cat > /var/www/map.html
    <title>OpenLayers Demo</title>
    <style type="text/css">
      html, body, #basicMap {
          width: 100%;
          height: 100%;
          margin: 0;
    <script src=""></script>
      function init() {
           var options = {
                projection: new OpenLayers.Projection("EPSG:900913"),
                displayProjection: new OpenLayers.Projection("EPSG:4326"),
                units: "m",
                maxResolution: 156543.0339,
                maxExtent: new OpenLayers.Bounds(-20037508.34, -20037508.34,
                                                 20037508.34, 20037508.34),
                numZoomLevels: 20,
                controls: [
                        new OpenLayers.Control.Navigation(),
                        new OpenLayers.Control.PanZoomBar(),
                        new OpenLayers.Control.Permalink(),
                        new OpenLayers.Control.ScaleLine(),
                        new OpenLayers.Control.MousePosition(),
                        new OpenLayers.Control.KeyboardDefaults()
        map = new OpenLayers.Map("basicMap",options);
        var newL = new OpenLayers.Layer.OSM("Default", "/osm_tiles2/${z}/${x}/${y}.png", {numZoomLevels: 19});
  <body onload="init();">
    <div id="basicMap"></div>

Open a webbrowser to http://localhost/map.html


And many more that I forgot... Will try to find them in my history.