OpenStreetMap Carto Tutorials - OpenStreetMap Carto documentation     Site map    

Installing an OpenStreetMap Tile Server on Ubuntu

Installing an OpenStreetMap Tile Server on Ubuntu

Introduction

Even if the general scope of this site is to provide tutorials to set-up a development environment of OpenStreetMap Carto and offer recommendations to edit the style, this page shows how OpenStreetMap Carto can be used to implement a tile server using the same software adopted by OpenStreetMap.

The OSM Tile Server is a web server specialized in delivering raster maps, serving them as static tiles and able to perform rendering in real time or providing cached images. The adopted web software by OpenStreetMap is the Apache HTTP Server, together with a specific plugin named mod_tile and a related backend stack able to generate tiles at run time; programs and libraries are chained together to create the tile server.

As so often with OpenStreetMap, there are many ways to achieve a goal and nearly all of the components have alternatives that have various specific advantages and disadvantages. This tutorial describes the standard installation process of the OSM Tile Server used on OpenStreetMap.org.

It consists of the following main components:

  • Mapnik
  • Apache
  • Mod_tile
  • renderd
  • osm2pgsql
  • PostgreSQL/PostGIS database, to be installed locally (suggested) or remotely (might be slow, depending on the network).
  • carto
  • openstreetmap-carto

All mentioned software is open-source.

For the tile server, a PostGIS database is required, storing geospatial features populated by osm2pgsql tool from OSM data. Also, a file system directory including the OSM.xml file, map symbols (check openstreetmap-carto/symbols subdirectory) and shapefiles (check openstreetmap-carto/data subdirectory) is needed. OSM.xml is preliminarily produced by a tool named carto from the openstreetmap-carto style (project.mml and all related CartoCSS files included in openstreetmap-carto).

When the Apache web server receives a request from the browser, it invokes the mod_tile plugin, which in turn checks if the tile has already been created (from a previous rendering) and cached, so that it is ready for use; in case, mod_tile immediately sends the tile back to the web server. Conversely, if the request needs to be rendered, then it is queued to the renderd backend, which is responsible to invoke Mapnik to perform the actual rendering; renderd is a daemon process included in the mod_tile sources and interconnected to mod_tile via UNIX queues. renderd is the standard backend currently used by www.openstreetmap.org, even if some OSM implementations use tirex; Mapnik extracts data from the PostGIS database according to the openstreetmap-carto style information and dynamically renders the tile. renderd passes back the produced tile to the web server and in turn to the browser.

The renderd daemon implements a queuing mechanism with multiple priority levels to provide an as up-to-date viewing experience given the available rendering resources. The highest priority is for on the fly rendering of tiles not yet in the tile cache, two priority levels for re-rendering out of date tiles on the fly and two background batch rendering queues. To avoid problems with directories becoming too large and to avoid too many tiny files, Mod_tile/renderd store the rendered tiles in “meta tiles”, in a special hashed directory structure.1

Even if the tileserver dynamically generates tiles at run time, they can also be pre-rendered for offline viewing with a specific tool named render_list, which is typically used to pre-render low zoom level tiles and takes significant time to accomplish the process (tens of hours in case the full planet is pre-rendered); this utility is included in mod_tile, as well as another tool named render_expired, which provides methods to allow expiring map tiles. More detailed description of render_list and render_expired can be found in their man pages.

A background on the tiles expiry method can be found at tiles expiry mechanism.

The overall process is here represented2.

    client browser web    
       
Disk Cache (tiles)png   Apache Web Server prg Web page html
     
renderd prg mod_tile prg tiles png
   
Mapnik XML xml Mapnik prg    
     
PostgreSQL PostGIS db   shapefiles data directory shape    


An additional description of the rendering process of OpenStreetMap can be found at OSM architecture.

The following step-by-step procedure can be used to install and configure all the necessary software to operate your own OpenStreetMap tile server on Ubuntu 16.4 or 14.4.3

The goal for this procedure is to use Ubuntu packages and official PPAs whenever possible.

We consider using Ubuntu 16.04.1 LTS Xenial Xerus, Ubuntu 15.4 Vivid Vervet or Ubuntu 14.04.3 LTS Trusty Tahr (other versions should work). All should be 64-bit computing architecture.

Other distributions like Debian might require changes and are not tested.

This procedure is updated to the version of OpenStreetMap Carto available at the time of writing. To get the correct installation procedure, the INSTALL history should be checked, considering that the OpenStreetMap Carto maintainers use to keep the INSTALL page updated. Check also the README changelog.

General setup for Ubuntu

Update Ubuntu

Make sure your Ubuntu system is fully up-to-date:

lsb_release -a

Previous command returns the Ubuntu version.

To update the system:

sudo apt-get update
sudo apt-get -y upgrade

If on a brand new system you also want to do sudo apt-get dist-upgrade && sudo shutdown -r now.

Install essential tools

sudo apt-get -y install curl unzip gdal-bin tar wget bzip2 build-essential clang

For the subsequent installation steps, we suppose that cd defaults to your home directory.

Configure a swap

Importing and managing map data takes a lot of RAM and a swap is generally needed.

To check whether a swap partition is already configured on your system, use one of the following two commands:

  • Reports the swap usage summary (no output means missing swap):

    swapon -s
    
  • Display amount of free and used memory in the system (check the line specifying Swap):

    free -h
    

If you do not have an active swap partition, especially if your physical memory is small, you should add a swap file. First we use fallocate command to create a file. For example, create a file named swapfile with 2G capacity in root file system:

sudo fallocate -l 2G /swapfile

Then make sure only root can read and write to it.

sudo chmod 600 /swapfile

Format it to swap:

sudo mkswap /swapfile

Enable the swap file

sudo swapon /swapfile

The Operating System tuning adopted by the OpenStreetMap tile servers can be found in the related Chef configuration.

