Skip to content

Instantly share code, notes, and snippets.

@coder4web
Last active June 7, 2021 08:58
Show Gist options
  • Save coder4web/39e981adbc859db62576dc6840a4023b to your computer and use it in GitHub Desktop.
Save coder4web/39e981adbc859db62576dc6840a4023b to your computer and use it in GitHub Desktop.
CentOS 7 Open Street Map Tile Server
# Based on next manuals:
# Ubuntu 18.04 LTS: https://switch2osm.org/manually-building-a-tile-server-18-04-lts/
# CentOS 7 - https://www.hyperlearning.ai/en/knowledgebase/blog/centos-7-open-street-map-tile-server
# Updated to latest CentOS / deps packages
# Dependencies
sudo yum install git gdal sqlite geos boost curl libcurl libicu bzip2-devel zlib-devel libxml2-devel python-setuptools proj-devel proj proj-epsg proj-nad libicu-devel gdal-devel sqlite-devel libcurl-devel geos-devel protobuf-devel protobuf-c-devel lua-devel cmake proj boost-thread proj-devel autoconf automake libtool pkgconfig ragel gtk-doc glib2 glib2-devel libtool-ltdl-devel python-devel boost-devel cabextract xorg-x11-font-utils fontconfig perl-DBD-Pg mesa-libGLU-devel
sudo yum install cairo pycairo cairo-devel pycairo-devel freetype freetype-devel harfbuzz harfbuzz-devel harfbuzz-icu libjpeg libjpeg-devel libpng libpng-devel libtiff libtiff-devel libwebp libwebp-devel
sudo mkdir -p /usr/local/src/osm_tile_server
cd /usr/local/src/osm_tile_server # custom source dir with user's RW access rights
ln -s /usr/local/src/osm_tile_server ~
# Mapnik and mod_tile now requires C++14 compliant compiler (-std=c++14)
# https://www.softwarecollections.org/
sudo yum install centos-release-scl
# https://www.softwarecollections.org/en/scls/rhscl/devtoolset-8/
# https://www.softwarecollections.org/en/scls/rhscl/llvm-toolset-7/
sudo yum install devtoolset-9 llvm-toolset-7
scl enable devtoolset-9 llvm-toolset-7 bash
gcc --version #9.x.x
clang --version #5.x.x
# PostgreSQL / Postgres Pro
# @see https://gist.github.com/coder4web/c6d7c3810518063f80ed145e1ed0e562
# @see https://github.com/openstreetmap/osm2pgsql/blob/master/install-postgis-osm-db.sh
# PostGIS 3
# https://postgis.net/install/
yum install postgis30_12 postgis30_12-utils postgis30_12-client ogr_fdw12 pgrouting_12
# db / user setup
if [ -z $DBOWNER ]; then
DBOWNER=gis
fi
if [ -z $DBNAME ]; then
DBNAME=gis
fi
# CREATE DATABASE gis WITH ENCODING = 'UTF8';
sudo -u postgres createdb -EUTF8 -O $DBOWNER $DBNAME
sudo -u postgres createuser --no-superuser --no-createdb --no-createrole "$DBOWNER" || true
sudo -u postgres createlang plpgsql $DBNAME || true
\c gis
# Spatial Extentions
CREATE EXTENSION postgis;
CREATE EXTENSION hstore;
\dx
CREATE USER apache WITH password '***';
GRANT ALL ON schema public TO apache;
GRANT ALL ON ALL TABLES IN SCHEMA public TO apache;
GRANT ALL ON ALL SEQUENCES IN SCHEMA public TO apache;
GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA public TO apache;
ALTER TABLE geometry_columns OWNER TO apache;
ALTER TABLE spatial_ref_sys OWNER TO apache;
psql -h 127.0.0.1 -d gis
SELECT pg_size_pretty(pg_database_size('gis'));
# mapnik
# https://mapnik.org/
sudo nano /etc/profile.d/pgsql.sh
:'SRC
export PATH=$PATH:/usr/pgsql-12/bin:/usr/pgsql-12/lib:/usr/local/lib
'
source /etc/profile.d/pgsql.sh
git clone git://github.com/mapnik/mapnik
cd mapnik
git submodule sync
git submodule update --init deps/mapbox/variant
git submodule update --init deps/mapbox/geometry
git submodule update --init deps/mapbox/polylabel
git submodule update --init deps/mapbox/protozero
./bootstrap.sh
./configure
make
sudo make install
sudo ldconfig
cd ..
# carto AKA CartoCSS - is a language for map design, allowing you to edit the style of maps including shapes, polygons, fonts and colours
# https://github.com/mapbox/carto/
sudo yum install nodejs #Carto can be installed via NodeJS
npm -v #3.10.10
sudo npm install -g carto
# OpenStreetMap Carto - stylesheet for the standard map layer for Open Street Map-based maps
# https://github.com/gravitystorm/openstreetmap-carto
git clone git://github.com/gravitystorm/openstreetmap-carto.git
cd openstreetmap-carto
# Compile and download shape files
carto project.mml > mapnik.xml
scripts/get-shapefiles.py
# osm2pgsql - converts OpenStreetMap data to postGIS-enabled PostgreSQL databases.
# https://wiki.openstreetmap.org/wiki/Osm2pgsql
git clone git://github.com/openstreetmap/osm2pgsql.git
cd osm2pgsql
mkdir build && cd build && cmake ..
make
sudo make install
cd ..
# Import Map Data into PostgreSQL
export PATH=$PATH:/usr/pgsql-12/bin
mkdir ~/osm_data
cd ~/osm_data
wget -c https://download.bbbike.org/osm/bbbike/Moscow/Moscow.osm.pbf
osm2pgsql -d gis --create --slim -G --hstore --tag-transform-script ~/osm_tile_server/openstreetmap-carto/openstreetmap-carto.lua -C 2500 --number-processes 1 -S ~/osm_tile_server/openstreetmap-carto/openstreetmap-carto.style ~/osm_data/Moscow.osm.pbf
#psql -h 127.0.0.1 -d gis -U osm
#osm2pgsql -H 127.0.0.1 -d gis -U osm --slim -C 1600 --number-process 1 -S /usr/local/share/osm2pgsql/default.style Moscow.osm.pbf
# mod_tile - renders and serves map tiles using Apache and mapnik
# https://github.com/openstreetmap/mod_tile
sudo yum install httpd-devel # fix for: configure: error: Could not find apxs on the path.
git clone git://github.com/openstreetmap/mod_tile.git
cd mod_tile
./autogen.sh
./configure
# From where you cloned the mapnik source (see above) copy mapnik libraries to /usr/include/mapnik
sudo mkdir -p /usr/include/mapnik
sudo cp -rf ../mapnik/include/mapnik/* /usr/include/mapnik
sudo cp ../mapnik/include/mapnik/geometry/box2d.hpp /usr/include/mapnik
make
sudo make install
sudo make install-mod_tile
sudo ldconfig
cd ..
# --------------------
# Renderd and mod_tile
# --------------------
# /usr/local/etc/renderd.conf
sudo cat >/usr/local/etc/renderd.conf <<CMD_EOF
[renderd]
socketname=/var/run/renderd/renderd.sock
num_threads=2
tile_dir=/var/lib/mod_tile
stats_file=/var/run/renderd/renderd.stats
[mapnik]
plugins_dir=/usr/local/lib/mapnik/input
font_dir=/usr/local/lib/mapnik/fonts
font_dir_recurse=1
[default]
# @see OSM Carto Stylesheet section:
XML=/usr/local/src/osm_tile_server/openstreetmap-carto/mapnik.xml
HOST=127.0.0.1:8880
URI=/tile/
CMD_EOF
nano /etc/httpd/conf.d/mod_tile.conf
# TODO: source
sudo mkdir /var/run/renderd
sudo mkdir /var/lib/mod_tile
sudo chown apache /var/lib/mod_tile
sudo chown apache /var/run/renderd
# test foreground launch
sudo su -l apache -s /usr/local/bin/renderd -f -c /usr/local/etc/renderd.conf
# renderd.service
sudo cat >/lib/systemd/system/renderd.service <<CMD_EOF
[Unit]
Description=Mapnik rendering daemon
After=syslog.target
After=network.target
[Service]
User=apache
Environment="PATH=/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin"
ExecStart=/usr/local/bin/renderd -f -c /usr/local/etc/renderd.conf
#WorkingDirectory=
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
[Install]
WantedBy=multi-user.target
CMD_EOF
sudo systemctl enable renderd
sudo systemctl start renderd
tail -f /var/log/messages |grep renderd
# --------------------
# Apache2 service
# --------------------
systemctl start httpd.service
@coder4web
Copy link
Author

coder4web commented Jul 2, 2020

Hello!
Something with include files path, sorry, can't say in detail.
Script is complete and tested but results may vary.
CentOS 7/8 in not very friendly for OSM, so after spent time in struggle with compiling, I've switched to Docker and stay with it now: https://github.com/Overv/openstreetmap-tile-server.
Or try Ubuntu there are tutorilas for it: https://switch2osm.org/serving-tiles/

@casaulenbo
Copy link

casaulenbo commented Jul 2, 2020

hi Aleksey Deryagin

Thanks for your sharing, i follow your instruction, everything work fine until i try to compile mapnik with make . It's said:

In file included from benchmark/include/bench_framework.hpp:9:
deps/../test/cleanup.hpp:14:10: fatal error: 'cairo.h' file not found
#include <cairo.h>
         ^~~~~~~~~
1 error generated.
scons: *** [benchmark/src/normalize_angle.o] Error 1
scons: building terminated because of errors.
make: *** [Makefile:48: mapnik] Error 2

Have you got any idea about it?
Thanks!

After research for a couple days, i figure out that, somehow compile path does not correct, so and execute the following command before exec make: export CPATH=/root/osm_tile_server/mapnik/mason_packages/.link/include/cairo/:/root/osm_tile_server/mapnik/mason_packages/.link/include/freetype2/
Note: Please correct path with your enviroment.

But, it go to another issuse ;(

CentOS 7/8 in not very friendly for OSM, so after spent time in struggle with compiling, I've switched to Docker and stay with it now: https://github.com/Overv/openstreetmap-tile-server.
Or try Ubuntu there are tutorilas for it: https://switch2osm.org/serving-tiles/

You right. It's really hard to make it worked with CentOS.
Thanks for your help and have a nice day!

@azaadshatru
Copy link

Hi Aleksey,

Thanks for providing the steps for configuring the OSM on CentOS. While following the steps, I am getting error on running make command for mod_tile

[root@mumcnsmapsina2 mod_tile]# make
/bin/sh ./libtool --tag=CXX --mode=link g++ -I/usr/local/include -I/usr/local/include/mapnik/agg -I/usr/local/include/mapnik/deps -I/usr/local/include -I/usr/include -I/usr/include/freetype2 -I/usr/include/libpng15 -I/usr/include/gdal -I/usr/pgsql-9.5/include -I/usr/include/cairo -I/usr/include/glib-2.0 -I/usr/lib64/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/uuid -I/usr/include/libdrm -DMAPNIK_MEMORY_MAPPED_FILE -DMAPNIK_HAS_DLCFN -DBIGINT -DBOOST_REGEX_HAS_ICU -DHAVE_JPEG -DMAPNIK_USE_PROJ4 -DHAVE_PNG -DHAVE_TIFF -DLINUX -DMAPNIK_THREADSAFE -DBOOST_SPIRIT_NO_PREDEFINED_TERMINALS=1 -DBOOST_PHOENIX_NO_PREDEFINED_TERMINALS=1 -DBOOST_SPIRIT_USE_PHOENIX_V3=1 -DNDEBUG -DHAVE_CAIRO -DGRID_RENDERER -std=c++14 -DU_USING_ICU_NAMESPACE=0 -fvisibility=hidden -fvisibility-inlines-hidden -pthread -ftemplate-depth-300 -O3 -g -O2 -o renderd src/daemon.o src/daemon_compat.o src/renderd-gen_tile.o src/sys_utils.o src/request_queue.o src/cache_expire.o src/renderd-metatile.o src/renderd-parameterize_style.o src/protocol_helper.o src/store.o src/store_file.o src/store_file_utils.o src/store_memcached.o src/store_rados.o src/store_ro_http_proxy.o src/store_ro_composite.o src/store_null.o -pthread -L/usr/local/lib -lmapnik -L/opt/packages/mapnik/freetype -L/usr/local/lib -L/usr/lib -L/usr/lib64 -L/usr/pgsql-9.5/lib -pthread -lboost_filesystem -lboost_regex -lcairo -lpng -lproj -ltiff -licui18n -lboost_system -lharfbuzz -ljpeg -licuuc -lfreetype -lz -ldl -lcurl -liniparser -lm
libtool: link: g++ -I/usr/local/include -I/usr/local/include/mapnik/agg -I/usr/local/include/mapnik/deps -I/usr/local/include -I/usr/include -I/usr/include/freetype2 -I/usr/include/libpng15 -I/usr/include/gdal -I/usr/pgsql-9.5/include -I/usr/include/cairo -I/usr/include/glib-2.0 -I/usr/lib64/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/uuid -I/usr/include/libdrm -DMAPNIK_MEMORY_MAPPED_FILE -DMAPNIK_HAS_DLCFN -DBIGINT -DBOOST_REGEX_HAS_ICU -DHAVE_JPEG -DMAPNIK_USE_PROJ4 -DHAVE_PNG -DHAVE_TIFF -DLINUX -DMAPNIK_THREADSAFE -DBOOST_SPIRIT_NO_PREDEFINED_TERMINALS=1 -DBOOST_PHOENIX_NO_PREDEFINED_TERMINALS=1 -DBOOST_SPIRIT_USE_PHOENIX_V3=1 -DNDEBUG -DHAVE_CAIRO -DGRID_RENDERER -std=c++14 -DU_USING_ICU_NAMESPACE=0 -fvisibility=hidden -fvisibility-inlines-hidden -pthread -ftemplate-depth-300 -O3 -g -O2 -o renderd src/daemon.o src/daemon_compat.o src/renderd-gen_tile.o src/sys_utils.o src/request_queue.o src/cache_expire.o src/renderd-metatile.o src/renderd-parameterize_style.o src/protocol_helper.o src/store.o src/store_file.o src/store_file_utils.o src/store_memcached.o src/store_rados.o src/store_ro_http_proxy.o src/store_ro_composite.o src/store_null.o -pthread -pthread -L/usr/local/lib -lmapnik -L/opt/packages/mapnik/freetype -L/usr/lib -L/usr/lib64 -L/usr/pgsql-9.5/lib -lboost_filesystem -lboost_regex -lcairo -lpng -lproj -ltiff -licui18n -lboost_system -lharfbuzz -ljpeg -licuuc -lfreetype -lz -ldl -lcurl -liniparser -lm -pthread
/opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/ld: cannot find -liniparser
collect2: error: ld returned 1 exit status
make: *** [Makefile:758: renderd] Error 1

I am not able to search for this error and cannot proceed further. Can you please help if you know something about this error and how to deal with it?

Thanks in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment