OSM tileserver on a vanilla Ubuntu 16.04 droplet.

I want to write down a few simple steps which helped me to setup an OpenStreetMap tileserver in under 2 hours. There are quite a few good tutorials out there which helped me to get things rollin. Without this great tutorial by Xiao Guoan I wouldn’t have been able to have this running yet. I tried to get the switch2osm tutorial for Ubuntu 14.04 working on Ubuntu 15.04 a few months back and failed terribly. The experience of compiling Mapnik from source is harrowing. Don’t waste your time and use the 16.04 packages as described in this article.

The tutorial was completed on a vanilla Ubuntu 16.04 droplet from DigitalOcean. The spec for the server is 2GB RAM with 40GB SSD. At $20 per month this is very good value from DigitalOcean.

We love OpenStreetMap data at GEOLYTIX and hope to roll out our own styles in the future.

Let’s get started…

Open a shell as root user and create a new user first. I call this user ‘osm’ and add the user to the sudo group. I add my public key to the authorized_keys file in order to open the shell for this user in the future.

My preferred shell editor is nano but you can use vi, vim or whichever editor you are most comfortable with.

adduser osm
usermod -aG sudo osm
su osm
sudo mkdir ~/.ssh
sudo nano ~/.ssh/authorized_keys

Paste your key into the authorized_keys file. This will allow you to open a shell as the OSM user in the future.

My first sudos are an apt update followed by an upgrade and the installation of a few dependencies which we will need later on.

sudo apt update sudo apt upgrade
sudo apt install curl unzip autoconf gdal-bin libtool

I also install fail2ban and setup a UFW firewall blocking all but http, https and ssh.

Installing Postgis

Next install PostgreSQL, PostGIS and a couple of tools which we need to load OSM data to our server.

sudo apt install postgresql postgresql-contrib postgis postgresql-9.5-postgis-2.2

Let’s switch to the user ‘postgres’; Open psql and change the password for this user. \q quits psql.

sudo -u postgres -i
postgres$ psql
\password
\q

Create a Postgresql user ‘osm’ and a database for the OSM data load. I call the database ‘england’. Add the postgis and hstore extensions to the newly created database.

postgres$ createuser osm
postgres$ createdb -E UTF8 -O osm england
postgres$ psql -c "CREATE EXTENSION hstore;" -d england
postgres$ psql -c "CREATE EXTENSION postgis;" -d england
postgres$ exit

Getting the data

I get OSM data dumps from Geofabrik. I suggest to create a directory to store the raw data.

mkdir ~/osm_data
wget http://download.geofabrik.de/europe/great-britain/england-latest.osm.pbf -O ~/osm_data/england.pbf

Getting the data into PostGIS

Install osm2pgsql and allocate a swap file.

sudo apt install osm2pgsql
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo nano /etc/ssh/ssh_config

Check these settings in the ssh_config file.

Host *

    ClientAliveInterval 30 TCP

    KeepAlive yes

    ClientAliveCountMax 99999

Then restart the sshd service.

sudo service sshd restart

The load into PostGIS itself will require a style. I create a new folder for OSM styles and get Andy‘s default OSM carto style.

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

Now load the data into PostGIS using osm2pgsql.

osm2pgsql --slim -d england -C 3600 --hstore -S ~/osm_styles/openstreetmap-carto/openstreetmap-carto.style ~/osm_data/england.pbf

Install Apache2, Mapnik, Carto

Apache2 is the tile server’s web server.

Mapnik is the rendering engine at the core of making OSM tiles.

CartoCSS is a syntax language for OSM styles.

sudo apt install node-carto libmapnik3.0 libmapnik-dev mapnik-utils python-mapnik apache2 apache2-dev

Build and install mod_tile

mod_tile is a plugin for Apache2 which ties the web server and mapnik render engine together. We need to make and install mod_tile from source.

git clone https://github.com/openstreetmap/mod_tile.git
cd mod_tile/
./autogen.sh
./configure
make
sudo make install
sudo make install-mod_tile

Build the style

We also need to build the OSM style. Style require some world shapefiles. OpenStreetMap-Carto styles comes with a script to get and index these shapefiles.

cd ~/osm_styles/openstreetmap-carto
./get-shapefiles.sh
nano project.mml