Check usage of English locale

Run locale to list what locales are currently defined for the current user account:

locale

To set the en_US locale:

export LANGUAGE=en_US.UTF-8
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8

The exported variables can be put to the file /etc/environment.

New locales can also be generated by issuing:

sudo locale-gen en_US en_US.UTF-8
sudo dpkg-reconfigure locales

Creating a UNIX user

We suppose that you have already created a login user during the installation of Ubuntu, to be used to run the tile server. Let’s suppose that your selected user name is tileserver. Within this document, all times tileserver is mentioned, change it with your actual user name.

If you need to create a new user:

sudo useradd -m tileserver
sudo passwd tileserver

Set a password when prompted.

Install Git

Git should be already installed on Ubuntu 16.04.

git --version # to verify whether git is already installed
sudo apt-get install -y git

Install Mapnik library

We need to install the Mapnik library. Mapnik is used to render the OpenStreetMap data into the tiles managed by the Apache web server through renderd and mod_tile.

We report some alternative procedures to install Mapnik (in the consideration to run an updated version of Ubuntu).

Install Mapnik library from package

Tested with Ubuntu 16.04 and Ubuntu 14.04; suggested as the preferred option to install Mapnik.

sudo apt-get install -y git autoconf libtool libxml2-dev libbz2-dev \
  libgeos-dev libgeos++-dev libproj-dev gdal-bin libgdal-dev g++ \
  libmapnik-dev mapnik-utils python-mapnik

This will most probably install Mapnik 2.2 on Ubuntu 14.04.3 LTS and Mapnik 3.0.9 on Ubuntu 16.04.1 LTS.

Go to check Mapnik installation.

Alternatively, install Mapnik from sources

Refer to Mapnik Ubuntu Installation to for specific documentation.

Refer to Mapnik Releases for the latest version and changelog.

This procedure to install Mapnik from sources has been tested with Ubuntu 16.4 only. Refer to the above links for other O.S. versions.

Remove any other old Mapnik packages:

sudo apt-get purge -y libmapnik* mapnik-* python-mapnik
sudo add-apt-repository --remove -y ppa:mapnik/nightly-trunk

Install prerequisites; first create a directory to load the sources:

test -d ~/src || mkdir  ~/src ; cd ~/src

sudo apt-get install -y libxml2-dev libfreetype6-dev \
  libjpeg-dev libpng-dev libproj-dev libtiff-dev \
  libcairo2 libcairo2-dev python-cairo python-cairo-dev \
  libgdal-dev git

sudo apt-get install -y build-essential python-dev libbz2-dev libicu-dev

We need to install Boost either from package or from source.

Install Boost from package

sudo apt-get install -y libboost-all-dev

Alternatively, install the latest version of Boost from source

sudo apt-get purge -y libboost-all-dev # remove installation from package
cd ~/src
wget -O boost.tar.bz2 https://sourceforge.net/projects/boost/files/latest/download?source=files
tar xjf boost.tar.bz2
rm boost.tar.bz2
cd boost_*
./bootstrap.sh
./b2 stage toolset=gcc --with-thread --with-filesystem --with-python --with-regex -sHAVE_ICU=1 -sICU_PATH=/usr/ --with-program_options --with-system link=shared
sudo ./b2 install toolset=gcc --with-thread --with-filesystem --with-python --with-regex -sHAVE_ICU=1 -sICU_PATH=/usr/ --with-program_options --with-system link=shared -d0
sudo ldconfig && cd ~/

Install HarfBuzz from source

HarfBuzz is an OpenType text shaping engine.

Check the lastest version here

cd ~/src
wget https://www.freedesktop.org/software/harfbuzz/release/harfbuzz-1.3.2.tar.bz2
tar xf harfbuzz-1.3.2.tar.bz2
rm harfbuzz-1.3.2.tar.bz2
cd harfbuzz-1.3.2
./configure && make && sudo make install
sudo ldconfig
cd ~/

Build the Mapnik library from source

cd ~/src
git clone https://github.com/mapnik/mapnik.git --depth 10
cd mapnik
git submodule update --init
bash
source bootstrap.sh
./configure && make

Test Mapnik (without needing to install):

make test # some test might not pass

Install Mapnik:

sudo make install
cd ~/

Python bindings are not included by default. You’ll need to add those separately.

cd ~/src
git clone https://github.com/mapnik/python-mapnik.git
cd python-mapnik
sudo apt-get install -y python-setuptools python3-setuptools libboost-python-dev
sudo python setup.py develop
sudo python setup.py install

Verify that Mapnik has been correctly installed

Report Mapnik version number:

mapnik-config -v

Check then with Python:

python -c "import mapnik;print mapnik.__file__"

It should return the path to the python bindings (e.g., /usr/lib/python2.7/dist-packages/mapnik/__init__.pyc). If python replies without errors, then Mapnik library was found by Python.

Configure the firewall

If you are preparing a remote virtual machine, configure the firewall to allow remote access to the local port 80 and local port 443.

If you run a cloud based VM, also the VM itself shall be set to open this port.

Install Apache HTTP Server

The Apache free open source HTTP Server is among the most popular web servers in the world. It’s well-documented, and has been in wide use for much of the history of the web, which makes it a great default choice for hosting a website.

To install apache:

sudo apt-get install -y apache2 apache2-dev

