Install on an Ubuntu 24.04 server

This topic provides detailed information about how to set up Open Street Maps on a Ubuntu 24.04 server.

Enable Ubuntu to download postgis

These steps to make the postgis repos visible were sourced from the trac.osgeo.org wiki

sudo apt install ca-certificates gnupg

curl  https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/apt.postgresql.org.gpg >/dev/null

sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

Install the required packages

sudo apt update

sudo apt upgrade

sudo apt install screen locate libapache2-mod-tile renderd git tar unzip wget bzip2 apache2 lua5.1 mapnik-utils python3-mapnik python3-psycopg2 python3-yaml gdal-bin npm postgresql-14 postgresql-contrib postgis postgresql-14-postgis-3 postgresql-14-postgis-3-scripts osm2pgsql net-tools curl

Set up the PostgreSQL database

  1. As the postgres user create a new PostgreSQL user called _renderd and then create the gis database.

    sudo service postgresql start
    
    sudo -u postgres -i
    
    createuser _renderd
    
    createdb -E UTF8 -O _renderd gis
  2. While still working as the "postgres" user, set up PostGIS on the PostgreSQL database:

    psql

    This takes you to a "postgres=#" prompt.

    \c gis

    You should see the message, "You are now connected to database 'gis' as user 'postgres'".

  3. Create extensions and tables.

    CREATE EXTENSION postgis;

    You see a response of CREATE EXTENSION

    CREATE EXTENSION hstore;

    You see a response of CREATE EXTENSION

    ALTER TABLE geometry_columns OWNER TO _renderd;

    You see a response of ALTER TABLE

    ALTER TABLE spatial_ref_sys OWNER TO _renderd;

    You see a response of ALTER TABLE

  4. Quit Postfres, and then exit back to the non-root user.

    \q
    exit

Confirm that Mapnik has been installed

  1. As the non-root user check for Mapnik. The second chevron prompt indicates success.

    python3
    >>> import mapnik
    >>>
  2. Exit python.

    quit()

Configure the mapping stylesheet

  1. Perform the following steps as the non-root user.

    mkdir ~/src
    cd ~/src
    git clone https://github.com/gravitystorm/openstreetmap-carto
    cd openstreetmap-carto
    git pull --all
    git switch --detach v5.9.0
  2. Install the Carto compiler.

    sudo npm install -g carto
    carto -v

    The response should be a number that is at least as high as: 1.2.0

  3. Convert the carto project.

    carto project.mml > mapnik.xml

Loading data

Mapping data for the United Kingdom has been used as an example. Further data is available from sites like Geofabrik.

  1. Create a data directory in the non-root user's home directory.

    mkdir ~/data
    cd ~/data
    wget https://download.geofabrik.de/europe/united-kingdom-latest.osm.pbf
  2. Allow other accounts to access the non-root user's home directory.

    chmod o+rx ~

    Note: If you don't want to grant this access you can change the location of the data, and adjust the following commands.

  3. Use the _renderd Postgres account to load the data into the gis database.

    sudo -u _renderd osm2pgsql -d gis --create --slim  -G --hstore --tag-transform-script ~/src/openstreetmap-carto/openstreetmap-carto.lua -C 2500 --number-processes 1 -S ~/src/openstreetmap-carto/openstreetmap-carto.style ~/data/united-kingdom-latest.osm.pbf

    This will take a while, perhaps 30 minutes depending on your hardware.

  4. Create additional indexes.

    openstreetmap-carto/
    
    sudo -u _renderd psql -d gis -f indexes.sql

    You should see the CREATE INDEX response 16 times

  5. Add database functions.

    sudo -u _renderd psql -d gis -f functions.sql

    You should see the CREATE FUNCTION response 3 times

  6. Add Shapefile data.

    cd ~/src/openstreetmap-carto/
    
    mkdir data
    
    sudo chown _renderd data
    
    sudo -u _renderd scripts/get-external-data.py
  7. Add fonts.

    cd ~/src/openstreetmap-carto/
    
    scripts/get-fonts.sh

    Note: You might see errors reporting that some fonts are not available, however these can be safely ignored.

Setting up the web server

  1. Update the renderd configuration file.

    sudo nano /etc/renderd.conf
  2. Change the number of threads from '4' to '24'.

    [renderd]
    stats_file=/run/renderd/renderd.stats
    socketname=/run/renderd/renderd.sock
    ; num_threads=4
    num_threads=24
    tile_dir=/var/cache/renderd/tiles
  3. Add the rendering style, and make sure to change accountname to the non-root user, such as osm.

    [s2o]
    URI=/hot/
    XML=/home/accountname/src/openstreetmap-carto/mapnik.xml
    HOST=localhost
    TILESIZE=256
    MAXZOOM=20
  4. Save and exit nano.

  5. Update the Apache mod-tile configuration.

    cd /etc/apache2/conf-available/
    
    sudo wget https://raw.githubusercontent.com/openstreetmap/mod_tile/python-implementation/etc/apache2/renderd.conf
    
    sudo a2enconf renderd
    
    sudo systemctl reload apache2
  6. Change the Apache web server listening port from '80' to '8080' within the default site configuration.

    sudo nano /etc/apache2/sites-available/000-default.conf

    Change <VirtualHost *:80> to <VirtualHost *:8080>

    Save and exit nano.

  7. Change the Apache web server listening port from 80 to 8080 within the ports configuration.

    sudo nano /etc/apache2/ports.conf

    Change Listen 80 to Listen 8080

    Save and exit nano.

Set up logging for the renderd service

  1. Edit the service definition.

    sudo nano /usr/lib/systemd/system/renderd.service
  2. Add the logging directive to .

    [Section]
    Environment=G_MESSAGES_DEBUG=all
  3. Save and exit the nano editor.

  4. Reload the render daemon.

    sudo systemctl daemon-reload
    sudo systemctl restart renderd
    sudo systemctl restart apache2

Viewing tiles

We can use sample_leaflet.html to view a very simple map.

  1. Get the sample file.

    cd /var/www/html
    
    sudo wget https://raw.githubusercontent.com/SomeoneElseOSM/mod_tile/switch2osm/extra/sample_leaflet.html
  2. Edit sample_leaflet.html

    sudo nano sample_leaflet.html
    • Change the IP address and port matches your server address rather than 127.0.0.1. e.g. localhost:8080

    • Replace:

      var map = L.map('map').setView([40.36629, 49.83335], 18);

      with

      var map = L.map('map').setView([51.5,0], 5);
  3. Save and exit the nano editor.

  4. Navigate to http://localhost:8080/sample_leaflet.html

Accessing the tile server triggers map tile generation, which might take a few seconds per tile to begin with. The rendered tiles are cached so subsequent performance is much faster.