├── import-postgresql.conf ├── run-postgresql.conf ├── import.sh ├── nginx.conf ├── scripts ├── import_data.sh └── build_tiles.sh ├── LICENSE.md ├── docker-compose.yml └── README.md /import-postgresql.conf: -------------------------------------------------------------------------------- 1 | # from https://wiki.openstreetmap.org/wiki/Osm2pgsql/benchmarks 2 | shared_buffers = 8GB 3 | maintenance_work_mem = 7144MB 4 | work_mem = 128MB 5 | max_wal_size = 2880MB 6 | min_wal_size = 80MB 7 | effective_cache_size = 16GB 8 | 9 | autovacuum = off 10 | -------------------------------------------------------------------------------- /run-postgresql.conf: -------------------------------------------------------------------------------- 1 | # from https://wiki.openstreetmap.org/wiki/Osm2pgsql/benchmarks 2 | shared_buffers = 8GB 3 | maintenance_work_mem = 7144MB 4 | work_mem = 128MB 5 | max_wal_size = 2880MB 6 | min_wal_size = 80MB 7 | effective_cache_size = 16GB 8 | 9 | autovacuum = on 10 | -------------------------------------------------------------------------------- /import.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -eo pipefail 3 | 4 | echo "Creating docker volumes" 5 | docker volume create osm-data 6 | docker volume create osm-tiles 7 | docker volume create valhalla-data 8 | docker volume create nominatim-data 9 | 10 | echo "Importing data for tile server" 11 | docker run \ 12 | -v $(pwd)/osm-pbf/region.osm.pbf:/data/region.osm.pbf \ 13 | -v osm-data:/data/database/ \ 14 | -v $(pwd)/import-postgresql.conf:/etc/postgresql/14/main/postgresql.custom.conf.tmpl \ 15 | -e THREADS=16 \ 16 | -e "OSM2PGSQL_EXTRA_ARGS=-C 16384" \ 17 | overv/openstreetmap-tile-server \ 18 | import 19 | 20 | echo "Importing data for valhalla" 21 | docker run \ 22 | -v $(pwd)/osm-pbf/region.osm.pbf:/data/region.osm.pbf \ 23 | -v $(pwd)/scripts:/scripts \ 24 | -v valhalla-data:/data/valhalla \ 25 | valhalla/valhalla:run-latest \ 26 | /scripts/import_data.sh 27 | 28 | -------------------------------------------------------------------------------- /nginx.conf: -------------------------------------------------------------------------------- 1 | # add to /etc/nginx/sites-available/default 2 | server { 3 | server_name maps.wcedmisten.dev; 4 | location / { 5 | proxy_set_header Host $host; 6 | proxy_pass http://127.0.0.1:8080; 7 | proxy_redirect off; 8 | } 9 | } 10 | 11 | server { 12 | server_name valhalla.wcedmisten.dev; 13 | location / { 14 | proxy_set_header Host $host; 15 | proxy_pass http://127.0.0.1:8002; 16 | proxy_redirect off; 17 | } 18 | } 19 | 20 | server { 21 | server_name nominatim.wcedmisten.dev; 22 | location / { 23 | proxy_set_header Host $host; 24 | proxy_pass http://127.0.0.1:8010; 25 | proxy_redirect off; 26 | } 27 | } 28 | 29 | server { 30 | server_name map-demo.wcedmisten.dev; 31 | location / { 32 | proxy_set_header Host $host; 33 | proxy_pass http://127.0.0.1:8020; 34 | proxy_redirect off; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /scripts/import_data.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | mkdir /data/valhalla/valhalla_tiles 4 | 5 | if ! test -f "/data/valhalla/valhalla.json"; then 6 | echo "Valhalla JSON not found. Creating config." 7 | valhalla_build_config --mjolnir-tile-dir /data/valhalla/valhalla_tiles --mjolnir-tile-extract /data/valhalla/valhalla_tiles.tar --mjolnir-timezone /data/valhalla/valhalla_tiles/timezones.sqlite --mjolnir-admin /data/valhalla/valhalla_tiles/admins.sqlite > /data/valhalla/valhalla.json 8 | fi 9 | 10 | if ! test -f "/data/valhalla/valhalla_tiles/timezones.sqlite"; then 11 | echo "Valhalla tiles not found. Building now." 12 | valhalla_build_timezones > /data/valhalla/valhalla_tiles/timezones.sqlite 13 | valhalla_build_tiles -c /data/valhalla/valhalla.json /data/region.osm.pbf 14 | fi 15 | 16 | if ! test -f "/data/valhalla/valhalla_tiles.tar"; then 17 | echo "Tile extract not found. Extracting now." 18 | valhalla_build_extract -c /data/valhalla/valhalla.json -v 19 | fi 20 | 21 | -------------------------------------------------------------------------------- /scripts/build_tiles.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | mkdir /data/valhalla/valhalla_tiles 4 | 5 | if ! test -f "/data/valhalla/valhalla.json"; then 6 | echo "Valhalla JSON not found. Creating config." 7 | valhalla_build_config --mjolnir-tile-dir /data/valhalla/valhalla_tiles --mjolnir-tile-extract /data/valhalla/valhalla_tiles.tar --mjolnir-timezone /data/valhalla/valhalla_tiles/timezones.sqlite --mjolnir-admin /data/valhalla/valhalla_tiles/admins.sqlite > /data/valhalla/valhalla.json 8 | fi 9 | 10 | if ! test -f "/data/valhalla/valhalla_tiles/timezones.sqlite"; then 11 | echo "Valhalla tiles not found. Building now." 12 | valhalla_build_timezones > /data/valhalla/valhalla_tiles/timezones.sqlite 13 | valhalla_build_tiles -c /data/valhalla/valhalla.json /data/valhalla/region.osm.pbf 14 | fi 15 | 16 | if ! test -f "/data/valhalla/valhalla_tiles.tar"; then 17 | echo "Tile extract not found. Extracting now." 18 | valhalla_build_extract -c /data/valhalla/valhalla.json -v 19 | fi 20 | 21 | echo "Starting valhalla server." 22 | valhalla_service /data/valhalla/valhalla.json 1 23 | 24 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 William Edmisten 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | 3 | services: 4 | tile-server: 5 | container_name: tile-server 6 | image: 7 | overv/openstreetmap-tile-server 8 | volumes: 9 | - osm-data:/data/database/ 10 | - osm-tiles:/data/tiles/ 11 | - ./run-postgresql.conf:/etc/postgresql/14/main/postgresql.custom.conf.tmpl 12 | shm_size: "5g" 13 | ports: 14 | - "8080:80" 15 | environment: 16 | - THREADS=16 17 | - ALLOW_CORS=enabled 18 | - UPDATES=enabled 19 | command: "run" 20 | 21 | valhalla: 22 | container_name: valhalla 23 | image: valhalla/valhalla:run-latest 24 | ports: 25 | - "8002:8002" 26 | volumes: 27 | - valhalla-data:/data/valhalla 28 | command: "valhalla_service /data/valhalla/valhalla.json 1" 29 | 30 | nominatim: 31 | container_name: nominatim 32 | image: mediagis/nominatim:4.1 33 | shm_size: "5g" 34 | ports: 35 | - "8010:8080" 36 | volumes: 37 | - nominatim-data:/var/lib/postgresql/14/main 38 | - ./osm-pbf/region.osm.pbf:/nominatim/data/region.osm.pbf 39 | environment: 40 | - PBF_PATH=/nominatim/data/region.osm.pbf 41 | # only import admin boundaries and places. cuts down the DB size by a LOT 42 | - IMPORT_STYLE=full 43 | 44 | demo: 45 | container_name: maps-demo 46 | build: 47 | context: valhalla-app 48 | ports: 49 | - "8020:80" 50 | 51 | volumes: 52 | osm-data: 53 | external: true 54 | osm-tiles: 55 | external: true 56 | valhalla-data: 57 | external: true 58 | nominatim-data: 59 | external: true 60 | 61 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Running a full valhalla demo entirely self hosted! 2 | 3 | # Prerequisites 4 | 5 | Must have docker and docker compose installed. 6 | 7 | https://docs.docker.com/engine/install/ubuntu/ 8 | 9 | # Hardware Requirements 10 | 11 | The configuration for PostgreSQL has been tuned for a machine with 12 | 16 cores and 128 GB RAM. These config files should be adjusted if 13 | you are running this on a smaller machine. 14 | 15 | See the [osm2pgsql docs](https://osm2pgsql.org/doc/manual.html#tuning-the-postgresql-server) 16 | for more information on how to adjust this config. 17 | 18 | # Clone the demo app 19 | 20 | ``` 21 | git clone git@github.com:wcedmisten/valhalla-app.git 22 | cd valhalla-app 23 | git checkout self-hosted 24 | cd .. 25 | ``` 26 | 27 | # Build the demo app 28 | 29 | ``` 30 | docker compose build 31 | ``` 32 | 33 | # Import the data 34 | 35 | ``` 36 | mkdir ./osm-pbf 37 | cp /path/to/osm.pbf ./osm-pbf/region.osm.pbf 38 | ./import.sh 39 | ``` 40 | 41 | # Run the apps 42 | 43 | ``` 44 | docker compose up 45 | ``` 46 | 47 | # Postgres config 48 | 49 | import-postgresql.conf is used for import. 50 | The main difference is that autovacuum is off. 51 | 52 | run-postgresql.conf has it turned on. 53 | This config is tuned for my machine with 128 GB of RAM and 16 CPUs. 54 | 55 | # Pre-render tiles at low zoom 56 | You may want to expand the parameters used to include higher zoom levels. 57 | E.g. using `-z 2 -Z 8` to pre-render up to level 8. 58 | Increasing zoom levels will take exponentionally more time to render 59 | and more space to store, but will provide a better experience for the user. 60 | 61 | There are also better ways to render zoom levels that don't include 62 | ocean tiles that should be used instead. 63 | 64 | ``` 65 | docker exec -it tile-server bash 66 | sudo -u _renderd render_list -t /data/tiles -a -z 2 -Z 3 -n 16 67 | ``` 68 | 69 | --------------------------------------------------------------------------------