Unbutu 20搭建开源地图Nominatim服务
一、安装所需软件
sudo apt install -y php-cgi
sudo apt install -y build-essential cmake g++ libboost-dev libboost-system-dev \
libboost-filesystem-dev libexpat1-dev zlib1g-dev \
libbz2-dev libpq-dev \
postgresql-12-postgis-3 \
postgresql-contrib-12 postgresql-12-postgis-3-scripts \
php-cli php-pgsql php-intl libicu-dev python3-dotenv \
python3-psycopg2 python3-psutil python3-jinja2 \
python3-icu python3-datrie python3-yaml git
二、PostgreSQL配置
vi /etc/postgresql/10/main/postgresql.conf
// 此项⽬的数据存储对磁盘空间要求较⾼,迁移数据库数据⽂件所在⽬录到挂载盘 /mnt/data/postgresql/12/main,并更改如下默认配置
// data_directory = '/var/lib/postgresql/10/main'
// 在/mnt/data路径下创建postgresql文件,且对该文件赋予755权限
chown -R postgres:postgres /mnt/data/postgresql
chmod 755 -R /mnt/data/postgresql/
data_directory = '/mnt/data/postgresql/12/main'
// 新加如下配置提⾼数据库性能
shared_buffers = 2GB
maintenance_work_mem = 10GB
autovacuum_work_mem = 2GB
work_mem = 50MB
effective_cache_size = 24GB
synchronous_commit = off
max_wal_size = 1GB
checkpoint_timeout = 10min
checkpoint_completion_target = 0.9
autovacuum_max_workers = 1
// 以下两个配置,在更新完地图数据后配置上。
fsync = off
full_page_writes = off
// 重启postgreSQL
sudo systemctl restart postgresql
// 创建nominatim所需的数据库用户
sudo -u postgres createuser -s nominatim
sudo -u postgres createuser www-data
三、创建系统用户nominatim
sudo useradd -d /srv/nominatim -s /bin/bash -m nominatim
四、安装nominatim
// /mnt/data路径下创建nominatim文件夹
chown -R nominatim:nominatim /mnt/data/nominatim
chmod 755 -R /mnt/data/nominatim/
su - nominatim
// 也可以从官方安装最新版 :
// git clone --recursive https://github.com/openstreetmap/Nominatim.git
wget https://nominatim.org/release/Nominatim-3.7.2.tar.bz2
tar xf Nominatim-3.7.2.tar.bz2
cd Nominatim-3.7.2
mkdir build
cd build
cmake /mnt/data/nominatim/Nominatim-3.7.2
make
// 发现文件缺少postgres.h文件,执行安装命令:
sudo apt-get install postgresql-server-dev-12
su nominatim
sudo make install
// 执行下nominatim,检查是否安装成功
nominatim
五、导入地图
su - nominatim
// /mnt/data/nominatim路径下创建
mkdir nominatim-project
cd nominatim-project
cp /mnt/data/nominatim/Nominatim-3.7.2/settings/env.defaults ./.env
// If you plan to import a large dataset, Enable flatnode storage of node locations, add to .env
NOMINATIM_FLATNODE_FILE="/mnt/data/nominatim/Nominatim-3.7.2/to/flatnode.file"
// 下载地图
wget https://download.geofabrik.de/africa-latest.osm.pbf
// 注意,服务端不稳定容易挂,故需要创建个窗口
screen -S osm
screen -r osm
// 进入到nominatim-project目录下执行导入地图,如果有错或者需重新导入地图,请执行sudo -u postgres dropdb nominatim 删除sql表再执行导入地图命令
nohup nominatim import --osm-file /mnt/data/nominatim/nominatim-project/south-korea-latest.osm.pbf 2>&1 | tee setup.log
// verify all required tables and indices got created successfully.
nominatim admin --check-database
六、设置网络服务器
// 此刻官网有两种选择,这里选择选项2:使用nginx
// 确保 php7.4-fpm.sock 在 /var/run/ 下而不是 /var/run/php 下。否则将 Nginx 配置更改为 /var/run/php/php7.4-fpm.sock。
sudo tee /etc/php/7.4/fpm/pool.d/www.conf << EOF_PHP_FPM_CONF
[www]
; Replace the tcp listener and add the unix socket
listen = /var/run/php7.4-fpm.sock
; Ensure that the daemon runs as the correct user
listen.owner = www-data
listen.group = www-data
listen.mode = 0666
; Unix user of FPM processes
user = www-data
group = www-data
; Choose process manager type (static, dynamic, ondemand)
pm = ondemand
pm.max_children = 5
EOF_PHP_FPM_CONF
sudo tee /etc/nginx/sites-available/default << EOF_NGINX_CONF
server {
listen 80 default_server;
listen [::]:80 default_server;
root $USERHOME/nominatim-project/website;
index search.php index.html;
location / {
try_files \$uri \$uri/ @php;
}
location @php {
fastcgi_param SCRIPT_FILENAME "\$document_root\$uri.php";
fastcgi_param PATH_TRANSLATED "\$document_root\$uri.php";
fastcgi_param QUERY_STRING \$args;
fastcgi_pass unix:/var/run/php7.4-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f \$document_root\$fastcgi_script_name) {
return 404;
}
fastcgi_pass unix:/var/run/php7.4-fpm.sock;
fastcgi_index search.php;
include fastcgi.conf;
}
}
EOF_NGINX_CONF
// 启用配置并重启 Nginx
sudo systemctl restart php7.4-fpm nginx
// 访问nominatim服务是否成功
curl 'http://localhost/status.php'
curl 'http://localhost/search.php?q=xxx'
curl 'http://localhost/reverse?format=json&accept-language=en&lat=xxx&lon=xxx'
注意:服务器硬盘所需空间是否足够
链接:
[1]: https://github.com/osm-search/Nominatim
[2]: https://nominatim.org/release-docs/develop/appendix/Install-on-Ubuntu-20/