Skip to content

Deploying OpenStreetMap Tile Server

Introduction

OpenStreetMap (OSM) is a collaborative project that creates and distributes free geographic data for the world. While you can use public OSM tile servers, hosting your own tile server gives you complete control over styling, caching, and availability. Self-hosting eliminates rate limits, ensures privacy, and allows customization of map appearance.

A tile server renders map data into image tiles that web mapping libraries like Leaflet and OpenLayers display to users. By deploying your own tile server, you can serve maps for offline applications, customize map styling to match your brand, and ensure consistent availability for your applications.

Key highlights of self-hosting OpenStreetMap tiles:

  • No Rate Limits: Serve unlimited map tiles to your applications without usage restrictions
  • Custom Styling: Create custom map styles that match your application’s design
  • Data Privacy: Keep map requests within your infrastructure without third-party tracking
  • Offline Capability: Serve maps in air-gapped or limited connectivity environments
  • Consistent Performance: Guaranteed availability independent of public tile server status
  • Regional Focus: Import only the regions you need, reducing storage requirements
  • Vector Tiles: Serve modern vector tiles for dynamic client-side styling
  • Historical Data: Maintain historical map data for time-based applications

This guide walks through deploying an OpenStreetMap tile server on Klutch.sh using Docker with the popular openstreetmap-tile-server image.

Why Deploy OpenStreetMap on Klutch.sh

Deploying an OpenStreetMap tile server on Klutch.sh provides several advantages:

Simplified Deployment: Klutch.sh automatically builds and deploys your tile server without complex configuration. Push your Dockerfile to GitHub and deploy with a click.

Persistent Storage: Attach persistent volumes for the PostgreSQL/PostGIS database and rendered tiles. Your imported map data survives container restarts.

HTTPS by Default: Klutch.sh provides automatic SSL certificates, ensuring secure tile delivery to your applications.

Scalable Resources: Map tile rendering is resource-intensive. Allocate CPU and memory based on your coverage area and expected traffic.

GitHub Integration: Connect your configuration repository for automatic redeployments when you update styles or configuration.

Custom Domains: Assign a custom domain for your tile server to integrate seamlessly with your applications.

Always-On Availability: Your map tiles remain accessible 24/7 without managing your own hardware or network infrastructure.

Prerequisites

Before deploying an OpenStreetMap tile server on Klutch.sh, ensure you have:

  • A Klutch.sh account
  • A GitHub account with a repository for your tile server configuration
  • Basic familiarity with Docker and containerization concepts
  • Understanding of map data and geographic concepts
  • OpenStreetMap data file (PBF format) for your region of interest

Understanding Tile Server Architecture

A complete OpenStreetMap tile server consists of several components:

PostgreSQL/PostGIS Database: Stores the imported OSM data in a geographic database format optimized for spatial queries.

osm2pgsql: Imports raw OSM data (PBF files) into the PostgreSQL database with appropriate schemas and indexes.

Mapnik: A map rendering library that generates image tiles from database data using style definitions.

mod_tile/renderd: Apache module and rendering daemon that manages tile requests, caching, and on-demand rendering.

Apache HTTP Server: Serves the rendered tiles to clients with appropriate caching headers.

Preparing Your Repository

Create a GitHub repository containing your Dockerfile and configuration for tile server deployment.

Repository Structure

osm-tile-server/
├── Dockerfile
├── .dockerignore
└── README.md

Creating the Dockerfile

Create a Dockerfile using the popular openstreetmap-tile-server image:

FROM overv/openstreetmap-tile-server:latest
# Environment configuration
ENV THREADS=4
ENV OSM2PGSQL_EXTRA_ARGS=""
ENV ALLOW_CORS=enabled
# The data will be imported at runtime
# Mount your PBF file to /data/region.osm.pbf
# Expose the tile server port
EXPOSE 80
# Use the default entrypoint from the base image

Advanced Dockerfile with Custom Styling

For custom map styles, extend the configuration:

FROM overv/openstreetmap-tile-server:latest
# Performance tuning
ENV THREADS=4
ENV FLAT_NODES=enabled
ENV OSM2PGSQL_EXTRA_ARGS="--cache 4096"
ENV ALLOW_CORS=enabled
# Create directories for custom styles
RUN mkdir -p /custom-styles
# Copy custom style files (if any)
# COPY ./styles /custom-styles
# Expose the tile server port
EXPOSE 80

Environment Variables Reference

The tile server supports various configuration options:

VariableDefaultDescription
THREADS4Number of rendering threads
FLAT_NODESdisabledUse flat nodes file for large imports
OSM2PGSQL_EXTRA_ARGS-Additional osm2pgsql import arguments
ALLOW_CORSdisabledEnable CORS headers for cross-origin requests
UPDATESdisabledEnable automatic OSM data updates

Deploying OpenStreetMap on Klutch.sh

Follow these steps to deploy your OpenStreetMap tile server:

    Download OpenStreetMap Data

    Download a PBF file for your region from Geofabrik:

    Terminal window
    # Example: Download data for a small region
    wget https://download.geofabrik.de/north-america/us/delaware-latest.osm.pbf

    Choose the smallest region that covers your needs, as larger regions require significantly more storage and import time.

    Push Your Repository to GitHub

    Initialize and push your repository:

    Terminal window
    git init
    git add Dockerfile .dockerignore README.md
    git commit -m "Initial OpenStreetMap tile server configuration"
    git remote add origin https://github.com/yourusername/osm-tile-server.git
    git push -u origin main

    Create a New Project on Klutch.sh

    Navigate to the Klutch.sh dashboard and create a new project named “osm-tiles” or similar.

    Create a New App

    Within your project, create a new app. Connect your GitHub account and select the repository containing your Dockerfile.

    Configure HTTP Traffic

    In the deployment settings:

    • Select HTTP as the traffic type
    • Set the internal port to 80

    Set Environment Variables

    Configure environment variables for your deployment:

    VariableValue
    THREADS4 (adjust based on allocated resources)
    ALLOW_CORSenabled
    FLAT_NODESenabled (for large regions)

    Attach Persistent Volumes

    Tile servers require substantial storage. Add the following volumes:

    Mount PathRecommended SizePurpose
    /data/database50-500 GBPostgreSQL/PostGIS database
    /data/tiles10-100 GBRendered tile cache
    /data/region.osm.pbfBased on regionOSM data file (upload separately)

    Upload OSM Data

    Before the initial import, upload your PBF file to the persistent volume at /data/region.osm.pbf.

    Deploy Your Application

    Click Deploy to start the build process. The initial import can take hours depending on region size and allocated resources.

    Access Your Tile Server

    Once deployment and import complete, access tiles at:

    https://your-app-name.klutch.sh/tile/{z}/{x}/{y}.png

Using Your Tile Server

Integration with Leaflet

Add your tiles to a Leaflet map:

var map = L.map('map').setView([51.505, -0.09], 13);
L.tileLayer('https://your-app-name.klutch.sh/tile/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '&copy; <a href="https://openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);

Integration with OpenLayers

Use with OpenLayers:

import Map from 'ol/Map';
import View from 'ol/View';
import TileLayer from 'ol/layer/Tile';
import XYZ from 'ol/source/XYZ';
const map = new Map({
target: 'map',
layers: [
new TileLayer({
source: new XYZ({
url: 'https://your-app-name.klutch.sh/tile/{z}/{x}/{y}.png'
})
})
],
view: new View({
center: [0, 0],
zoom: 2
})
});

Managing Map Data

Importing New Regions

To import additional or updated data:

  1. Upload the new PBF file to your volume
  2. Trigger a re-import by restarting the container with appropriate flags

Updating Map Data

Enable automatic updates for fresh data:

  1. Set UPDATES=enabled in environment variables
  2. The server will download and apply daily diffs from OSM

Storage Considerations

Map data storage requirements vary significantly:

RegionDatabase SizeTile Cache
Small city1-5 GB5-20 GB
State/Province10-50 GB20-100 GB
Country50-200 GB100-500 GB
Continent200+ GB500+ GB
Planet1+ TB2+ TB

Performance Optimization

Tile Caching

The server automatically caches rendered tiles. For high-traffic deployments:

  • Pre-render tiles for popular zoom levels
  • Use a CDN in front of your tile server
  • Increase the tile cache volume size

Resource Allocation

Map rendering is CPU and memory intensive:

  • Allocate 4+ CPU cores for responsive rendering
  • Provide 8+ GB RAM for the PostgreSQL database
  • Use SSD storage for faster tile generation

Rendering Strategies

Choose a rendering approach:

  • On-Demand: Tiles rendered when first requested (default)
  • Pre-Rendering: Generate all tiles for specific zoom levels in advance
  • Hybrid: Pre-render common areas, on-demand for others

Custom Map Styling

Using CartoCSS

Customize map appearance with CartoCSS stylesheets:

  1. Create a custom style directory
  2. Modify the CartoCSS files for colors, fonts, and features
  3. Rebuild the style and restart the renderer

Style Resources

Troubleshooting Common Issues

Tiles Not Rendering

Symptoms: Blank or missing tiles.

Solutions:

  • Verify the database import completed successfully
  • Check renderer logs for errors
  • Ensure sufficient disk space for tile cache
  • Verify the requested coordinates are within imported region

Slow Tile Generation

Symptoms: Long wait times for new tiles.

Solutions:

  • Increase THREADS environment variable
  • Allocate more CPU resources
  • Pre-render popular zoom levels
  • Enable flat nodes for large datasets

Database Import Failures

Symptoms: Import process crashes or stalls.

Solutions:

  • Increase available memory
  • Enable flat nodes mode
  • Use smaller region extracts
  • Check PostgreSQL logs for specific errors

Out of Storage

Symptoms: Import or rendering fails with disk errors.

Solutions:

  • Increase persistent volume sizes
  • Clean old tile cache
  • Use smaller region extract
  • Enable tile expiration

Additional Resources

Conclusion

Deploying an OpenStreetMap tile server on Klutch.sh gives you complete control over your map infrastructure. With self-hosted tiles, you eliminate rate limits, customize styling to match your brand, and ensure consistent availability for your mapping applications.

The combination of persistent storage for your database and tile cache, automatic HTTPS, and scalable resources makes Klutch.sh an excellent platform for map tile hosting. Whether you need maps for a single application or want to serve tiles across multiple projects, your self-hosted tile server provides the reliability and flexibility that public tile servers cannot match.

Start with a small region to test your deployment, then expand coverage as needed. With proper caching and resource allocation, your tile server can handle significant traffic while maintaining responsive tile delivery.