To check if Apache is installed, direct your browser to the IP address of your server (eg. http://localhost). The page should display the default Apache home page. Also this command allows checking correct working:

curl localhost| grep 'It works!'

The Apache tuning adopted by the OpenStreetMap tile servers can be found in the related Chef configuration.

How to Find the IP address of your server

You can run the following command to reveal the public IP address of your server:

wget http://ipinfo.io/ip -qO -

You can test Apache by accessing it through a browser at http://your-server-ip.

Install Mod_tile

Mod_tile is an Apache module to efficiently render and serve map tiles for www.openstreetmap.org map using Mapnik. We can compile it from Github repository.

test -d ~/src || mkdir  ~/src ; cd ~/src
git clone https://github.com/openstreetmap/mod_tile.git
cd mod_tile
./autogen.sh && ./configure && make && sudo make install && sudo make install-mod_tile && sudo ldconfig
cd ~/

The rendering process implemented by mod_tile and renderd is well explained here.

Python installation

Check that Python is installed:

python -V
python3 -V

Otherwise Python needs to be installed.

Install Yaml and Package Manager for Python

This is necessary in order to run OpenStreetMap-Carto scripts/indexes.

sudo apt-get install -y python-yaml

pip -V # to verify whether pip is already installed
sudo apt-get install -y python-pip

Install Mapnik Utilities

The Mapnik Utilities package includes shapeindex.

sudo apt-get install -y mapnik-utils

Install openstreetmap-carto

cd ~/src
git clone https://github.com/gravitystorm/openstreetmap-carto.git
cd openstreetmap-carto

Read installation notes for further information.

Install the fonts needed by openstreetmap-carto

Currently Noto fonts are used.

To install them (except Noto Emoji Regular and Noto Sans Arabic UI Regular/Bold):

sudo apt-get install -y fonts-noto-cjk fonts-noto-hinted fonts-noto-unhinted fonts-hanazono ttf-unifont

Installation of Noto Emoji Regular and Noto Sans Arabic UI Regular/Bold:

cd ~/src
git clone https://github.com/googlei18n/noto-emoji.git
git clone https://github.com/googlei18n/noto-fonts.git
sudo cp noto-emoji/fonts/NotoColorEmoji.ttf noto-emoji/fonts/NotoEmoji-Regular.ttf /usr/share/fonts/truetype/noto
sudo cp noto-fonts/hinted/NotoSansArabicUI-Regular.ttf noto-fonts/hinted/NotoNaskhArabicUI-Regular.ttf noto-fonts/hinted/NotoSansArabicUI-Bold.ttf noto-fonts/hinted/NotoNaskhArabicUI-Bold.ttf /usr/share/fonts/truetype/noto
sudo fc-cache -fv
sudo apt install fontconfig
fc-list
fc-list | grep Emoji
cd openstreetmap-carto

DejaVu Sans is used as an optional fallback font for systems without Noto Sans. If all the Noto fonts are installed, it should never be used.

sudo apt-get install -y fonts-dejavu-core

Read font notes for further information.

Create the data folder

cd ~/src
cd openstreetmap-carto
scripts/get-shapefiles.py

The actual shapefiles loaded by the OpenStreetMap tile servers are reported in the related Chef configuration.

Read scripted download for further information.

Install carto and build the Mapnik xml stylesheet

Install nodejs-legacy:

sudo apt install -y nodejs-legacy

Install npm:

sudo apt install -y npm

Install the latest version of carto:

sudo npm install -g carto@0.18.0

Ensure that the latest carto version is installed (at least version >= 0.18.0, using YAML):

carto -v

The output should be carto 0.18.0 (Carto map stylesheet compiler).

Test carto and produce style.xml from the openstreetmap-carto style:

cd ~/src
cd openstreetmap-carto
carto -a "3.0.0" project.mml > style.xml
ls -l style.xml

Warning messages for deprecated name attributes might happen and are normal (like Warning: using the name attribute for layers (like water-lines-text here) is deprecated and will be removed in 1.0.0. Use id instead..

The option -a "3.0.0" is needed when using Mapnik 3 functions 4.

Notice that the carto feature able to natively process project.mml in YAML format (currently adopted for openstreetmap-carto) is recent. The command sudo apt-get install -y node-carto will install an old carto version, not compatible with Openstreetmap Carto, and should be avoided.

Set the environment variables

export PGHOST=localhost
export PGPORT=5432
export PGUSER=postgres
export PGPASSWORD=postgres_007%

Install PostgreSQL and PostGIS

PostgreSQL is a relational database, and PostGIS is its spatial extender, which allows you to store geographic objects like map data in it; it serves a similar function to ESRI’s SDE or Oracle’s Spatial extension. PostgreSQL + PostGIS are used for a wide variety of features such as rendering maps, geocoding, and analysis.

Currently the tested versions for OpenstreetMap Carto are PostgreSQL 9.5 and PostGIS 2.2:

Also older PostgreSQL version should be suitable.

On Ubuntu there are pre-packaged versions of both postgis and postgresql, so these can simply be installed via the Ubuntu package manager.

sudo apt-get update
sudo apt-get install -y postgresql postgis pgadmin3 postgresql-contrib

Note: used PostgeSQL port is 5432 (default).

A user named postgres will be created during the installation process.

Set the password for the postgres user

sudo -u postgres psql postgres
\password postgres

Alternative procedure (useful if you get authentication issues with the previous one):

sudo su -
sudo -i -u postgres
psql postgres
\password postgres

Enter the following password twice: postgres_007%

This is just an example of password, you can use the one you prefer.

After entering the password, exit from psql with:

\q

With the second procedure, also isssue:

exit # from 'sudo -i -u postgres'
exit # from 'sudo su -'

Create the PostGIS instance

Now you need to create a postgis database. The defaults of various programs including openstreetmap-carto (ref. project.mml) assume the database is called gis. You need to set up PostGIS on the PostgreSQL database.

export PGPASSWORD=postgres_007%
HOSTNAME=localhost # set it to the actual ip address or host name
psql -U postgres -h $HOSTNAME -c "create database gis" # alternative command: createdb -E UTF8 -O postgres gis
psql -U postgres -h $HOSTNAME -c "\connect gis"
psql -U postgres -h $HOSTNAME -d gis -c "CREATE EXTENSION postgis"
psql -U postgres -h $HOSTNAME -d gis -c "CREATE EXTENSION hstore"

The character encoding scheme to be used in the database is UTF8.

If you get the following error

ERROR: could not open extension control file "/usr/share/postgresql/9.3/extension/postgis.control": No such file or directory

then you might be installing PostgreSQL 9.3, for which you should also need:

sudo apt-get install postgis postgresql-9.3-postgis-scripts

Install it and repeat the create extension commands.

If you need to use a different tablespace than the default one, execute the following commands instead of the previous ones (example: the tablespace has location /tmp/db):

export PGPASSWORD=postgres_007%
HOSTNAME=localhost # set it to the actual ip address or host name
sudo mkdir /mnt/db # Suppose this is the tablespace location
sudo chown postgres:postgres /mnt/db
psql -U postgres -h $HOSTNAME -c "CREATE TABLESPACE gists LOCATION '/mnt/db'"
psql -U postgres -h $HOSTNAME -c "CREATE DATABASE gis TABLESPACE gists"
psql -U postgres -h $HOSTNAME -c "\connect gis"
psql -U postgres -h $HOSTNAME -d gis -c "CREATE EXTENSION postgis"
psql -U postgres -h $HOSTNAME -d gis -c "CREATE EXTENSION hstore"

Add a user and grant access to gis DB

In order for the application to access the gis database, a DB user with the same name of your UNIX user is needed. Let’s suppose your UNIX ue is tileserver.

sudo su -
sudo -i -u postgres
createuser tileserver
psql
grant all privileges on database gis to tileserver;
\q
exit
exit

Enabling remote access to PostgreSQL

To remotely access PostgreSQL, you need to edit pg_hba.conf:

sudo vi /etc/postgresql/9.5/main/pg_hba.conf

and add the following line:

host    all             all             <your IP set>/<your netmask>             md5

host all all 0.0.0.0/0 md5 is an access control rule that let anybody login in from any address if providing a valid password (md5 keyword).

Then edit postgresql.conf:

sudo vi /etc/postgresql/9.5/main/postgresql.conf

and set listen_addresses = '*'

Finally, the DB shall be restarted:

sudo /etc/init.d/postgresql restart

Tuning the database

The default PostgreSQL settings aren’t great for very large databases like OSM databases. Proper tuning can just about double the performance.

The PostgreSQL wiki has a page on database tuning.

Paul Norman’s Blog has an interesting note on optimizing the database, which is used here below.

Adjusting maintenance_work_mem and work_mem are probably enough on a development or testing machine.5: both parameters should be increased for faster data loading and faster queries while rendering respectively.

Conservative settings for a 2GB VM are work_mem=16MB and maintenance_work_mem=128MB. On a machine with enough memory you could set them as high as work_mem=128MB and maintenance_work_mem=1GB.

Besides, important settings are shared_buffers and the write-ahead-log (wal). There are also some other settings you might want to change specifically for the import.

To edit the PostgreSQL configuration file with vi editor:

sudo vi /etc/postgresql/9.5/main/postgresql.conf

and if you are running PostgreSQL 9.3:

sudo vi /etc/postgresql/9.3/main/postgresql.conf

Suggested settings:

shared_buffers = 128MB
min_wal_size = 1GB
max_wal_size = 2GB
maintenance_work_mem = 256MB
autovacuum = off
fsync = off

The latter two ones allow a faster import: the first turns off auto-vacuum during the import and allows you to run a vacuum at the end; the second introduces data corruption in case of a power outage and is dangerous. If you have a power outage while importing the data, you will have to drop the data from the database and re-import, but it’s faster. Just remember to change these settings back after importing. fsync has no effect on query times once the data is loaded.

The PostgreSQL tuning adopted by OpenStreetMap can be found in the PostgreSQL Chef Cookbook: the specific PostgreSQL tuning for the OpenStreetMap tile servers is reported in the related Tileserver Chef configuration.

For a dev&test installation on a system with 16GB of RAM, the suggested settings are the following:

shared_buffers = 2GB
work_mem = 128MB
maintenance_work_mem = 1GB
wal_level = minimal
synchronous_commit = off
min_wal_size = 1GB
max_wal_size = 2GB
checkpoint_timeout = 15min
checkpoint_completion_target = 0.9
default_statistics_target = 1000
autovacuum = off
fsync = off

To stop and start the database:

sudo /etc/init.d/postgresql stop

sudo /etc/init.d/postgresql start

You may get an error and need to increase the shared memory size. Edit /etc/sysctl.d/30-postgresql-shm.conf and run sudo sysctl -p /etc/sysctl.d/30-postgresql-shm.conf. A parameter like kernel.shmmax=17179869184 and kernel.shmall=4194304 could be appropriate for a 16GB segment size.6

To manage and maintain the configuration of the servers run by OpenStreetMap, the Chef configuration management tool is used.

The configuration adopted for PostgreSQL is postgresql/attributes/default.rb.

Install Osm2pgsql

Osm2pgsql is an OpenStreetMap specific software used to load the OSM data into the PostGIS database.

To install osm2pgsql:

sudo apt-get install -y osm2pgsql

Go to Get an OpenStreetMap data extract.

Alternative installation procedure

This alternative installation procedure generates the most updated executable by compiling the sources.

# Needed dependencies
sudo apt-get install -y make cmake g++ libboost-dev libboost-system-dev \
  libboost-filesystem-dev libexpat1-dev zlib1g-dev \
  libbz2-dev libpq-dev libgeos-dev libgeos++-dev libproj-dev lua5.2 \
  liblua5.2-dev

# Download osm2pgsql
cd /tmp
git clone git://github.com/openstreetmap/osm2pgsql.git 

# Prepare for compiling
cd osm2pgsql
mkdir build && cd build
cmake ..

# Compile
make

# Install
sudo make install

# Clean-out temporary files
cd ../..
rm -rf osm2pgsql

Get an OpenStreetMap data extract

You need to download an appropriate .osm or .pbf file to be subsequently loaded into the previously created PostGIS instance via osm2pgsql.

There are many ways to download the OSM data.

The reference is Planet OSM.

It’s probably easiest to grab an PBF of OSM data from Mapzen or geofabrik.

Also, BBBike.org provides extracts of more than 200 cities and regions world-wide in different formats.

Examples:

  • Map data of the whole planet (32G):

    wget -c http://planet.openstreetmap.org/pbf/planet-latest.osm.pbf
    
  • Map data of Great Britain (847M):

    wget -c http://download.geofabrik.de/europe/great-britain-latest.osm.pbf
    
  • Map data of Lombardy (279M):

    wget -c http://osm-estratti.wmflabs.org/estratti/regioni/pbf/03---Lombardia.pbf
    
  • For just Liechtenstein:

    wget http://download.geofabrik.de/europe/liechtenstein-latest.osm.pbf.md5
    wget http://download.geofabrik.de/europe/liechtenstein-latest.osm.pbf
    md5sum -c liechtenstein-latest.osm.pbf.md5 # Check that the download wasn't corrupted
    

Another method to download data is directly with your browser. Check this page.

Alternatively, JOSM can be used (Select the area to download the OSM data: JOSM menu, File, Download From OSM; tab Slippy map; drag the map with the right mouse button, zoom with the mouse wheel or Ctrl + arrow keys; drag a box with the left mouse button to select an area to download. The Continuous Download plugin is also suggested. When the desired region is locally available, select File, Save As, <filename>.osm. Give it a valid file name and check also the appropriate directory where this file is saved.

In all cases, avoid using too small areas.

OpenStreetMap is open data. OSM’s license is Open Database License.

Load data to PostGIS

The osm2pgsql documentation reports all needed information to use this ETL tool, including related command line options.

osm2pgsql uses overcommit like many scientific and large data applications, which requires adjusting a kernel setting:

sudo sysctl -w vm.overcommit_memory=1

To load data from an .osm or .pbf file to PostGIS, issue the following:

cd ~/src
cd openstreetmap-carto
HOSTNAME=localhost # set it to the actual ip address or host name
osm2pgsql -s -C 300 -c -G --hstore --style openstreetmap-carto.style --tag-transform-script openstreetmap-carto.lua -d gis -H $HOSTNAME -U postgres [.osm or .pbf file]

If everything is ok, you can go to Create indexes.

Notice that the following elements are used:

  • hstore
  • the openstreetmap-carto.style
  • the openstreetmap-carto.lua LUA script
  • gis DB name

Depending on the input file size, the osm2pgsql command might take very long.

Note: if you get the following error:

` node_changed_mark failed: ERROR: prepared statement “node_changed_mark” does not exist

do the following command on your original.osm:

sed "s/action='modify' //" < original.osm | > fixedfile.osm

Then process fixedfile.osm.

If you get errors like this one:

Error reading style file line 79 (fields=4)
flag 'phstore' is invalid in non-hstore mode
Error occurred, cleaning up

or this one:

Postgis Plugin: ERROR:  column "tags" does not exist
LINE 8: ...ASE WHEN "natural" IN ('mud') THEN "natural" ELSE tags->'wet...

then you need to enable hstore extension to the db with CREATE EXTENSION hstore; and also add the –hstore flag to osm2pgsql. Enabling hstore extension and using it with osm2pgsql will fix those errors.

Create indexes

Add the indexes indicated by openstreetmap-carto:

HOSTNAME=localhost # set it to the actual ip address or host name
cd ~/src
cd openstreetmap-carto
scripts/indexes.py | psql -U postgres -h $HOSTNAME -d gis

Read custom indexes for further information.

Configure renderd

Next we need to plug renderd and mod_tile into the Apache webserver, ready to receive tile requests.

Edit renderd configuration file with your preferite editor:

sudo vi /usr/local/etc/renderd.conf

In the [default] section, change the value of XML and HOST to the following.

XML=/home/tileserver/src/openstreetmap-carto/style.xml
HOST=localhost

Also, substitute all ;** with ;xxx=** (e.g., with vi :1,$s/^;\*\* /;xxx=** /g).

We suppose in the above example that your home directory is /home/tileserver. Change it to your actual home directory.

In [mapnik] section, change the value of plugins_dir.

plugins_dir=/usr/lib/mapnik/3.0/input/

Pay attention to the mapnik version: if it is at version 2.2 (Ubuntu 14.4):

plugins_dir=/usr/lib/mapnik/2.2/input/

Save the file.

Check this to be sure:

ls -l /home/tileserver/src/openstreetmap-carto/style.xml
grep '^;xxx=\*\*' /usr/local/etc/renderd.conf

In case of error, verify user name and check again /usr/local/etc/renderd.conf.

Install renderd init script by copying the sample init script included in its package.

sudo cp ~/src/mod_tile/debian/renderd.init /etc/init.d/renderd

Grant execute permission.

sudo chmod a+x /etc/init.d/renderd

Edit the init script file

sudo vi /etc/init.d/renderd

Change the following variables:

DAEMON=/usr/local/bin/$NAME
DAEMON_ARGS="-c /usr/local/etc/renderd.conf"
RUNASUSER=tileserver

In RUNASUSER=tileserver we suppose that your user is tileserver. Change it to your actual user name.

Save the file.

Create the following file and set tileserver (your actual user) the owner.

sudo mkdir -p /var/lib/mod_tile

sudo chown tileserver:tileserver /var/lib/mod_tile

Again change it to your actual user name.

Then start renderd service

sudo systemctl daemon-reload

sudo systemctl start renderd

sudo systemctl enable renderd

The following output is regular:

renderd.service is not a native service, redirecting to systemd-sysv-install
Executing /lib/systemd/systemd-sysv-install enable renderd

If systemctl is not installed (e.g., Ubuntu 14.4) use these commands respectively:

sudo update-rc.d renderd defaults

sudo service renderd start

Configure Apache

Create a module load file.

sudo vi /etc/apache2/mods-available/mod_tile.load

Paste the following line into the file.

LoadModule tile_module /usr/lib/apache2/modules/mod_tile.so

Create a symlink.

sudo ln -s /etc/apache2/mods-available/mod_tile.load /etc/apache2/mods-enabled/

Then edit the default virtual host file.

sudo vi /etc/apache2/sites-enabled/000-default.conf

Past the following lines after the line <VirtualHost *:80>

# Load all the tilesets defined in the configuration file into this virtual host
LoadTileConfigFile /usr/local/etc/renderd.conf
# Socket where we connect to the rendering daemon
ModTileRenderdSocketName /var/run/renderd/renderd.sock
# 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 60

Save and close the file. Restart Apache.

sudo systemctl restart apache2

If systemctl is not installed (e.g., Ubuntu 14.4):

sudo service apache2 restart

Test access to tiles locally:

wget --spider http://localhost/osm_tiles/0/0/0.png

You should get Remote file exists. if everything is correctly configured.

Then in your web browser address bar, type

your-server-ip/osm_tiles/0/0/0.png

where you need to change your-server-ip with the actual IP address of the installed map server.

To expand it with the public IP address of your server, check this command for instance (paste its output to the browser):

echo "http://`wget http://ipinfo.io/ip -qO -`/osm_tiles/0/0/0.png"

You should see the tile of world map.

Congratulations! You just successfully built your own OSM tile server.

You can go to OpenLayers to display the slippy map.

Pre-rendering tiles

Pre-rendering tiles is generally not needed (or not wanted); its main usage is to allow offline viewing instead of rendering tiles on the fly. Depending on the DB size, the procedure can take very long time and relevant disk data.

To pre-render tiles, use render_list command. Pre-rendered tiles will be cached in /var/lib/mod_tile directory.

To show all command line option of render_list:

render_list --help

Example usage:

render_list -a

Depending on the DB size, this command might take very long.

The following command pre-renders all tiles from zoom level 0 to zoom level 10 using 1 thread:

render_list -n 1 -z 0 -Z 10 -a

A command line Perl script named render_list_geo.pl and developed by alx77 allows automatic pre-rendering of tiles in a particular area using geographic coordinates. The related Github README describes usage and samples.

To install it:

cd ~/src
git clone https://github.com/alx77/render_list_geo.pl
cd render_list_geo.pl

Example of command to generate the z11 tiles for the UK:

./render_list_geo.pl -n 1 -z 11 -Z 11 -x -9.5 -X 2.72 -y 49.39 -Y 61.26

For both render_list and render_list_geo.pl, option -m allows selecting specific profiles relates to named sections in renderd.conf. Not using this option, the [default] section of renderd.conf is selected.

Troubleshooting Apache, mod_tile and renderd

To clear all osm tiles cache, remove /var/lib/mod_tile/default (using rm -rf if you dare) and restart renderd daemon:

sudo rm -rf /var/lib/mod_tile/default
sudo systemctl restart renderd

If systemctl is not installed (e.g., Ubuntu 14.4):

sudo service renderd restart

Show Apache loaded modules:

apache2ctl -M

You should find tile_module (shared)

Show Apache configuration:

apache2ctl -S

You should get the following messages within the log:

Loading tile config default at /osm_tiles/ for zooms 0 - 20 from tile directory /var/lib/mod_tile with extension .png and mime type image/png

Tail log:

tail -f /var/log/apache2/error.log

Most of the configuration issues can discovered by analyzing the debug log of renderd; we need to stop the daemon and start renderd in foreground:

sudo systemctl stop renderd

If systemctl is not installed (e.g., Ubuntu 14.4):

sudo service renderd stop

Then:

sudo -u tileserver /usr/local/bin/renderd -fc /usr/local/etc/renderd.conf

Ignore the five errors related to iniparser: syntax error in /usr/local/etc/renderd.conf when referring to commented out variables (e.g., beginning with ;).

Press Control-C to kill the program. After fixing the error, the daemon can be restarted with:

sudo systemctl start renderd

If systemctl is not installed (e.g., Ubuntu 14.4):

sudo service renderd start

Check existence of /var/run/renderd:

ls -ld /var/run/renderd

Verify that the access permission are -rw-r--r-- 1 tileserver tileserver.

Check existence of the style.xml file:

 ls -l /home/tileserver/src/openstreetmap-carto/style.xml

If missing, see above to create it.

Check existence of /var/run/renderd/renderd.sock:

ls -ld /var/run/renderd/renderd.sock

Verify that the access permission are srwxrwxrwx 1 tileserver tileserver.

In case of wrong owner:

sudo chown 'tileserver' /var/run/renderd
sudo chown 'tileserver' /var/run/renderd/renderd.sock
sudo service renderd restart

If the directory is missing:

sudo mkdir /var/run/renderd
sudo chown 'tileserver' /var/run/renderd
sudo service renderd restart

An error related to missing tags column in the renderd logs means that osm2pgsql was not run with the --hstore option.

If everything in the configuration looks fine, but the map is still not rendered without any particular message produced by renderd, try performing a system restart:

sudo shutdown -r now

If the problem persists, you might have a problem with your UNIX user. Try debugging again, after setting these variables:

export PGHOST=localhost
export PGPORT=5432
export PGUSER=postgres
export PGPASSWORD=postgres_007%

As exceptional case, the following commands allow to fully remove Apache, mod_tile and renderd and reinstall the service:

sudo rm -r ~/src/mod_tile/
sudo apt-get purge apache2 apache2-dev
sudo rm -r /etc/apache2/mods-available
sudo rm /usr/local/etc/renderd.conf
sudo rm  /etc/init.d/renderd
sudo rm -rf /var/lib/mod_tile
sudo rm -rf /usr/lib/apache2
sudo rm -rf /etc/apache2/
sudo rm -rf /var/run/renderd
sudo apt-get --reinstall install apache2-bin
sudo apt-get install apache2 apache2-dev

Hardware considerations

This paragraph is currently in early stage, not yet revised. Information at the moment is taken from the Hardware paragraph of Building a tile server from packages page within switch2osm.org.

Hardware requirements can be quite demanding if you want to render larger areas, but aren’t too bad if you are only interested in smaller regions. For a standard desktop (approximately 4 GB of RAM, standard hard disk, dual – quad core CPU) probably an extract size of about 100 – 300 Mb is reasonable (import time of the order of an hour).

If you want to import and render the whole world, you will need a considerably beefier server than a typical desktop. E.g. starting from about 24GB of RAM upwards. It is also strongly recommended to use an SSD for the database or at least a fast RAID array. The OSM map database is called planet.osm. The full database and regular update files are both available at Planet OSM. The full planet import is currently around about 256GB (58 GB zipped), so to store all of the DB on an SSD, you will likely need a 512GB SSD. An import that is not updated and uses the –drop option on the otherhand likely still fits on a 256GB SSD. You can also selectively put the most important parts of the database on an SSD and the rest on slower disks. Osm2pgsql supports using separate tablespaces for different parts of the database for this purpose.

Tile names format of OpenStreetMap tile server

The file naming and image format used by mod_tile is described at Slippy map tilenames. Similar format is also used by Google Maps and many other map providers.

TMS and WMS are other protocols for serving maps as tiles, managed by different rendering backends.

Deploying your own Slippy Map

Tiled web map is also known as slippy map in OpenStreetMap terminology.

OpenStreetMap does not provide an “official” JavaScript library which you are required to use. Rather, you can use any library that meets your needs. The two most popular are OpenLayers and Leaflet. Both are open source.

Page Deploying your own Slippy Map illustrates how to embed the previously installed map server into a website. A number of possible map libraries are mentioned, including some relevant ones (Leaflet, OpenLayers, Google Maps API) as well as many alternatives.

OpenLayers

To display your slippy map with OpenLayers, create a file named ol.html under /var/www/html.

sudo vi /var/www/html/ol.html

Paste the following HTML code into the file.

You might wish to adjust the longitude, latitude and zoom level according to your needs. Check var zoom = 2, center = [0, 0];.

<!DOCTYPE html>
<html>
<head>
<title>OpenStreetMap with OpenLayers</title>
<link rel="stylesheet" href="https://openlayers.org/en/v3.19.0/css/ol.css" type="text/css">
<script src="https://openlayers.org/en/v3.19.0/build/ol.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
  <style>
  html,
  body,
  #map {
    height: 100%;
    margin: 0;
    padding: 0;
  }
  .ol-custom-overviewmap,
  .ol-custom-overviewmap.ol-uncollapsible {
    bottom: auto;
    left: auto;
    right: 0;
    top: 85px;
  }  
  .ol-zoom {
    top: 50px;
  }
  .ol-zoom-extent {
      top: 110px;
  }
  .ol-zoomslider {
      top: 140px;
  }
  .ol-custom-fullscreen {
    bottom: auto;
    left: auto;
    right: 0;
    top: 50px;
  }
  .ol-custom-mouse-positionXY {
    top: auto;
    bottom: 3em;
    font-family: "Arial";
    font-size: 12px;
    text-shadow: 0 0 0.5em #FFE, 0 0 0.5em #FFE, 0 0 0.5em #FFE;
  }
  .ol-custom-mouse-positionHDMS {
    top: auto;
    bottom: 4em;
    font-family: "Arial";
    font-size: 12px;
    text-shadow: 0 0 0.5em #FFE, 0 0 0.5em #FFE, 0 0 0.5em #FFE;
  }
  .ol-custom-mouse-position3857 {
    top: auto;
    bottom: 5em;
    font-family: "Arial";
    font-size: 12px;
    text-shadow: 0 0 0.5em #FFE, 0 0 0.5em #FFE, 0 0 0.5em #FFE;
  }
  #ZoomElement {
    position: absolute;
    top: auto;
    left: 10px;
    bottom: 2.5em;
    text-decoration: none;
    font-family: "Arial";
    font-size: 10pt;
    text-shadow: 0 0 0.5em #FFE, 0 0 0.5em #FFE, 0 0 0.5em #FFE;
    z-index: 30;
  }
  #TSLabel {
    position: absolute;
    top: 21px;
    right: 0;
    font-family: "Arial";
    font-size: 12px;
    z-index: 30;
  }
  #osmLabel {
    position: absolute;
    top: 21px;
    left: 0;
    font-family: "Arial";
    font-size: 12px;
    z-index: 30;
  }
  #swipe {
    position: absolute;
    top: 0;
    left: -4px;
    z-index: 20;
  }
</style>
</head>
<body>
  <div class="ol-viewport">
  <input class="ol-unselectable ol-control" id="swipe" type="range" style="width: 100%">
  <div class="ol-unselectable ol-control" id="TSLabel"> Tile Server &#9658;</div>
  <div class="ol-unselectable ol-control" id="osmLabel">&#9668; OpenStreetMap </div>
  <a class="ol-unselectable ol-control" id="ZoomElement"></a>
  </div>
  <div tabindex="0" id="map" class="map"></div>
  <script>
    var zoom = 2, center = [0, 0];

    // Set up the Tile Server layer
    var myTileServer = new ol.layer.Tile({
      preload: Infinity,
      source: new ol.source.OSM({
        crossOrigin: null,
        url: 'osm_tiles/{z}/{x}/{y}.png'
      })
    });
    
    // Set up the OSM layer
    var openStreetMap = new ol.layer.Tile({
      preload: Infinity,
      source: new ol.source.OSM({
        crossOrigin: null,
        url: 'http://{a-c}.tile.openstreetmap.org/{z}/{x}/{y}.png'
      })
    });

    if (window.location.hash !== '') {
      var hash = window.location.hash.replace('#', '');
      var parts = hash.split(';');
      if (parts.length === 3) {
        zoom = parseInt(parts[0], 10);
        center = [
          parseFloat(parts[2]),
          parseFloat(parts[1])
          ];
      }
    }

    // Set up the default view
    var myTileView = new ol.View({
      center: ol.proj.transform(center, 'EPSG:4326', 'EPSG:3857'),
      zoom: zoom
    });
    
    // Create the map
    var map = new ol.Map({
      layers: [myTileServer, openStreetMap],
      loadTilesWhileInteracting: true,
      target: 'map',
      controls: ol.control.defaults().extend([
        new ol.control.ScaleLine(),
        new ol.control.Zoom(),
        new ol.control.ZoomSlider(),
        new ol.control.ZoomToExtent(),
        new ol.control.FullScreen({
          className: 'ol-fullscreen ol-custom-fullscreen'
        }),
        new ol.control.OverviewMap({
          className: 'ol-overviewmap ol-custom-overviewmap'
        }),
        new ol.control.MousePosition({
          className: 'ol-mouse-position ol-custom-mouse-position3857',
          coordinateFormat: ol.coordinate.createStringXY(4),
          projection: 'EPSG:3857',
          undefinedHTML: '&nbsp;'
        }),
        new ol.control.MousePosition({
          coordinateFormat: function(coord) {
            return ol.coordinate.toStringHDMS(coord);
          },
          projection: 'EPSG:4326',
          className: 'ol-mouse-position ol-custom-mouse-positionHDMS',
          target: document.getElementById('mouse-position'),
          undefinedHTML: '&nbsp;'
        }),
        new ol.control.MousePosition({
          className: 'ol-mouse-position ol-custom-mouse-positionXY',
          coordinateFormat: ol.coordinate.createStringXY(4),
          projection: 'EPSG:4326',
          undefinedHTML: '&nbsp;'
        }),
      ]),
      view: myTileView
    });
    map.on("moveend", function() {
      var view = map.getView();
      var center = ol.proj.transform(view.getCenter(), 'EPSG:3857', 'EPSG:4326');
      var zoom = view.getZoom();
      var zoomInfo = 'Zoom level = ' + zoom;
      document.getElementById('ZoomElement').innerHTML = zoomInfo;
      window.location.hash =
        view.getZoom() + ';' +
          Math.round(center[1]*1000000)/1000000 + ';' +
          Math.round(center[0]*1000000)/1000000;
    });

    var swipe = document.getElementById('swipe');
    
    openStreetMap.on('precompose', function(event) {
        var ctx = event.context;
        var width = ctx.canvas.width * (swipe.value / 100);

        ctx.save();
        ctx.beginPath();
        ctx.rect(width, 0, ctx.canvas.width - width, ctx.canvas.height);
        ctx.clip();
      });

    openStreetMap.on('postcompose', function(event) {
        var ctx = event.context;
        ctx.restore();
      });
    
    swipe.addEventListener('input', function() {
        map.render();
    }, false);
  </script>
