[[サーバ関連]]
目次~
#contents

*概要 [#t4bfcc78]
Ubuntu 22を用いたタイルサーバの構築手順。~

**環境情報 [#v57cc40d]
|OS|Ubuntu 22.04.4 LTS|


*構築 [#g2dac86d]
**概要 [#z2682b98]
基礎情報はOSMのインストール手順を参考にすること。~

[[Manually building a tile server (Ubuntu 22.04):https://switch2osm.org/serving-tiles/manually-building-a-tile-server-ubuntu-22-04-lts/]]

**ユーザ作成 [#ma3948a0]
インストール作業を行うユーザのホームディレクトリ内に重要なファイルが配置されるため、専用のユーザを作成する。~

-ユーザ作成
 # adduser osm
 
 Adding user `osm' ...
 Adding new group `osm' (1001) ...
 Adding new user `osm' (1001) with group `osm' ...
 Creating home directory `/home/osm' ...
 Copying files from `/etc/skel' ...
 New password: 
 Retype new password: 
 passwd: password updated successfully
 Changing the user information for osm
 Enter the new value, or press ENTER for the default
         Full Name []: osm        
         Room Number []: 
         Work Phone []: 
         Home Phone []: 
         Other []: 
 Is the information correct? [Y/n] y

**アップデート [#g85548e3]
OSのアップデートを行い、最新化する。~

-アップデート
 $ sudo su - osm
 $ sudo apt update
 $ sudo apt upgrade

**パッケージインストール [#iaef07de]
使用するパッケージをインストールする。~

-インストール
 $ 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 fonts-noto-cjk fonts-noto-hinted fonts-noto-unhinted fonts-unifont fonts-hanazono postgresql postgresql-contrib postgis postgresql-14-postgis-3 postgresql-14-postgis-3-scripts osm2pgsql net-tools curl

**DB作成 [#uf14f2d2]
GIS情報を格納するデータベースを作成する。~

-DB作成
 $ sudo -u postgres -i
 $ createuser _renderd
 $ createdb -E UTF8 -O _renderd gis

-スキーマ作成
 $ psql
 psql (14.11 (Ubuntu 14.11-0ubuntu0.22.04.1))
 Type "help" for help.
 
 postgres=# \c gis
 You are now connected to database "gis" as user "postgres".
 
 gis=# CREATE EXTENSION postgis;
 CREATE EXTENSION
 
 gis=# CREATE EXTENSION hstore;
 CREATE EXTENSION
 
 gis=# ALTER TABLE geometry_columns OWNER TO _renderd;
 ALTER TABLE
 
 gis=# ALTER TABLE spatial_ref_sys OWNER TO _renderd;
 ALTER TABLE
 
 gis=# \q

-DBユーザからログアウト
 $ exit

**Mapnikインストール [#p64b2c2e]

-Mapnikインストール
 $ python3
 
 Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
 Type "help", "copyright", "credits" or "license" for more information.
 
 >>> import mapnik
 >>> quit()

**スタイルシート作成 [#hf671554]

-スタイルシート作成
 $ mkdir ~/src
 $ cd ~/src
 $ git clone https://github.com/gravitystorm/openstreetmap-carto
 
 Cloning into 'openstreetmap-carto'...
 remote: Enumerating objects: 18101, done.
 remote: Counting objects: 100% (256/256), done.
 remote: Compressing objects: 100% (164/164), done.
 remote: Total 18101 (delta 140), reused 187 (delta 88), pack-reused 17845
 Receiving objects: 100% (18101/18101), 17.23 MiB | 26.33 MiB/s, done.
 Resolving deltas: 100% (12514/12514), done.
 
 $ cd openstreetmap-carto
 $ sudo npm install -g carto

-バージョン確認
 $ carto -v
 
 1.2.0

-スタイルシートをMapnik用にコンバート
 $ carto project.mml > mapnik.xml

**GIS情報の取り込み [#l7bc8ce9]
日本地図を取り込む。~

-作業場所の作成
 $ mkdir ~/data

-GIS情報のダウンロード
 $ cd ~/data
 $ wget https://download.geofabrik.de/asia/japan-latest.osm.pbf
 $ chmod o+rx ~

日本以外の地図を取り込む際には、以下のURLから国名を指定してファイルを取得する。|

[[アジア圏のGIS情報:https://download.geofabrik.de/asia/]]

**GIS情報をDBへ書き込む [#x30776cb]
本処理は、システムのI/O性能と取り込む地図のサイズ次第で時間単位の処理待ちが発生する。~
システムの性能に合わせてパラメータを調整して実行する。~

-DBへの書き込み
 $ sudo -u _renderd osm2pgsql -d gis --create --slim -G --hstore --tag-transform-script ~/src/openstreetmap-carto/openstreetmap-carto.lua -C 14000 --number-processes 8 -S ~/src/openstreetmap-carto/openstreetmap-carto.style ~/data/japan-latest.osm.pbf 

-書式
 sudo -u _renderd osm2pgsql -d gis --create --slim -G --hstore --tag-transform-script ~/src/openstreetmap-carto/openstreetmap-carto.lua -C 【使用するメモリ量(MB)】 --number-processes 【処理するプロセッサ数】 -S ~/src/openstreetmap-carto/openstreetmap-carto.style 【取り込むGIS情報】 

***処理性能 [#d4ba8d25]
-参考:クラウドサービスのNFSボリュームをストレージとした際の処理速度
  Reading input files done in 2414s (40m 14s).                         
    Processed 256616513 nodes in 1005s (16m 45s) - 255k/s
    Processed 35702697 ways in 1103s (18m 23s) - 32k/s
    Processed 156150 relations in 306s (5m 6s) - 510/s
  osm2pgsql took 7948s (2h 12m 28s) overall.
~
-参考:仮想化基盤のSSDデータストアをストレージとした際の処理速度
  Reading input files done in 1961s (32m 41s).      
    Processed 228171421 nodes in 1077s (17m 57s) - 212k/s
    Processed 31090659 ways in 634s (10m 34s) - 49k/s
    Processed 116738 relations in 250s (4m 10s) - 467/s
  osm2pgsql took 4316s (1h 11m 56s) overall.

**indexの生成 [#ldc4048b]

-Indexを生成
 $ cd ~/src/openstreetmap-carto/
 $ sudo -u _renderd psql -d gis -f indexes.sql
 
 CREATE INDEX

**Shapefileの取得 [#i06a9c52]

-Shapefileのダウンロード
 $ cd ~/src/openstreetmap-carto/
 $ mkdir data
 $ sudo chown _renderd data
 $ sudo -u _renderd scripts/get-external-data.py
 
 INFO:root:Starting load of external data into database
 INFO:root:Checking table simplified_water_polygons
 INFO:root:  Download complete (23839309 bytes)
 INFO:root:  Decompressing file
 INFO:root:  Importing into database
 INFO:root:  Import complete
 〜以下略

**Fontの導入 [#p0b9c94b]

-Fontのダウンロード
 $cd ~/src/openstreetmap-carto/
 $ scripts/get-fonts.sh


**Web Server導入 [#ve5a330c]

***Web Server設定 [#z399cf80]
-設定
 $ sudo nano /etc/renderd.conf

-/etc/renderd.conf
 [s2o]
 URI=/hot/
 XML=/home/osm/src/openstreetmap-carto/mapnik.xml
 HOST=localhost
 TILESIZE=256
 MINZOOM=0 
 MAXZOOM=11

***renderdの設定 [#i071fef1]
-設定
 $sudo nano /usr/lib/systemd/system/renderd.service

-/usr/lib/systemd/system/renderd.service
 [Unit]
 Description=Daemon that renders map tiles using mapnik
 Documentation=man:renderd
 After=network.target auditd.service
 
 [Service]
 ExecStart=/usr/bin/renderd -f
 User=_renderd
 Environment=G_MESSAGES_DEBUG=all ←追記
 
 [Install]
 WantedBy=multi-user.target

***サービス再起動 [#bfe497b2]
 $ sudo systemctl daemon-reload
 $ sudo systemctl restart renderd
 $ sudo systemctl restart apache2

**コンテンツの配置 [#sa04a155]
地図生成を行うテストページを配置する。~

-コンテンツの配置
 cd /var/www/html
 sudo wget https://raw.githubusercontent.com/SomeoneElseOSM/mod_tile/switch2osm/extra/sample_leaflet.html
 sudo mv indev.html index-ubuntu.html
 sudo mv indev.html ubuntu.html
 sudo ln -s sample_leaflet.html index.html

-設定修正
 sudo nano sample_leaflet.html

-修正箇所(デフォルトの地図表示場所)
 var map = L.map('map').setView([35.680, 136.853], 5);

-修正箇所(アクセス先URL)
 L.tileLayer('http://【サーバアドレス】/hot/{z}/{x}/{y}.png', { 


**設定チューニング [#z351eebf]
***renderd [#e6f9be23]

-/etc/renderd.conf
 [s2o]
 URI=/hot/
 XML=/home/osm/src/openstreetmap-carto/mapnik.xml
 HOST=localhost
 TILESIZE=256
 MINZOOM=0
 MAXZOOM=11

-/etc/apache2/conf-available/renderd.conf
 Options Indexes FollowSymLinks MultiViews
 AllowOverride None
 Require all granted
 ModTileTileDir /var/cache/renderd/tiles
 LoadTileConfigFile /etc/renderd.conf
 ModTileEnableStats On
 ModTileBulkMode Off
 ModTileRequestTimeout 600
 ModTileMissingRequestTimeout 60
 ModTileMaxLoadOld 50
 ModTileMaxLoadMissing 80
 ModTileVeryOldThreshold 31536000000000
 ModTileRenderdSocketName /run/renderd/renderd.sock
 ModTileCacheExtendedDuration 2592000
 ModTileCacheDurationMax 604800
 ModTileCacheDurationDirty 900
 ModTileCacheDurationMinimum 10800
 ModTileCacheDurationMediumZoom 13 86400
 ModTileCacheDurationLowZoom 1 518400
 ModTileCacheLastModifiedFactor 0.20
 ModTileEnableTileThrottling Off
 ModTileEnableTileThrottlingXForward 0
 ModTileThrottlingTiles 10000 1
 ModTileThrottlingRenders 128 0.2

***PostgreSQL [#jeb26119]
-/etc/postgresql/14/main/postgresql.conf
 max_wal_size = 20GB
 min_wal_size = 80MB

***サービス再起動 [#qc9b99c4]
 $ sudo systemctl restart postgresql
 $ sudo systemctl restart renderd
 $ sudo systemctl restart apache2

**動作確認 [#n30aae58]
Webブラウザにてサーバのアドレスにアクセスを行う。~

 http://【サーバアドレス】/

地図が表示され、拡大/縮小によりタイルのレンダリングが行われる。



*操作 [#x983cdff]
**キャッシュのクリア [#we0bb033]
レンダリング済みのタイルを手動で削除するには以下の操作を行う。~
キャッシュは以下のパスに格納されている。~

 /var/cache/renderd/tiles/s2o


-手順(全て消す場合)
 cd /var/cache/renderd/tiles/s2o
 rm -rf *

-特定の倍率のみ消す(例では10)
 cd /var/cache/renderd/tiles/s2o
 rm -rf ./10

*設定 [#i5b3a9cd]
**ポート番号の変更 [#s15917b2]
デフォルト設定ではHTTP(80/tcp)にて動作するが、これをHTTP(tcp/8080)に変更する方法。~

***Aoache設定 [#g1f1710d]
Webサーバの待受ポートとしてtcp/8080を追加する。

-/etc/apache2/ports.conf
 【追記】Listen 8080

-/etc/apache2/sites-available/000-default.conf 
 【変更】<VirtualHost *:8080>

***Firewall設定 [#l3a8a40d]
Firewallにtcp/8080の受信を許可する。~

-8080ポートの開放
 # ufw allow 8080

***コンテンツ設定 [#nc253a94]
タイルサーバのコンテンツにて、tcp/8080で応答する様に変更する。~

 vi /var/www/html/sample_leaflet.html


-[変更前]
 L.tileLayer('http://[SERVER IP]/hot/{z}/{x}/{y}.png', {
     attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
     maxZoom: 21,
     maxNativeZoom: 20
 }).addTo(map);

-[変更後]
 L.tileLayer('http://[SERVER IP]:8080/hot/{z}/{x}/{y}.png', {
     attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
     maxZoom: 21,
     maxNativeZoom: 20
 }).addTo(map);








トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS