在Centos7上安裝Nominatim

0.環境介紹

系統:CentOS 7.2php

1.準備工做

:tw-26a0: Never, ever run the installation as a root user.
經過root用戶將一個普通用戶joe加入sudoer,解決 xxx is not in the sudoers file。命令:html

visudo

加入紅框中的內容

Note: these installation instructions are also available in executable form for use with vagrant under vagrant/Install-on-Centos-7.sh
https://github.com/openstreetmap/Nominatim/blob/master/vagrant/Install-on-Centos-7.shnode

Hardware

A minimum of 2GB of RAM is required or installation will fail. For a full planet import 32GB of RAM or more strongly are recommended.python

For a full planet install you will need about 500GB of hard disk space (as of June 2016, take into account that the OSM database is growing fast). SSD disks will help considerably to speed up import and queries.linux

On a 6-core machine with 32GB RAM and SSDs the import of a full planet takes a bit more than 2 days. Without SSDs 7-8 days are more realistic.nginx

2.安裝必要的軟件

#更新軟件包
sudo yum update -y
#The standard CentOS repositories don't contain all the required packages,Enable the EPEL repository
sudo yum install -y epel-release
#安裝軟件
sudo yum install postgresql-server postgresql-contrib postgresql-devel postgis postgis-utils git cmake make gcc gcc-c++ libtool policycoreutils-python php-pgsql php php-pear php-pear-DB php-intl libpqxx-devel proj-epsg bzip2-devel proj-devel geos-devel libxml2-devel boost-devel expat-devel zlib-devel

For compiling:c++

For running Nominatim:git

  • PostgreSQL (9.1 or later)
  • PostGIS (2.0 or later)
  • PHP (5.4 or later)
  • PHP-pgsql
  • PHP-intl (bundled with PHP)
  • PEAR::DB
  • a webserver (apache or nginx are recommended)

:tw-26a0: Note that you must install php5, php7 does not work.github

For running continuous updates:web

查看軟件版本

#查看PHP版本
php -v
#查看PostgreSQL版本
psql --version
#查看PostGIS版本
rpm -qa | grep postgis

3.系統設置和配置

Nominatim will run as a global service on your machine. It is therefore best to install it under its own separate user account. In the following we assume this user is called nominatim and the installation will be in /srv/nominatim. To create the user and directory

#建立帳號並設置目錄
sudo useradd -d /srv/nominatim -s /bin/bash -m nominatim
export USERNAME=nominatim
export USERHOME=/srv/nominatim
#Make sure that system servers can read from the home directory:
chmod a+x $USERHOME

4.設置PostgreSQL數據庫

#初始化
sudo postgresql-setup initdb
sudo systemctl enable postgresql
#修改PostgreSQL默認配置
vim /var/lib/pgsql/data/postgresql.conf

添加或修改以下內容:

fsync = off
full_page_writes = off
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

:tw-26a0: Don't forget to reenable fsync full_page_writesafter the initial import or you risk database corruption. Autovacuum must not be switched off because it ensures that the tables are frequently analysed.

#重啓PostgreSQL數據庫
sudo systemctl restart postgresql

建立三個PostgreSQL數據庫用戶:
one for the user that does the import;
another for the webserver which should access the database only for reading:
another for the website user www-data as a PostgreSQL database role

sudo -u postgres createuser -s $USERNAME
sudo -u postgres createuser apache
sudo -u postgres createuser -SDR www-data

5.設置Apache Webserver

You need to create an alias to the website directory in your apache configuration. Add a separate nominatim configuration to your webserver:

sudo tee /etc/httpd/conf.d/nominatim.conf << EOFAPACHECONF
<Directory "$USERHOME/Nominatim/build/website">
  Options FollowSymLinks MultiViews
  AddType text/html   .php
  DirectoryIndex search.php
  Require all granted
</Directory>

Alias /nominatim $USERHOME/Nominatim/build/website
EOFAPACHECONF
#重啓Apache服務器
sudo systemctl restart httpd

6.Adding SELinux Security Settings

It is a good idea to leave SELinux enabled and enforcing, particularly with a web server accessible from the Internet. At a minimum the following SELinux labeling should be done for Nominatim:

sudo semanage fcontext -a -t httpd_sys_content_t "$USERHOME/Nominatim/(website|lib|settings)(/.*)?"
sudo semanage fcontext -a -t lib_t "$USERHOME/Nominatim/module/nominatim.so"
sudo restorecon -R -v $USERHOME/Nominatim

7.安裝Nominatim

#進入/srv/nominatim目錄,獲取源碼
cd $USERHOME
git clone --recursive git://github.com/openstreetmap/Nominatim.git
cd Nominatim
#When installing the latest source from github, you also need to download the country grid:
wget -O data/country_osm_grid.sql.gz http://www.nominatim.org/data/country_grid.sql.gz
#The code must be built in a separate directory.
mkdir build
cd build
cmake $USERHOME/Nominatim
make

create a minimal configuration file that tells nominatim the name of your webserver user and the URL of the website:

tee settings/local.php << EOF
<?php
 @define('CONST_Database_Web_User', 'apache');
 @define('CONST_Website_BaseURL', '/nominatim/');
EOF

8.導入 OSM data

If you plan to import a large dataset (e.g. Europe, North America, planet), you should also enable flatnode storage of node locations. With this setting enabled, node coordinates are stored in a simple file instead of the database.Add to your /srv/nominatim/Nominatim/build/settings/local.php

#由於我導入的地圖數據較小,未設置該項
@define('CONST_Osm2pgsql_Flatnode_File', '/srv/nominatim/Nominatim/data/flatnode.file');

:tw-26a0: Replace the second part with a suitable path on your system and make sure the directory exists. There should be at least 35GB of free space.

