OpenStreetMap, aka OSM, is a user contributed world map that is freely editable. You can think of it as an open-source and self-hosted alternative to Google Maps. This tutorial will show you how to build your own OpenStreetMap tile server on Ubuntu 18.04 so you don’t have to use a proprietary map service.
OpenStreetMap Features
- OpenStreetMap data covers the whole world, making it easy to support users in any country, or every country.
- OpenStreetMap is updated every minute of every hour of every day, and these updates are available to you in real-time.
- OpenStreetMap data is free and open – there is no subscription fee and no page-view fee.
- OpenStreetMap data is rich and detailed, containing huge amounts of data which is relevant to people on the ground – the people who collected it.
Prerequisites/Hardware Requirements
The required RAM and disk space depends on which country’s map you are going to use. For example,
- The UK map requires at least 4G RAM and 60GB disk space.
- The whole planet map requires at least 32G RAM and 1TB SSD disk. It’s not viable to use a spinning hard disk for the whole planet map.
You will need more disk space if you are going to pre-render tiles to speed up map loading in web browser, which is highly recommended. Check this page to see how much disk space are required for pre-rendering tiles. Another thing to note is that importing large map data, like the whole planet, to PostgreSQL database takes a long time. Consider adding more RAM and especially using SSD instead of spinning hard disk to speed up the import process.
If you are going to host the entire world map, I recommend you buy the extra large VPS from Contabo, which boasts
- A 10 core CPU
- 60 GB RAM
- 1.6 TB Intel Optane SSD
It costs just 26.99 €/month.
Step 1: Upgrade Software
It’s always a good idea to update server software before doing any major work on your server. Log into your server via SSH and run the following command.
sudo apt update; sudo apt upgrade
Step 2: Install PostgreSQL Database Server and the PostGIS Extension
We will use PostgreSQL to store map data. PostGIS is a geospatial extenstion to PostgreSQL. Run the following commands to install them.
sudo apt install postgresql postgresql-contrib postgis postgresql-10-postgis-2.4
PostgreSQL database server will automatically start and listens on 127.0.0.1:5432
. The postgres
user will be created on the OS during the installation process. It’s the super user for PostgreSQL database server. By default, this user has no password and there’s no need to set one because you can use sudo
to switch to the postgres
user and log into PostgreSQL server.
sudo -u postgres -i
Now you can create a PostgreSQL database user osm
.
createuser osm
Then create a database named gis
and at the same time make osm
as the owner of the database. -E UTF8
specifies the character encoding scheme to be used in the database is UTF8.
createdb -E UTF8 -O osm gis
Next, create the postgis
and hstore
extension for the gis
database.
psql -c "CREATE EXTENSION postgis;" -d gis psql -c "CREATE EXTENSION hstore;" -d gis
Set osm
as the table owner.
psql -c "ALTER TABLE spatial_ref_sys OWNER TO osm;" -d gis
Exit from the postgres
user.
exit
Create osm
user on your operating system so the tile server can run as osm
user.
sudo adduser osm
Step 3: Download Map Stylesheet and Map Data
Switch to osm user.
su - osm
Download the latest CartoCSS map stylesheets to the osm
user’s home directory. You can check the latest version here. You can always use the following link to download, simply replace the version number.
wget https://github.com/gravitystorm/openstreetmap-carto/archive/v4.20.0.tar.gz
Extract it.
tar xvf v4.20.0.tar.gz
Next, run the following command to download the map data of the whole planet (44G) in PBF (ProtoBufBinary) format.
wget -c http://planet.openstreetmap.org/pbf/planet-latest.osm.pbf
If you want a map of individual country/state/province/city, go to http://download.geofabrik.de. Also, BBBike.org provides extracts of more than 200 cities and regions world-wide in different formats. For example, download the map data of Great Britain (985M) with the following command.
wget -c http://download.geofabrik.de/europe/great-britain-latest.osm.pbf
Exit from osm user.
exit
Recommendations before Importing Map Data
Importing map data takes a lot of RAM. If your physical memory is small, you can easily add a swap file to prevent out-of-memory problem. First we use fallocate
command to create a file. For example, create a file named swapfile with 2G capacity in root file system. (Increase the size of swapfile if you are going to import a large map.)
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
Output:
Setting up swapspace version 1, size = 2097148 KiB no label, UUID=h32b3e10-0779-4865-9ea0-6e2af8f3kea9
Enable the swap file
sudo swapon /swapfile
The import process can take some time. It’s recommended to configure SSH keepalive so that you don’t lose the SSH connection. It’s very easy to do. Just open the SSH client configuration file on your local Linux machine.
sudo nano /etc/ssh/ssh_config
And paste the following text at the end of the file.
ServerAliveInterval 60
Then save the file and connect to your Ubuntu server. You can also access the remote server via VNC to prevent flaky connection interrupting the import process.
Step 4: Import the Map Data to PostgreSQL
To import map data, we need to install osm2pgsql
which converts OpenStreetMap data to postGIS-enabled PostgreSQL databases.
sudo apt install osm2pgsql
Next, swith to osm user.
su - osm
Run the following command to load map stylesheet and map data into the gis
database. Replace great-britain-latest.osm.pbf
with your own map data file.
osm2pgsql --slim -d gis --hstore --multi-geometry --number-processes 8 --tag-transform-script /home/osm/openstreetmap-carto-4.20.0/openstreetmap-carto.lua --style /home/osm/openstreetmap-carto-4.20.0/openstreetmap-carto.style -C 12000 /home/osm/great-britain-latest.osm.pbf
where
--slim
: run in slim mode rather than normal mode. This option is needed if you want to update the map data using OSM change files (OSC) in the future.-d gis
: select database.--hstore
: add tags without column to an additional hstore (key/value) column to PostgreSQL tables--multi-geometry
: generate multi-geometry features in postgresql tables.--style
: specify the location of style file--number-processes
: number of CPU cores on your server-C
flag specify the cache size in MegaBytes. It should be around 80% of the free RAM on your machine. Bigger cache size results in faster import speed. For example, my server has 16GB free RAM, so I can specify -C 12000.- Finally, you need to specify the location of map data file.
If you are going to import the full planet map data, then use the --drop
option and the --flat-nodes
option to increase the import speed. Note that the --flat-nodes
option isn’t suitable for small maps.
osm2pgsql --slim -d gis --drop --flat-nodes nodes.cache --hstore --multi-geometry --number-processes 8 --tag-transform-script /home/osm/openstreetmap-carto-4.20.0/openstreetmap-carto.lua --style /home/osm/openstreetmap-carto-4.20.0/openstreetmap-carto.style -C 12000 /home/osm/great-britain-latest.osm.pbf
RAM usage will gradually increase during the importing process. Once finished, exit from osm user.
exit
Step 5: Install mod_tile and Renderd
mod_tile
is an Apache module that is required to serve tiles and renderd is the rendering daemon for rendering OpenStreetMap tiles. The default Ubuntu repository does not include mod_tile
and renderd
, but we can install them from the OSM PPA.
sudo add-apt-repository ppa:osmadmins/ppa sudo apt install libapache2-mod-tile renderd
During the installation, it will install Apache web server and create an Apache config for mod_tile (/etc/apache2/sites-available/tileserver_site.conf
). The render daemon will automatically start, as can be seen with:
systemctl status renderd
Step 6: Generate Mapnik Stylesheet
Install required packages.
sudo apt install curl unzip gdal-bin mapnik-utils libmapnik-dev nodejs npm sudo npm install -g carto
Switch to osm user.
su - osm
Cd into the carto style directory.
cd /home/osm/openstreetmap-carto-4.20.0/
Get shapefiles.
scripts/get-shapefiles.py
Now build the Mapnik xml stylesheet with the carto
map stylesheet compiler.
carto project.mml > style.xml
Exit from the osm
user.
exit
Step 7: Install Fonts
You need to install the ttf-dejavu
package.
sudo apt install ttf-dejavu
To display non-Latin characters, install the following packages.
sudo apt install fonts-noto-cjk fonts-noto-hinted fonts-noto-unhinted ttf-unifont
Step 8: Configure renderd
Edit renderd config file.
sudo nano /etc/renderd.conf
In the [renderd]
section, change to the number of threads according to the number of CPU cores on your server.
num_threads=4
In the [default]
section, change the value of XML and HOST to the following.
XML=/home/osm/openstreetmap-carto-4.20.0/style.xml
HOST=map.your-domain.com
In [mapnik]
section, change the value of plugins_dir
to the following.
plugins_dir=/usr/lib/mapnik/3.0/input/
You can print the default input plugins directory with the following command.
mapnik-config --input-plugins
If you want to display non-Latin characters, it’s better to change the font settings to the following.
font_dir=/usr/share/fonts/truetype font_dir_recurse=true
Save and close the file. Then edit the init script file
sudo nano /etc/init.d/renderd
Change the following variable. This is needed to load map data from PostgreSQL database.
RUNASUSER=osm
Save the file. Set osm
as the owner of /var/lib/mod_tile/
directory, which will hold the rendered tile files.
sudo chown osm:osm /var/lib/mod_tile/ -R
Then restart renderd service.
sudo systemctl daemon-reload sudo systemctl restart renderd
You need to check the log of renderd.
sudo journalctl -eu renderd
Make sure renderd does not produce any error in the log, or the map won’t be displayed.
Step 9: Configure Apache
Edit the OSM virtual host file.
sudo nano /etc/apache2/sites-available/tileserver_site.conf
Change the ServerName to your own domain name like map.yourdomain.com
. You alos need to create DNS A record for this sub-domain.
ServerName map.yourdomain.com
Save and close the file. Restart Apache.
sudo systemctl restart apache2
Then in your web browser address bar, type
map.your-domain.com/osm/0/0/0.png
You should see the tile of world map. Congrats! You just successfully built your own OSM tile server.
Display Your Tiled Web Map
Tiled web map is also known as slippy map in OpenStreetMap terminology. There are two free and open source JavaScript map libraries you can use for your tile server: OpenLayer and Leaflet. The advantage of Leaflet is that it is simple to use and your map will be mobile-friendly.
OpenLayer
To display your slippy map with OpenLayer, download JavaScript and CSS from openlayer.org and extract it to the web root folder.
cd /var/www/ sudo wget https://github.com/openlayers/openlayers/releases/download/v5.3.0/v5.3.0.zip sudo unzip v5.3.0.zip
Next, create the index.html
file.
sudo nano /var/www/index.html
Paste the following HTML code in the file. Replace red-colored text and adjust the longitude, latitude and zoom level according to your needs.
<meta charset="UTF-8">Accessible Map Go to map
Save and close the file. Now you can view your slippy map by typing your sub-domain in browser address bar.
map.yourdomain.com
or
map.yourdomain.com/index.html
Leaflet
To display your slippy map with Leftlet, download JavaScript and CSS from leftletjs.com and extract it to the web root folder.
cd /var/www/ sudo wget http://cdn.leafletjs.com/leaflet/v1.4.0/leaflet.zip sudo unzip leaflet.zip
Next, create the index.html
file.
sudo nano /var/www/index.html
Paste the following HTML code in the file. Replace red-colored text and adjust the longitude, latitude and zoom level according to your needs.
My first osm
Save and close the file. Now you can view your slippy map by typing your server IP address in browser.
map.yourdomain.com
or
map.yourdomain.com/index.html
Pre-render Tiles
Rendering tiles on-the-fly will increase the map loading time in web browser. To pre-render tiles instead of rendering on the fly, use the following render_list
command. Use -z
and -Z
flag specify the zoom level and replace the number of threads according to the number of CPU cores on your server. Render_list
renders a list of map tiles by sending requests to the rendering daemon. Pre-rendered tiles will be cached in /var/lib/mod_tile
directory.
render_list -m default -a -z 0 -Z 15 --num-threads=8
If later you updated the map data, you can pre-render all tiles again by using the --force
option.
render_list -m default -a -z 0 -Z 15 --num-threads=8 --force
Enabling HTTPS
To encrypt HTTP traffic, we can obtain and install a free TLS certificate from Let’s Encrypt. First, install the Let’s Encrypt client (certbot) on Ubuntu 18.04.
sudo add-apt-repository ppa:certbot/certbot sudo apt install certbot
Since we are using Apache web server, we also need to install the Apache plugin.
sudo apt install python3-certbot-apache
Then run the following command to obtain and install TLS certificate.
sudo certbot --apache --agree-tos --redirect --hsts --staple-ocsp --must-staple --email [email protected] -d map.yourdomain.com
Once the certificate is installed, refresh the web page and you will see a lock in the address bar.
If you see a yellow triangle in Firefox address bar, that means the tile URLs are still using HTTP. You need to edit the index.html file and replace all HTTP protocol with HTTPS.
Enable HTTP2
To furthur improve map loading peformance, you can enable HTTP2 protocol. First, you need to enable the HTTP2 module.
sudo a2enmod http2
Then open the SSL virtual host file.
sudo nano /etc/apache2/sites-enabled/tileserver_site-le-ssl.conf
Put the following directive after the opening
tag.
Protocols h2 http/1.1
Save and close the file. Then restart Apache for the changes to take effect.
sudo systemctl restart apache2
PostgreSQL Database and Web Server on Different Hosts
If your PostgreSQL and Apache web server reside on different hosts, then you need to edit the project.mml
file on the Apache host.
nano /home/osm/openstreetmap-carto-4.20.0/project.mml
Find the following lines:
osm2pgsql: &osm2pgsql type: "postgis" dbname: "gis" key_field: "" geometry_field: "way" extent: "-20037508,-20037508,20037508,20037508"
Specify the IP address of PostgreSQL database server.
osm2pgsql: &osm2pgsql
type: "postgis"
host: "10.0.0.2"
dbname: "gis"
key_field: ""
geometry_field: "way"
extent: "-20037508,-20037508,20037508,20037508"
Save and close the file. Then build the Mapnik xml stylesheet with the carto
map stylesheet compiler.
carto project.mml > style.xml
On the PostgreSQL database server, edit the main configuration file.
sudo nano /etc/postgresql/10/main/postgresql.conf
Add the following line to set PostgreSQL listen on all interfaces.
listen_addresses = '*'
Save and close the file. Then edit the PostgreSQL client authentication configuration file.
sudo nano /etc/postgresql/10/main/pg_hba.conf
Add the following line at the end of the file to allow the osm
user to login from the Apache host. Replace 10.0.0.1 with the IP address of Apache host.
host gis osm 10.0.0.1/32 trust
Save and close the file. Then restart PostgreSQL.
sudo systemctl restart postgresql
Restart the render daemon on the Apache host.
sudo systemctl restart renderd
You need to check the log of renderd. Make sure renderd does not produce any error in the log, or the map won’t be displayed.
sudo journalctl -eu renderd
You should also restrict access to port 5432 of the PostgreSQL database server. For example, you can use the following UFW command to allow the IP address of Apache host only.
sudo ufw allow in from 10.0.0.1 to any port 5432
Conclusion
I hope this turorial helped you set up OpenStreetMap tile server on Ubuntu 18.04. As always, if you found this post useful, then subscribe to our free newsletter to get more tips and tricks. Take care 🙂
Rate this tutorial
[Total: 8 Average: 4.6]