Saturday, October 7, 2017

How to install Nominatim on CentOS 7



Nominatim Installation
Update date: 2017/03/29





Contents



1.    Các thuật ngữ

-         OSM : Open Street Map

2.    Giới thiệu

-         Link giới thiệu : http://wiki.openstreetmap.org/wiki/Nominatim
-         Đây là công cụ dùng truy vấn dữ liệu của OSM, do không có giới hạn về số request và làm việc trên database riêng nên nó phù hợp để thay thế công cụ Geocoder của Google Map cho ứng dụng định vị
-         Nominatim cung cấp 2 API quan trọng:
o   Tìm kiếm tọa độ từ địa chỉ
VD:
o   Tìm kiếm địa chỉ từ tọa độ
VD:

-         2 web service này là cơ sở khá quan trọng để xử lý vấn đề địa chỉ tồn đọng trong bài toán định vị của Việt Toàn Cầu

3.    Cài đặt Nominatim

3.1          Môi trường

-         Môi trường cài đặt chỉ dùng phần dữ liệu của Vietnam. Phần dữ liệu của toàn trái đất khá nặng và yêu cầu cao hơn về phần cứng
-         Việc cài đặt sử dụng VPS cho bản đồ Vietnam với môi trường phần cứng
o   CPU 4 core
o   Ram 4G
o   80G SSD
-         Hệ điều hành :
o   Centos 7.0.1406
o   Linux 3.10.0-123.4.2.el7.x86_64
o   Kiến trúc: x86-64

3.2          Cài các phần bổ trợ

3.2.1       Các thư viện

-         Các thư viện yêu cầu bởi Nominatim
-         yum install epel-release
-         yum install postgresql-server postgresql-contrib postgresql-devel postgis postgis-utils
-         yum install git make automake gcc gcc-c++ libtool policycoreutils-python
-         yum install php-pgsql php php-pear php-pear-DB libpqxx-devel proj-epsg
-         yum install bzip2-devel proj-devel geos-devel libxml2-devel boost-devel expat-devel zlib-devel
-         yum install bzip2
-         yum install wget
-         yum install php-fpm

3.2.2       Cài Java

-         Cài đặt:
sudo yum install java-1.7.0-openjdk

3.2.3       Cài Apache

-         Cài PHP 5 ( PHP 7 sẽ không chạy )
-         Cần thiết lập timezone cho php.ini (/etc/php.ini)
date.timezone = "Asia/Ho_Chi_Minh"
-         Cài đặt Apache:
o   sudo yum install httpd
-         Cấu hình httpd.cnf
-         Mở tường lửa cho Apache
o   sudo firewall-cmd --permanent --add-port=80/tcp
o   sudo firewall-cmd --permanent --add-port=443/tcp
o   sudo firewall-cmd –reload
-         Thiết lập service cho apache
o   sudo systemctl start httpd
o   sudo systemctl enable httpd
o   sudo systemctl status httpd

3.3          Cài đặt database

3.3.1       Cấu hình Postgres

-         Khi thực hiện 3.2.1 ta sẽ cài đặt sẵn Postgres bản 9.2. Ở phần này ta chỉ thực hiện cấu hình cần thiết
-         Thiết lập postgres chạy dạng service
o   postgresql-setup initdb
o   systemctl enable postgresql
o   systemctl start postgresql
-         Cấu hình Postgres file “/var/lib/pgsql/data/postgresql.conf”
o   Đây là các thông số được đề xuất
shared_buffers (2GB)
maintenance_work_mem (10GB)
work_mem (50MB)
effective_cache_size (24GB)
synchronous_commit = off
checkpoint_segments = 100 (only for PostgreSQL <= 9.4)
checkpoint_timeout = 10min
checkpoint_completion_target = 0.9
o   Tuy nhiên do hạn chế phần cứng RAM VPS ta cấu hình như sau
shared_buffers (2GB)
maintenance_work_mem (3GB)
work_mem (50MB)
effective_cache_size (3GB)
synchronous_commit = off
checkpoint_segments = 100 (only for PostgreSQL <= 9.4)
checkpoint_timeout = 10min
checkpoint_completion_target = 0.9
o   Cấu hình sau chỉ sử dụng khi import dữ liệu OSM sau khi import xong cần trả lại
fsync = off
full_page_writes = off
-         Khởi động lại postgres
systemctl restart postgresql

3.3.2       Tạo người dùng Postgres

-         Cần tạo ra các người dùng sau
o   nominatim
o   apache
-         Lệnh thực hiện ( reference create user : https://www.postgresql.org/docs/8.4/static/app-createuser.html )
o   sudo su - postgres
o   createuser -sdRe nominatim
o   createuser -sdRe root
o   createuser -SDRe apache

3.4          Cài Nominatim

3.4.1       Tạo người dùng OS

-         Cần tạo 1 tài khoản riêng ( không dùng root để cài đặt Nominatim )
-         Tạo người dùng ( dùng tài khoản root để tạo )
o   adduser nominatim
-         Thiết lập pass
o   passwd nominatim
-         Phân quyền sudo
o   usermod -aG wheel nominatim

3.4.2       Tải và build Nominatim

-         Dùng tài khoản root
cd /srv
wget http://www.nominatim.org/release/Nominatim-2.5.1.tar.bz2
tar xvf Nominatim-2.5.1.tar.bz2
mv Nominatim-2.5.1 nominatim
./configure
 make

3.4.3       Cấu hình Nominatim

-         Cấu hình file /srv/nominatim/settings/settings.php
@define('CONST_Database_Web_User', 'apache');
@define('CONST_Postgresql_Version', '9.2');
@define('CONST_Postgis_Version', '2.0');
@define('CONST_Website_BaseURL', 'http://localhost/nominatim/');
-         Cấu hình sau được dự đoán sẽ tăng performance của hệ thống bằng việc loại bỏ ghi log
@define('CONST_Log_DB', false);
@define('CONST_Log_File', false);

3.4.4       Tải dữ liệu OSM của Vietnam

-         Dùng tài khoản root thực hiện từ thư mục hiện hành /srv/Nominatim
wget --output-document=data/wikipedia_article.sql.bin http://www.nominatim.org/data/wikipedia_article.sql.bin
wget --output-document=data/wikipedia_redirect.sql.bin http://www.nominatim.org/data/wikipedia_redirect.sql.bin
wget --output-document=data/vi.osm.pbf http://download.geofabrik.de/asia/vietnam-latest.osm.pbf
-         Tạo thêm 1 số dữ liệu bổ sung
./utils/specialphrases.php --countries > specialphrases_countries.sql
./utils/specialphrases.php --wiki-import > specialphrases.sql

3.4.5       Thiết lập SELinux và 1 số quyền hạn

-         Dùng tài khoản root thiết lập selinux
semanage fcontext -a -t httpd_sys_content_t "/srv/nominatim/(website|lib|settings)(/.*)?"
semanage fcontext -a -t lib_t "/srv/nominatim/module/nominatim.so"
restorecon -R -v /srv/nominatim
-         Thiết lập quyền hạn dùng root
chown -R nominatim /srv/nominatim
sudo su nominatim
chmod -R 755 /srv/nominatim

3.4.6       Cài đặt Nominatim

-         Chuyển sang người dùng nominatim bằng lệnh sudo su nominatim
-         Cài đặt
mkdir -m 755 /var/www/html/nominatim
sudo chown nominatim /var/www/html/nominatim
cd /srv/nominatim
./utils/setup.php --create-website /var/www/html/nominatim

3.4.7       Cài dữ liệu OSM

-         Dùng tài khoản nominatim. Quá trình này với dữ liệu Vietnam có thể kéo dài khoản 1h, với dữ liệu lớn hơn có thể lâu hơn nên hay chú ý
-         Các lệnh thực hiện
sudo su nominatim
cd /srv/nominatim
./utils/setup.php --osm-file /srv/nominatim/data/vi.osm.pbf --all 2>&1 | tee setup.log
-         Trả lại cấu hình postgres “/var/lib/pgsql/data/postgresql.conf”
fsync = on
full_page_writes = on

3.4.8       Cấu hình apache hoàn thành cài đặt

-         Sửa file “/etc/httpd/conf.d/welcome.conf” xóa hết nội dung cũ chỉ để
<Directory "/var/www/html/nominatim/">
    Options FollowSymLinks MultiViews
    AddType text/html   .php
</Directory>
-         Khởi động lại Apache
systemctl restart httpd
-         Kiểm tra web: [link]/nominatim
-         Tạo robots.txt chặn search engine cho web này

4.    Cập nhật dữ liệu OSM

4.1          Cài osmosis

-         Dùng tài khoản root
cd /usr/local/bin
wget http://bretth.dev.openstreetmap.org/osmosis-build/osmosis-latest.tgz
mkdir osmosis
mv osmosis-latest.tgz osmosis
cd osmosis
tar xvfz osmosis-latest.tgz
rm osmosis-latest.tgz
chmod a+x bin/osmosis

4.2          Cấu hình Nominatim update dữ liệu vietnam

-         Cấu hình file /srv/nominatim/settings/settings.php
@define('CONST_Osmosis_Binary', '/usr/local/bin/osmosis/bin/osmosis');
@define('CONST_Replication_Url', 'http://download.geofabrik.de/asia/vietnam-updates/');
@define('CONST_Replication_MaxInterval', '40000');
@define('CONST_Replication_Update_Interval', '86400');
@define('CONST_Replication_Recheck_Interval', '900');

4.3          Cài đặt update

-         Chuyển sang người dùng nominatim và vào thư mục nominatim
sudo su nominatim
cd /srv/nominatim
-         Thực hiện
./utils/setup.php --osmosis-init
./utils/setup.php --create-functions --enable-diff-updates

4.4          Cập nhật OSM

-         Chuyển sang người dùng nominatim và vào thư mục nominatim
sudo su nominatim
cd /srv/nominatim
-         Cập nhật OSM
./utils/update.php --import-osmosis-all --no-npi



No comments:

Post a Comment

Sublime Text 3 Usage

1. Download and install sublime 2. Install some essential packages: - Seti.sublime-theme  : theme beautiful - HTML-CSS-JS Prettify (nee...