Installing OpenStreetMap Nominatim to find latitude and longitude at the entered address

I would like to tell my story about installing the Nominatim geocoder on a dedicated server. Initially, it was assumed that this task would take me about 5-7 hours, but it wasn’t here ... Therefore, it was decided to write an article describing the deployment of Nominatim to the server until the site is fully operational. But first things first.
Introduction
There are many services with geocoders, I won’t list them all here, I’ll note that there are good articles on Habré on this subject: “Geocoding with PHP and the Google Maps API” , as well as “Yandex Maps: Search for arbitrary objects” .
For those who are not in the know: Geocoding is the process of finding latitude and longitude at the entered address.
My choice fell on Nominatim, since it can be deployed on your server and not be limited by the number of requests, I also had experience with OSM cards and would like to apply it.
Documentation
A site with an already working map can be found at: OpenStreetMap Nominatim
And here is a link to sample requests: Wiki nominatim
There is also a link to install Nominatim Installation . But this documentation is a little outdated, for installation on this particular article you need to dance a little with a tambourine. Everything described below will refer to this installation article, only without the participation of a tambourine.
And also there is a docker container nominatim docker . For some reason, most likely due to outdated installation packages, this container did not start with me.
Step 1: Create a Virtual Machine
It was based on a machine from the cloud of Microsoft Azure standart D2 series with characteristics: 2 cores, 7 GB RAM, 100 GB SSD. It is on this machine that Nominatim will be installed. 100GB is actually more than necessary for my task, since the map of Ukraine is not so big. After installing all the necessary components, 70% remains free.
Step 2: Installing the Required Packages
sudo apt-get update
sudo apt-get -y install wget
sudo apt-get -y install build-essential automake
sudo apt-get -y install libxml2-dev
sudo apt-get -y install libgeos-dev
sudo apt-get -y install libpq-dev
sudo apt-get -y install libbz2-dev
sudo apt-get -y install libtool libproj-dev
sudo apt-get -y install libgeos++-dev
sudo apt-get -y install gcc proj-bin libgeos-c1 git osmosis
sudo apt-get -y install php5 php-pear php5-pgsql php5-json
sudo apt-get -y install bc
sudo apt-get -y install postgresql-9.4 postgresql-9.4-postgis-2.1 postgresql-contrib-9.4 postgresql-server-dev-9.4
sudo apt-get -y install libboost-chrono1.55-dev libboost-thread1.55-dev libboost-filesystem1.55-dev
sudo apt-get -y install python-software-properties && add-apt-repository -y ppa:kakrueger/openstreetmap && apt-get update && apt-get --no-install-recommends install -y osm2pgsql
Step 3: Setup
sudo -i
pear install DB
sudo -u username
sudo mkdir -p /app/nominatim
sudo -i
useradd -m -p password1234 -d /app/nominatim nominatim
chown nominatim: /app/nominatim
cd /app/nominatim
wget http://www.nominatim.org/release/Nominatim-2.4.0.tar.bz2
tar xvf Nominatim-2.4.0.tar.bz2
rm Nominatim-2.4.0.tar.bz2
mv Nominatim-2.4.0/* .
rm Nominatim-2.4.0/
sudo -u nominatim ./autogen.sh
./configure && make
I think these lines of commands are not difficult to understand. If an error occurs with obtaining access rights, set the read and execute access rights to all users and groups chmod -R 755 / app . I would like to note that sometimes there was a problem when running the make command . if you have this problem, use the sudo make clean command , and then ./configure && make .
Step 4: PostgreSQL
Initially, postgres was not configured for a combat server, so you need to configure it here are the tips from the nominatim documentation wiki:
Location /etc/postgresql/9.x/main/postgresql.conf Ubuntu
CentOS location /var/lib/pgsql/data/postgresql.conf
shared_buffers (4GB)
maintenance_work_mem (10GB)
work_mem (50MB)
effective_cache_size (24GB)
synchronous_commit = off
checkpoint_segments = 100
checkpoint_timeout = 10min
checkpoint_completion_target = 0.9
The numbers in brackets behind some parameters seem to work fine for 32GB RAM machine
sudo passwd postgres
sudo usermod -a -G sudo postgres
service postgresql start && pg_dropcluster --stop 9.4 main
service postgresql start && pg_createcluster --start -e UTF-8 9.4 main
service postgresql start && sudo -u postgres psql postgres -tAc "SELECT 1 FROM pg_roles WHERE rolname='nominatim'" | grep -q 1 || sudo -u postgres createuser -s nominatim && sudo -u postgres psql postgres -tAc "SELECT 1 FROM pg_roles WHERE rolname='www-data'" | grep -q 1 || sudo -u postgres createuser -SDR www-data && sudo -u postgres psql postgres -c "DROP DATABASE IF EXISTS nominatim"
This is the easiest part of the installation. And now hardcore begins, because further actions are not always performed correctly. And after such an incorrect completion, unfortunately, sometimes you have to perform actions anew on a new virtual machine.
Step 5: Download and install osm cards
First you need to download the map itself from the site in * .pbf format: OpenStreetMap Data Extracts . As an example, I download a map of Ukraine and rename it to data.pbf.
wget --output-document=data.pbf http://download.geofabrik.de/europe/ukraine-latest.osm.pbf
chown nominatim: data.pbf
touch local.php /app/nominatim/settings/local.php
nano /app/nominatim/settings/local.php
The last command opens the configuration file for installing nominatim. In the local.php file, you need to enter the code described below.
As BaseURL, you must enter the address of the site from which nominatim will be launched.
Now run the nominatim installation command. This operation takes quite a long time, for example, with a map of Ukraine, the installation lasted about 7 hours. Due to the fact that I executed all the commands through ssh, and am prone to variable Internet disconnection, the installation command is executed in the screen .
screen
service postgresql start && sudo -u nominatim -- ./utils/setup.php --osm-file /app/nominatim/data.pbf --all --threads 2 2>&1; sudo -u nominatim -- ./utils/setup.php --index --create-search-indices
To exit the screen you need to type the combination Ctrl + A then Ctrl + D. The screen -r command returns to the screen.
Now start the site in a quick way:
./utils/setup.php --create-website /var/www/html
rm /var/www/html/index.html
/etc/init.d/apache2 restart
I managed without configuring apache or working with nginx, since you can easily read about it on Habr in other articles. Yes, and without setting up the site works great.
Testing the site
As a result, we should see something like this on my screen:

Let's check how the api works by making a request:
http://geocoder.cloudapp.net/?format=json&addressdetails=1&q=Odessa&format=json&limit=1
Response:
[{"place_id":"1145869","licence":"Data © OpenStreetMap contributors, ODbL 1.0. http:\/\/www.openstreetmap.org\/copyright","osm_type":"relation","osm_id":"1413934","boundingbox":["46.342707","46.6291187","30.6114013","30.8313753"],"lat":"46.4858883","lon":"30.68365101101","display_name":"Одесса, Одесская область, Украина","class":"place","type":"city","importance":0.45,"icon":"http:\/\/geocoder.cloudapp.net\/images\/mapicons\/poi_place_city.p.20.png","address":{"city":"Одесса","county":"Одесса","state":"Одесская область","country":"Украина","country_code":"ua"}}]
conclusions
In the end, I would like to note that to download the Earth map you need a server more powerful than the D2 series if you do not want to * wait a week for the download. This problem can also be solved by temporarily scaling the server to the D14 series (16 cores, 112 GB of memory). But the time it takes to complete a search request is: on average, a search in Ukraine takes only 300ms.
I hope this article helps other developers spend less time deploying Nominatim and see if you need it. Maybe you should take it ready and use it?
If someone from the habrosociety has a need for a working Docker container with Nominatim, or other ideas - write, I’m always glad to talk.