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.04working 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.
usermod -aG sudo 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
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
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
Getting the data
I get OSM data dumps from Geofabrik. I suggest to create a directory to store the raw 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.
ClientAliveInterval 30 TCP
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.
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
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.
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
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.
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.
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.
Header set Access-Control-Allow-Origin "*"
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.
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.