commit 59068928e6a343c8872af27b89460b1c6ece7a84 Author: Alexander Overvoorde Date: Tue May 8 22:43:34 2018 +0200 Initial version diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..810fd9a --- /dev/null +++ b/Dockerfile @@ -0,0 +1,98 @@ +FROM ubuntu:18.04 + +# Based on +# https://switch2osm.org/manually-building-a-tile-server-18-04-lts/ + +# Install dependencies +RUN apt-get update +RUN apt-get install -y libboost-all-dev git-core tar unzip wget bzip2 build-essential autoconf libtool libxml2-dev libgeos-dev libgeos++-dev libpq-dev libbz2-dev libproj-dev munin-node munin libprotobuf-c0-dev protobuf-c-compiler libfreetype6-dev libtiff5-dev libicu-dev libgdal-dev libcairo-dev libcairomm-1.0-dev apache2 apache2-dev libagg-dev liblua5.2-dev ttf-unifont lua5.1 liblua5.1-dev libgeotiff-epsg + +# Set up environment and renderer user +ENV TZ=UTC +RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone +RUN adduser --disabled-password --gecos "" renderer +USER renderer + +# Install latest osm2pgsql +RUN mkdir /home/renderer/src +WORKDIR /home/renderer/src +RUN git clone https://github.com/openstreetmap/osm2pgsql.git +WORKDIR /home/renderer/src/osm2pgsql +USER root +RUN apt-get install -y make cmake g++ libboost-dev libboost-system-dev libboost-filesystem-dev libexpat1-dev zlib1g-dev libbz2-dev libpq-dev libgeos-dev libgeos++-dev libproj-dev lua5.2 liblua5.2-dev +USER renderer +RUN mkdir build +WORKDIR /home/renderer/src/osm2pgsql/build +RUN cmake .. +RUN make +USER root +RUN make install +USER renderer + +# Install and test Mapnik +USER root +RUN apt-get -y install autoconf apache2-dev libtool libxml2-dev libbz2-dev libgeos-dev libgeos++-dev libproj-dev gdal-bin libmapnik-dev mapnik-utils python-mapnik +USER renderer +RUN python -c 'import mapnik' + +# Install mod_tile and renderd +WORKDIR /home/renderer/src +RUN git clone -b switch2osm https://github.com/SomeoneElseOSM/mod_tile.git +WORKDIR /home/renderer/src/mod_tile +RUN ./autogen.sh +RUN ./configure +RUN make +USER root +RUN make install +RUN make install-mod_tile +RUN ldconfig +USER renderer + +# Configure stylesheet +WORKDIR /home/renderer/src +RUN git clone https://github.com/gravitystorm/openstreetmap-carto.git +WORKDIR /home/renderer/src/openstreetmap-carto +USER root +RUN apt-get install -y npm nodejs +RUN npm install -g carto +USER renderer +RUN carto -v +RUN carto project.mml > mapnik.xml + +# Load shapefiles +WORKDIR /home/renderer/src/openstreetmap-carto +RUN scripts/get-shapefiles.py + +# Install fonts +USER root +RUN apt-get install -y fonts-noto-cjk fonts-noto-hinted fonts-noto-unhinted ttf-unifont +USER renderer + +# Configure renderd +USER root +RUN sed -i 's/renderaccount/renderer/g' /usr/local/etc/renderd.conf +RUN sed -i 's/hot/tile/g' /usr/local/etc/renderd.conf +USER renderer + +# Configure Apache +USER root +RUN mkdir /var/lib/mod_tile +RUN chown renderer /var/lib/mod_tile +RUN mkdir /var/run/renderd +RUN chown renderer /var/run/renderd +RUN echo "LoadModule tile_module /usr/lib/apache2/modules/mod_tile.so" >> /etc/apache2/conf-available/mod_tile.conf +RUN a2enconf mod_tile +COPY apache.conf /etc/apache2/sites-available/000-default.conf +USER renderer + +# Install PostgreSQL +USER root +RUN apt-get install -y postgresql postgresql-contrib postgis postgresql-10-postgis-2.4 +USER renderer + +# Start running +USER root +RUN apt-get install -y sudo +COPY run.sh / +ENTRYPOINT ["/run.sh"] +CMD [] \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..247c1e2 --- /dev/null +++ b/Makefile @@ -0,0 +1,12 @@ +.PHONY: build push test + +build: + docker build -t overv/openstreetmap-tile-server . + +push: build + docker push overv/openstreetmap-tile-server:latest + +test: build + docker volume create openstreetmap-data + docker run -v openstreetmap-data:/var/lib/postgresql/10/main overv/openstreetmap-tile-server import + docker run -v openstreetmap-data:/var/lib/postgresql/10/main -p 80:80 -d overv/openstreetmap-tile-server run \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..4ef586e --- /dev/null +++ b/README.md @@ -0,0 +1,54 @@ +# openstreetmap-tile-server + +This container allows you to easily set up an OpenStreetMap PNG tile server given a `.osm.pbf` file. It is based on the [latest Ubuntu 18.04 LTS guide](https://switch2osm.org/manually-building-a-tile-server-18-04-lts/) from [switch2osm.org](https://switch2osm.org/) and therefore uses the default OpenStreetMap style. + +## Setting up the server + +First create a Docker volume to hold the PostgreSQL database that will contain the OpenStreetMap data: + + docker volume create openstreetmap-data + +Next, download an .osm.pbf extract from geofabrik.de for the region that you're interested in. You can then start importing it into PostgreSQL by running a container and mounting the file as `/data.osm.pbf`. For example: + + docker run -v luxembourg.osm.pbf:/data.osm.pbf -v openstreetmap-data:/var/lib/postgresql/10/main overv/openstreetmap-tile-server import + +If the container exits without errors, then your data has been successfully imported and you are now ready to run the tile server. + +## Running the server + +Run the server like this: + + docker run -p 80:80 -v openstreetmap-data:/var/lib/postgresql/10/main -d overv/openstreetmap-tile-server run + +Your tiles will now be available at http://localhost:80/tile/{z}/{x}/{y}.png. If you open `leaflet-demo.html` in your browser, you should be able to see the tiles served by your own machine. Note that it will initially quite a bit of time to render the larger tiles for the first time. + +## Preserving rendered tiles + +Tiles that have already been rendered will be stored in `/var/lib/mod_tile`. To make sure that this data survives container restarts, you should create another volume for it: + + docker volume create openstreetmap-rendered-tiles + docker run -p 80:80 -v openstreetmap-data:/var/lib/postgresql/10/main -v openstreetmap-rendered-tiles:/var/lib/mod_tile -d overv/openstreetmap-tile-server run + +## Performance tuning + +The import and tile serving processes use 4 threads by default, but this number can be changed by setting the `THREADS` environment variable. For example: + + docker run -p 80:80 -e THREADS=24 -v openstreetmap-data:/var/lib/postgresql/10/main -d overv/openstreetmap-tile-server run + +## License + +``` +Copyright 2018 Alexander Overvoorde + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +``` \ No newline at end of file diff --git a/apache.conf b/apache.conf new file mode 100644 index 0000000..77d1627 --- /dev/null +++ b/apache.conf @@ -0,0 +1,13 @@ + + ServerAdmin webmaster@localhost + + LoadTileConfigFile /usr/local/etc/renderd.conf + ModTileRenderdSocketName /var/run/renderd/renderd.sock + ModTileRequestTimeout 0 + ModTileMissingRequestTimeout 30 + + DocumentRoot /var/www/html + + ErrorLog ${APACHE_LOG_DIR}/error.log + CustomLog ${APACHE_LOG_DIR}/access.log combined + diff --git a/leaflet-demo.html b/leaflet-demo.html new file mode 100644 index 0000000..c3b626b --- /dev/null +++ b/leaflet-demo.html @@ -0,0 +1,35 @@ + + + + Custom Tile Server + + + + + + + + + + + +
+ + + + diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..353c9bd --- /dev/null +++ b/run.sh @@ -0,0 +1,50 @@ +#!/bin/bash + +if [ "$#" -ne 1 ]; then + echo "usage: " + echo "commands:" + echo " import: Set up the database and import /data.osm.pbf" + echo " run: Runs Apache and renderd to serve tiles at /tile/{z}/{x}/{y}.png" + echo "environment variables:" + echo " THREADS: defines number of threads used for importing / tile rendering" + exit 1 +fi + +if [ "$1" = "import" ]; then + # Initialize PostgreSQL + service postgresql start + sudo -u postgres createuser renderer + sudo -u postgres createdb -E UTF8 -O renderer gis + sudo -u postgres psql -d gis -c "CREATE EXTENSION postgis;" + sudo -u postgres psql -d gis -c "CREATE EXTENSION hstore;" + sudo -u postgres psql -d gis -c "ALTER TABLE geometry_columns OWNER TO renderer;" + sudo -u postgres psql -d gis -c "ALTER TABLE spatial_ref_sys OWNER TO renderer;" + + # Download Luxembourg as sample if no data is provided + if [ ! -f /data.osm.pbf ]; then + echo "WARNING: No import file at /data.osm.pbf, so importing Luxembourg as example..." + wget -nv http://download.geofabrik.de/europe/luxembourg-latest.osm.pbf -O /data.osm.pbf + fi + + # Import data + sudo -u renderer osm2pgsql -d gis --create --slim -G --hstore --tag-transform-script /home/renderer/src/openstreetmap-carto/openstreetmap-carto.lua -C 2048 --number-processes ${THREADS:-4} -S /home/renderer/src/openstreetmap-carto/openstreetmap-carto.style /data.osm.pbf + + exit 0 +fi + +if [ "$1" = "run" ]; then + # Initialize PostgreSQL and Apache + service postgresql start + service apache2 restart + + # Configure renderd threads + sed -i -E "s/num_threads=[0-9]+/num_threads=${THREADS:-4}/g" /usr/local/etc/renderd.conf + + # Run + sudo -u renderer renderd -f -c /usr/local/etc/renderd.conf + + exit 0 +fi + +echo "invalid command" +exit 1 \ No newline at end of file