8.1下載輔助數據(備選項)

8.1.1 Wikipedia rankings

Wikipedia can be used as an optional auxiliary data source to help indicate the importance of osm features. Nominatim will work without this information but it will improve the quality of the results if this is installed.

cd /srv/nominatim/Nominatim/data
wget http://www.nominatim.org/data/wikipedia_article.sql.bin
wget http://www.nominatim.org/data/wikipedia_redirect.sql.bin

Combined the 2 files are around 1.5GB and add around 30GB to the install size of nominatim. They also increase the install time by an hour or so.

8.1.2 UK postcodes

Nominatim can use postcodes from an external source to improve searches that involve a UK postcode.

cd /srv/nominatim/Nominatim/data
wget http://www.nominatim.org/data/gb_postcode_data.sql.gz

8.2導入地圖數據

Important: first try the import with a small excerpt, for example from Geofabrik.
數據能夠從Geofabrik下載,建議下載.osm.pbf格式。

#下載中國的數據
cd /srv/nominatim/Nominatim/data
wget http://download.geofabrik.de/asia/china-latest.osm.pbf
#導入數據
cd /srv/nominatim/Nominatim/build
./utils/setup.php --osm-file ../data/china-latest.osm.pbf --all 2>&1 | tee setup.log
#

:tw-26a0: The --osm2pgsql-cache parameter is optional but strongly recommended for planet imports. It sets the node cache size for the osm2pgsql import part (see -C parameter in osm2pgsql help). 28GB are recommended for a full planet import, for excerpts you can use less. Adapt to your available RAM to avoid swapping, never give more than 2/3 of RAM to osm2pgsql.當導入的數據十分大時(好比超過1GB)建議使用如下命令:

./utils/setup.php --osm-file ../data/china-latest.osm.pbf --all [--osm2pgsql-cache 28000] 2>&1 | tee setup.log

完成導入數據大概須要1個小時!!!

8.3導入額外數據Loading additional datasets

The following commands will create additional entries for POI searches:

cd /srv/nominatim/Nominatim/build
./utils/specialphrases.php --wiki-import > specialphrases.sql
psql -d nominatim -f specialphrases.sql

9.保持數據是最新的Updates

There are many different possibilities to update your Nominatim database. The following section describes how to keep it up-to-date with Pyosmium. For a list of other methods see the output of

cd /srv/nominatim/Nominatim/build
./utils/update.php --help

9.1安裝Pyosmium

It is recommended to install Pyosmium via pip:

pip install --user osmium

Nominatim needs a tool called pyosmium-get-updates that comes with Pyosmium. You need to tell Nominatim where to find it. Add the following line to your build/settings/local.php:

@define('CONST_Pyosmium_Binary', '/home/user/.local/bin/pyosmium-get-changes');

The path above is fine if you used the --user parameter with pip. Replace user with your user name.

9.2設置更新時間

Next the update needs to be initialised. By default Nominatim is configured to update using the global minutely diffs.
If you want a different update source you will need to add some settings to build/settings/local.php. For example, to use the daily country extracts diffs for Ireland from geofabrik add the following:

// base URL of the replication service
@define('CONST_Replication_Url', 'http://download.geofabrik.de/europe/ireland-and-northern-ireland-updates');
// How often upstream publishes diffs
@define('CONST_Replication_Update_Interval', '86400');
// How long to sleep if no update found yet
@define('CONST_Replication_Recheck_Interval', '900');

To set up the update process now run the following command:

cd /srv/nominatim/Nominatim/build
./utils/update --init-updates

It outputs the date where updates will start. Recheck that this date is what you expect.
The --init-updates command needs to be rerun whenever the replication service is changed.

10.更新Nominatim

The following command will keep your database constantly up to date:

cd /srv/nominatim/Nominatim/build
./utils/update.php --import-osmosis-all

If you have imported multiple country extracts and want to keep them up-to-date, have a look at the script in issue #60.

11.參考資料

1.https://github.com/openstreetmap/Nominatim/blob/master/docs/Install-on-Centos-7.md
2.https://github.com/openstreetmap/Nominatim/blob/master/docs/Import-and-Update.md
3.https://github.com/openstreetmap/Nominatim/blob/master/docs/Installation.md

12.錯誤解決Troubleshooting

1.https://github.com/openstreetmap/Nominatim/blob/master/docs/Faq.md
2.http://wiki.openstreetmap.org/wiki/Nominatim/Installation/Troubleshooting
3.ERROR: permission denied for relation

參考

  1. https://stackoverflow.com/questions/13497352/error-permission-denied-for-relation-tablename-on-postgres-while-trying-a-selec
  2. https://stackoverflow.com/questions/15520361/permission-denied-for-relation/37389690
    個人解決方案:
#切換用戶
su - nominatim
進入PostgreSQL
psql
#使用nominatim數據庫
\c nominatim
#給apache用戶賦予權限
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO apache;

比較好的解決方案:(最後的操做:把用戶readonly改成apache,或者修改build/settings/local.phpCONST_Database_Web_User爲readonly)

CREATE USER readonly  WITH ENCRYPTED PASSWORD 'readonly';
GRANT USAGE ON SCHEMA public to readonly;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO readonly;
#在每一個數據庫上重複如下代碼,以nominatim數據庫爲例:
GRANT CONNECT ON DATABASE nominatim to readonly;
\c nominatim
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO readonly;
#this grants privileges on new tables generated in new database "nominatim"
GRANT USAGE ON SCHEMA public to readonly;
GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO readonly;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO readonly;

13.訪問Nominatim主頁

1.http://localhost/nominatim 2.逆地理編碼查詢 http://localhost/nominatim/reverse?format=xml&lat=31&lon=121&zoom=18&addressdetails=1

相關文章
相關標籤/搜索