</body>
</html>

Save and close the file. Now you can view your slippy map by typing the following URL in browser.

http://your-server-ip/ol.html

To expand it with the public IP address of your server, check this command for instance (paste its output to the browser):

echo "http://`wget http://ipinfo.io/ip -qO -`/ol.html"

Leaflet

Leaflet is a JavaScript library for embedding maps. It is simpler and smaller than OpenLayers.

The easiest example to display your slippy map with Leaflet consists in creating a file named lf.html under /var/www/html.

sudo vi /var/www/html/lf.html

Paste the following HTML code in the file. Replace your-server-ip with your IP Address and adjust the longitude, latitude and zoom level according to your needs.

<!DOCTYPE html>
<html>
<head>
<title>OpenStreetMap with Leaflet</title>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.6.4/leaflet.css" type="text/css">
<script src="http://cdn.leafletjs.com/leaflet-0.6.4/leaflet.js"></script>
<style>
  html,
  body,
  #map {
    height: 100%;
    margin: 0;
    padding: 0;
  }
</style>
</head>
<body>
  <div id="map" class="map"></div>
  <script>
    // Create the map
    var map = L.map('map').setView([45, 10], 3);
    
    // Set up the OSM layer
    L.tileLayer(
    'http://your-server-ip/osm_tiles/{z}/{x}/{y}.png'
    ).addTo(map);
  </script>
</body>
</html>

Save and close the file. Now you can view your slippy map by typing the following URL in the browser.

http://your-server-ip/lf.html

A rapid way to test the slippy map is through an online source code playground like this JSFiddle template.

The following example exploits Leaflet to show OpenStreetMap data.

Default tiles can be replaced with your tile server ones by changing

http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png

to http://your-server-ip/osm_tiles/{z}/{x}/{y}.png.

To edit the sample, click on Edit in JSFiddle. Then in the Javascript panel modify the string inside quotes as descripted above. Press Run.


Footnotes

  1. Check process description at mod_tile for further details. 

  2. Information on previous periods partially taken from switch2osm.org - Serving Tiles, ref “The toolchain” chapter. 

  3. sources used for this document are the following:

  4. math1985’s note 

  5. Information taken from switch2osm

  6. Information from Paul Norman’s Blog


Please, avoid using Disqus below to notify issues on this page, just use it for general comments. Create a GitHub issue instead. This is the most effective way to track problems.
comments powered by Disqus