We need to make some edits in the project.mml file before we can build the style.

Replace ‘gis’ with database name which you chose earlier e.g. ‘england‘.

In nano this is easily done with the find and replace function (crtl + \). There should be around 70 instances of the database name in the project.mml.

We can build the style now using the carto tool.

carto project.mml > style.xml

Configure renderd

Renderd is the rendering demon for OSM tiles.

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

In the renderd configuration check the host definition, the location of the OSM style, and the mapnik plugin.

[default]

XML=/home/osm/osm_styles/openstreetmap-carto/style.xml

HOST=localhost

[mapnik]

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

Copy the sample rendered.init file into the SystemV init directory in order to start the service when the system boots. Make the file executable with chmod. And open nano to do some config edits.

sudo cp ~/mod_tile/debian/renderd.init /etc/init.d/renderd
sudo chmod a+x /etc/init.d/renderd
sudo nano /etc/init.d/renderd

Check following configs in the renderd startup.

DAEMON=/usr/local/bin/$NAME

DAEMON_ARGS="-c /usr/local/etc/renderd.conf"

RUNASUSER=osm

Make sure that mod_tile is good to go for renderd before we start the service.

sudo mkdir -p /var/lib/mod_tile
sudo chown osm:osm /var/lib/mod_tile
sudo systemctl daemon-reload
sudo service renderd start
sudo service renderd enabled

Let’s edit the mod_tile.load file for Apache2.

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

In this file we tell Apache2 to load the load the mod_tile module.

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

Enable the module and open the default site config for Apache2.

sudo ln -s /etc/apache2/mods-available/mod_tile.load /etc/apache2/mods-enabled/
sudo nano /etc/apache2/sites-enabled/000-default.conf

In this file we set the actual web service and cross origin access.

<VirtualHost *:80>
  ErrorLog ${APACHE_LOG_DIR}/error.log
  LoadTileConfigFile /usr/local/etc/renderd.conf
  ModTileRenderdSocketName /var/run/renderd/renderd.sock
  ModTileRequestTimeout 0
  ModTileMissingRequestTimeout 30
  Header set Access-Control-Allow-Origin "*"
</VirtualHost>

Now we need to restart Apache2 and renderd.

sudo service apache2 restart
sudo service renderd restart

Done! Give yourself a pad on the back and open tile zero in a browser. Here is my OSM world image tile. Note… I tend to use https for pretty much everything and so should you. I am also bad with IP addresses and give all my servers a name and URL redirect.

IMHO the easiest way to check whether you tile server plays well with a web map framework is to setup a jsFiddle or codePen, etc.

It doesn’t matter whether you use Openlayers or Leaflet. I like Leaflet because it is so simple and small. Plus, there is finally a proper Leaflet release.

Here is a quick jsFiddle for the tile server which I just setup.

All good? Well there is one last thing which I wasn’t able to workout. For me renderd does not start when I reboot the server. I have tried all of the suggestion which I could find on the web but so far no cigar.

Here is my gis.stackexchange post in this regard. It would be awesome if you can help.

4 thoughts on “OSM tileserver on a vanilla Ubuntu 16.04 droplet.”

  1. About to attempt this myself, and really glad you made the write-up. Was going to give up and just do 14.04.
    Two things though. Did you ever figure out renderd? Also, do you have some sort of update system like osmosis with it?
    Thanks!

    1. No I haven’t fully worked out renderd and osmosis. To be fair I haven’t spent enough time on this as I am holding out for the advances in vector tiles and WebGL rendering. I am planning to do a writeup on vector tiles in the next couple of weeks.

  2. Hi,

    It’s great tutorial. Manage to have my own OSM tileserver.

    Just another thing, do you mind to share on how I could apply my own styling for the current setup?

    Assuming that I have done styling using TileMill and export it as mapnik XML.

    Appreciate it. Thanks.

    1. I would love to make another tutorial for exactly that purpose. I just have been really short on time in recent months and have not had the time to look at custom styling in earnest. I will probably play with TileMill and see how easy it is to export the style from TileMill to the my own OSM tile server. Another reason is the amazing development of OSM vector tiles. I am not sure where this is going but it is possible that the whole OSM raster tiles stack becomes obsolete.

Leave a Reply

Your email address will not be published. Required fields are marked *