OpenStreetMap 服务搭建
2017 年 3 月 30 日 OpenStreetMap 服务搭建无评论
系统环境: ubuntu 16.04 64位
因为要在离线环境(局域网)下使用地图服务,所以需要搭建一个 OpenStreetMap 服务。
下载离线数据包
在 http://download.geofabrik.de/asia/china.html 下载离线数据包。
cd /var/www
wget http://download.geofabrik.de/asia/china-latest.osm.pbf
wget http://download.geofabrik.de/asia/china-latest.osm.pbf
安装基础环境
apt update -y
apt upgrade -y
apt-get install -y software-properties-common python-software-properties
apt-get install -y libboost-dev libboost-filesystem-dev libboost-program-options-dev libboost-python-dev libboost-regex-dev libboost-system-dev libboost-thread-dev
apt-get install -y subversion git-core tar unzip wget bzip2 build-essential autoconf libtool libxml2-dev libgeos-dev libpq-dev libbz2-dev munin-node munin libprotobuf-c0-dev protobuf-c-compiler libfreetype6-dev libpng12-dev libtiff5-dev libicu-dev libgdal-dev libcairo-dev libcairomm-1.0-dev apache2 apache2-dev libagg-dev liblua5.2-dev ttf-unifont
apt-get install -y autoconf apache2-dev libtool libxml2-dev libbz2-dev libgeos-dev libgeos++-dev libproj-dev gdal-bin libgdal1-dev mapnik-utils python-mapnik libmapnik-dev
apt upgrade -y
apt-get install -y software-properties-common python-software-properties
apt-get install -y libboost-dev libboost-filesystem-dev libboost-program-options-dev libboost-python-dev libboost-regex-dev libboost-system-dev libboost-thread-dev
apt-get install -y subversion git-core tar unzip wget bzip2 build-essential autoconf libtool libxml2-dev libgeos-dev libpq-dev libbz2-dev munin-node munin libprotobuf-c0-dev protobuf-c-compiler libfreetype6-dev libpng12-dev libtiff5-dev libicu-dev libgdal-dev libcairo-dev libcairomm-1.0-dev apache2 apache2-dev libagg-dev liblua5.2-dev ttf-unifont
apt-get install -y autoconf apache2-dev libtool libxml2-dev libbz2-dev libgeos-dev libgeos++-dev libproj-dev gdal-bin libgdal1-dev mapnik-utils python-mapnik libmapnik-dev
安装 postgresql
apt-get install -y postgresql-9.5-postgis-2.2 postgresql-contrib postgresql-server-dev-9.5
apt-get install -y osm2pgsql
apt-get install -y osm2pgsql
安装 mod_tile
mkdir ~/src
cd src/
git clone https://github.com/openstreetmap/mod_tile/
cd mod_tile
./autogen.sh
./configure
make
make install
make install-mod_tile
ldconfig
cd src/
git clone https://github.com/openstreetmap/mod_tile/
cd mod_tile
./autogen.sh
./configure
make
make install
make install-mod_tile
ldconfig
安装 Mapnik 样式表
cd /usr/local/src && svn co http://svn.openstreetmap.org/applications/rendering/mapnik mapnik-style
cd /usr/local/src/mapnik-style && ./get-coastlines.sh /usr/local/share
cd /usr/local/src/mapnik-style && ./get-coastlines.sh /usr/local/share
配置服务
cd /usr/local/src/mapnik-style/inc && cp fontset-settings.xml.inc.template fontset-settings.xml.inc
wget https://raw.githubusercontent.com/ncareol/osm-tiles-docker/master/datasource-settings.sed -O /tmp/datasource-settings.sed
cd /usr/local/src/mapnik-style/inc && sed --file /tmp/datasource-settings.sed datasource-settings.xml.inc.template > datasource-settings.xml.inc
wget https://raw.githubusercontent.com/ncareol/osm-tiles-docker/master/settings.sed -O /tmp/settings.sed
cd /usr/local/src/mapnik-style/inc && sed --file /tmp/settings.sed settings.xml.inc.template > settings.xml.inc
wget https://raw.githubusercontent.com/ncareol/osm-tiles-docker/master/renderd.conf.sed -O /tmp/renderd.conf.sed
cd /usr/local/etc && sed --file /tmp/renderd.conf.sed --in-place renderd.conf
mkdir /var/run/renderd && chown www-data: /var/run/renderd
mkdir /var/lib/mod_tile && chown www-data /var/lib/mod_tile
wget https://raw.githubusercontent.com/ncareol/osm-tiles-docker/master/index.html -O /var/www/html/index.html
wget https://raw.githubusercontent.com/ncareol/osm-tiles-docker/master/mod_tile.load -O /etc/apache2/mods-available/mod_tile.load
wget https://raw.githubusercontent.com/ncareol/osm-tiles-docker/master/mod_tile.conf -O /etc/apache2/mods-available/mod_tile.conf
a2enmod mod_tile
sed -i -e 's/local all all peer/local gis www-data peer/' /etc/postgresql/9.5/main/pg_hba.conf
wget https://raw.githubusercontent.com/ncareol/osm-tiles-docker/master/postgresql.conf.sed -O /tmp/postgresql.conf.sed
sed --file /tmp/postgresql.conf.sed --in-place /etc/postgresql/9.5/main/postgresql.conf
wget https://gist.githubusercontent.com/xdtianyu/ea9a57f4824eede676e6036c299f04e0/raw/renderd.service -O /etc/systemd/system/renderd.service
wget https://raw.githubusercontent.com/ncareol/osm-tiles-docker/master/datasource-settings.sed -O /tmp/datasource-settings.sed
cd /usr/local/src/mapnik-style/inc && sed --file /tmp/datasource-settings.sed datasource-settings.xml.inc.template > datasource-settings.xml.inc
wget https://raw.githubusercontent.com/ncareol/osm-tiles-docker/master/settings.sed -O /tmp/settings.sed
cd /usr/local/src/mapnik-style/inc && sed --file /tmp/settings.sed settings.xml.inc.template > settings.xml.inc
wget https://raw.githubusercontent.com/ncareol/osm-tiles-docker/master/renderd.conf.sed -O /tmp/renderd.conf.sed
cd /usr/local/etc && sed --file /tmp/renderd.conf.sed --in-place renderd.conf
mkdir /var/run/renderd && chown www-data: /var/run/renderd
mkdir /var/lib/mod_tile && chown www-data /var/lib/mod_tile
wget https://raw.githubusercontent.com/ncareol/osm-tiles-docker/master/index.html -O /var/www/html/index.html
wget https://raw.githubusercontent.com/ncareol/osm-tiles-docker/master/mod_tile.load -O /etc/apache2/mods-available/mod_tile.load
wget https://raw.githubusercontent.com/ncareol/osm-tiles-docker/master/mod_tile.conf -O /etc/apache2/mods-available/mod_tile.conf
a2enmod mod_tile
sed -i -e 's/local all all peer/local gis www-data peer/' /etc/postgresql/9.5/main/pg_hba.conf
wget https://raw.githubusercontent.com/ncareol/osm-tiles-docker/master/postgresql.conf.sed -O /tmp/postgresql.conf.sed
sed --file /tmp/postgresql.conf.sed --in-place /etc/postgresql/9.5/main/postgresql.conf
wget https://gist.githubusercontent.com/xdtianyu/ea9a57f4824eede676e6036c299f04e0/raw/renderd.service -O /etc/systemd/system/renderd.service
初始化数据库
chown -R postgres /var/lib/postgresql/
sudo -u postgres -i /usr/lib/postgresql/9.5/bin/initdb --pgdata /var/lib/postgresql/9.5/main
ln -s /etc/ssl/certs/ssl-cert-snakeoil.pem /var/lib/postgresql/9.5/main/server.crt
ln -s /etc/ssl/private/ssl-cert-snakeoil.key /var/lib/postgresql/9.5/main/server.key
sudo -u postgres -i /usr/lib/postgresql/9.5/bin/initdb --pgdata /var/lib/postgresql/9.5/main
ln -s /etc/ssl/certs/ssl-cert-snakeoil.pem /var/lib/postgresql/9.5/main/server.crt
ln -s /etc/ssl/private/ssl-cert-snakeoil.key /var/lib/postgresql/9.5/main/server.key
systemctl start postgresql
sudo -H -u postgres bash -c "createuser -s www-data"
dbname=gis
sudo -H -u postgres bash -c "createdb -O www-data $dbname"
sudo -H -u www-data bash -c "psql -d $dbname -f /usr/share/postgresql/9.5/contrib/postgis-2.2/postgis.sql"
sudo -H -u www-data bash -c "psql -d $dbname -c 'CREATE EXTENSION HSTORE;'"
sudo -H -u www-data bash -c "psql -d $dbname -c 'ALTER TABLE geometry_columns OWNER TO "www-data"; ALTER TABLE spatial_ref_sys OWNER TO "www-data";'"
sudo -H -u www-data bash -c "psql -d $dbname -f /usr/share/postgresql/9.5/contrib/postgis-2.2/spatial_ref_sys.sql"
dbname=gis
sudo -H -u postgres bash -c "createdb -O www-data $dbname"
sudo -H -u www-data bash -c "psql -d $dbname -f /usr/share/postgresql/9.5/contrib/postgis-2.2/postgis.sql"
sudo -H -u www-data bash -c "psql -d $dbname -c 'CREATE EXTENSION HSTORE;'"
sudo -H -u www-data bash -c "psql -d $dbname -c 'ALTER TABLE geometry_columns OWNER TO "www-data"; ALTER TABLE spatial_ref_sys OWNER TO "www-data";'"
sudo -H -u www-data bash -c "psql -d $dbname -f /usr/share/postgresql/9.5/contrib/postgis-2.2/spatial_ref_sys.sql"
导入数据库
注意这个过程可能需要至少20分钟时间,产生的数据库约占 8G 空间。
sudo -H -u www-data bash -c "osm2pgsql --slim --hstore --cache 2000 --database gis --number-processes 8 /var/www/china-latest.osm.pbf"
修改 `/usr/local/etc/renderd.conf` 配置
[renderd]
socketname=/var/run/renderd/renderd.sock
num_threads=4
tile_dir=/var/lib/mod_tile
stats_file=/var/run/renderd/renderd.stats
[mapnik]
plugins_dir=/usr/lib/mapnik/3.0/input
font_dir=/usr/share/fonts
font_dir_recurse=true
[default]
font_dir=/usr/share/fonts
font_dir_recurse=true
URI=/osm_tiles/
TILEDIR=/var/lib/mod_tile
XML=/opt/carto/style.xml
HOST=localhost
TILESIZE=256
socketname=/var/run/renderd/renderd.sock
num_threads=4
tile_dir=/var/lib/mod_tile
stats_file=/var/run/renderd/renderd.stats
[mapnik]
plugins_dir=/usr/lib/mapnik/3.0/input
font_dir=/usr/share/fonts
font_dir_recurse=true
[default]
font_dir=/usr/share/fonts
font_dir_recurse=true
URI=/osm_tiles/
TILEDIR=/var/lib/mod_tile
XML=/opt/carto/style.xml
HOST=localhost
TILESIZE=256
启动服务
添加
/etc/systemd/system/renderd.service
文件,内容如下
[Service]
ExecStart=/usr/local/bin/renderd --config /usr/local/etc/renderd.conf --foreground yes
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=renderd
User=www-data
Group=www-data
Environment=SYSTEM_FONTS=/usr/share/fonts
[Install]
WantedBy=multi-user.target
ExecStart=/usr/local/bin/renderd --config /usr/local/etc/renderd.conf --foreground yes
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=renderd
User=www-data
Group=www-data
Environment=SYSTEM_FONTS=/usr/share/fonts
[Install]
WantedBy=multi-user.target
systemctl restart renderd
systemctl restart apache2
systemctl restart apache2
可以使用如下命令测试 renderd 服务
sudo -H -u www-data bash -c "export SYSTEM_FONTS=/usr/share/fonts && /usr/local/bin/renderd --config /usr/local/etc/renderd.conf --foreground yes"
字体问题
apt install fonts-wqy-microhei fonts-noto-cjk fonts-noto-hinted fonts-noto-unhinted ttf-unifont
ls /usr/share/fonts/truetype/
export SYSTEM_FONTS=/usr/share/fonts
ls /usr/share/fonts/truetype/
export SYSTEM_FONTS=/usr/share/fonts
重启服务
预加载数据
注意这个过程可能需要几个或几十个小时,产生的文件可能会占用 10G-30G 空间。
sudo -H -u www-data bash -c "export SYSTEM_FONTS=/usr/share/fonts && render_list --all --min-zoom 1 --max-zoom 18 --num-threads 4"
参考 https://github.com/ncareol/osm-tiles-docker
————–
精简安装
安装编译依赖
apt install -y autoconf libtool build-essential apache2-dev libmapnik-dev
安装 mod_tile
mkdir ~/src
cd src/
git clone https://github.com/openstreetmap/mod_tile/
cd mod_tile
./autogen.sh
./configure
make
export DESTDIR="$HOME/out" && make install
export DESTDIR="$HOME/out" && make install-mod_tile
ldconfig
cd src/
git clone https://github.com/openstreetmap/mod_tile/
cd mod_tile
./autogen.sh
./configure
make
export DESTDIR="$HOME/out" && make install
export DESTDIR="$HOME/out" && make install-mod_tile
ldconfig
打包生成的文件
tar czf out.tar.gz "$HOME/out"
部署
安装部署依赖
apt install libmapnik3.0
将生成的 out.tar.gz 文件复制到目标服务器
tar xf out.tar.gz
cd out
cp -r usr /
ldconfig
cd out
cp -r usr /
ldconfig
运行
renderd --help
测试二进制执行环境是否正常。
————–
整理于 2017-02-16
发表评论