shreeniraula.com.np | LinkedIn: ShreeNiraula | Twitter: @ShreeNiraula | YouTube: @ShreeNiraula
Welcome! This repository walks you through setting up Nominatim, a powerful geocoding tool that works with Open Street Map (OSM) data. This guide is specifically designed for Nepal, making it easy to set up and start using OSM data for geospatial services like location search, reverse geocoding, and more.
The setup includes everything you need, from installing dependencies to configuring PostgreSQL, setting up Nginx as a reverse proxy, and securing your server with HTTPS. Once set up, you'll have a fully functional Nominatim instance for your own geospatial needs.
Here are some key reasons why Nominatim is a great choice for your geocoding needs:
- Completely Free: Nominatim is open-source, meaning you don't have to pay any licensing fees to use it, and you can modify it to suit your needs.
- Accurate and Up-to-Date: It uses Open Street Map, which is constantly updated by contributors around the world, ensuring that the geographic data you’re working with is accurate and current.
- Highly Customizable: You can tweak the setup to focus on specific regions or change configurations to meet the needs of your application.
- Privacy-First: By hosting your own instance of Nominatim, you maintain complete control over your data, offering you peace of mind in terms of privacy.
- Built for Scale: Whether you're running a small app or handling a large number of requests, Nominatim is designed to scale easily, making it suitable for any project.
- Tootle: A popular Nepali ride-hailing app that uses geocoding for locating riders and drivers, optimizing travel routes, and managing pick-up points.
- Pathao Nepal: Used for ride services and deliveries, Pathao employs geocoding to find the user's current location and their destination for accurate route mapping.
- Hamro Patro: A Nepali app offering calendar, news, and other local services that might use location-based features for festivals, local events, and nearby places.
- Uber: Uses geocoding for ride request locations and destination searches.
- Airbnb: Relies on geocoding to help users search for accommodations by location.
- Foursquare: Uses location data for recommending nearby places to eat, shop, or visit.
If you're ready to set up your own Nominatim instance, just follow the steps in this repository. Here's a quick overview of what you'll be doing:
- Install Dependencies: We'll walk you through installing PostgreSQL, PostGIS, osm2pgsql, and the necessary Python packages.
- Set Up PostgreSQL: We'll create the necessary users and databases for Nominatim and configure PostgreSQL.
- Import OSM Data: We provide steps to download and import the latest Open Street Map data for Nepal into your Nominatim database.
- Configure the Server: Set up Nginx as a reverse proxy and secure the server with HTTPS.
- Run Nominatim: Finally, we guide you through running Nominatim as a Gunicorn application to make it available to your users.
Follow the full instructions in this repository, and you'll have a fully functional Nominatim setup in no time!
Before you begin, ensure your machine meets the following requirements:
- Debian-based operating system (e.g., Ubuntu): This guide assumes you're using a Debian-based distribution like Ubuntu. Other Linux distributions may require different commands or configurations.
- Elevated privileges (sudo access): You'll need administrator (root) access to install software, configure system settings, and manage users. Ensure you have
sudoprivileges on your machine to carry out these actions. - Set the A Record for the domain name (optional)
sudo vi /etc/hosts
127.0.0.1 osm.shreeniraula.com.np
First, update your package list and install the required dependencies:
sudo apt-get install -y osm2pgsql postgresql-postgis postgresql-postgis-scripts \
pkg-config libicu-dev virtualenv python3-pip aclsudo useradd -d /srv/nominatim -s /bin/bash -m nominatim
sudo mkdir -p /srv/nominatim
sudo chown -R nominatim:nominatim /srv/nominatimSwitch to the 'nominatim' user to continue with setup
sudo -u nominatim bash
cdSet environment variables for the 'nominatim' user
export USERNAME=nominatim
export USERHOME=/srv/nominatim
chmod a+x $USERHOMESwitch to Admin user and create PostgreSQL users for Nominatim and web service
sudo -u postgres createuser nominatim
sudo -u postgres createuser www-dataGrant 'nominatim' user permission to create databases in PostgreSQL
sudo -u postgres psql
ALTER USER nominatim WITH CREATEDB;
CREATE EXTENSION IF NOT EXISTS postgis; # Enable PostGIS extension
\du # List PostgreSQL roles
\q # Exit PostgreSQL promptSwitch back to nominatim user and create a Python virtual environment for Nominatim
virtualenv $USERHOME/nominatim-venvInstall the latest version of Nominatim using pip
$USERHOME/nominatim-venv/bin/pip install nominatim-dbActivate the virtual environment to run Nominatim commands
. $USERHOME/nominatim-venv/bin/activateCreate the Nominatim project directory and navigate into it, setting an environment variable for the directory path
mkdir ~/nominatim-project
cd ~/nominatim-project
export PROJECT_DIR=~/nominatim-project
cd $PROJECT_DIRDownload the OSM data for Nepal from Geofabrik
wget https://download.geofabrik.de/asia/nepal-latest.osm.pbfSwitch to admin user and grant 'nominatim' user superuser privileges temporarily to import data
sudo -u postgres psql
ALTER USER nominatim WITH SUPERUSER;
\qSwitch to 'nominatim' user and import the OSM data into the Nominatim database
nominatim import --osm-file nepal-latest.osm.pbf 2>&1 | tee setup.logVerify the Nominatim database setup
nominatim admin --check-databaseInstall Python dependencies for the Nominatim API to interact with the database
$USERHOME/nominatim-venv/bin/pip install psycopg[binary] falcon uvicorn gunicorn
$USERHOME/nominatim-venv/bin/pip install nominatim-apiTest the Nominatim installation by performing a search for Kathmandu
nominatim search --query KathmanduSwitch to 'admin' user revoke superuser privileges from the 'nominatim' user
sudo -u postgres psql
ALTER USER nominatim WITH NOSUPERUSER;
\qCreate a systemd service to run Nominatim as a Gunicorn application
sudo tee /etc/systemd/system/nominatim.service << EOFNOMINATIMSYSTEMD
[Unit]
Description=Nominatim running as a Gunicorn application
After=network.target
[Service]
Type=simple
User=www-data
Group=www-data
WorkingDirectory=/srv/nominatim/nominatim-project
Environment="PATH=/srv/nominatim/nominatim-venv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
ExecStart=/srv/nominatim/nominatim-venv/bin/gunicorn -b 0.0.0.0:8088 -w 4 -k uvicorn.workers.UvicornWorker nominatim_api.server.falcon.server:run_wsgi
ExecReload=/bin/kill -s HUP $MAINPID
StandardOutput=journal
StandardError=journal
PrivateTmp=true
TimeoutStopSec=5
KillMode=mixed
[Install]
WantedBy=multi-user.target
EOFNOMINATIMSYSTEMDReload systemd, enable and start the Nominatim service
sudo systemctl daemon-reload
sudo systemctl enable nominatim.service
sudo systemctl restart nominatim.serviceInstall Nginx web server
sudo apt-get install -y nginxConfigure Nginx to act as a reverse proxy for Nominatim by setting up a server block
sudo tee /etc/nginx/sites-available/osm.shreeniraula.com.np << EOF_NGINX_CONF
server {
listen 80; # Listen on port 80 for incoming HTTP requests
server_name osm.shreeniraula.com.np; # Specify the domain name for this server block
location / { # Define a location block to handle requests to the root URL
proxy_pass http://127.0.0.1:8088; # Forward requests to the Nominatim service running on localhost:8088
proxy_set_header Host \$host; # Pass the original host header to the proxied server
proxy_set_header X-Real-IP \$remote_addr; # Forward the real client IP address
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; # Pass along the client’s IP address in the chain
proxy_set_header X-Forwarded-Proto \$scheme; # Pass along the protocol (HTTP or HTTPS) of the original request
}
}
EOF_NGINX_CONF # Save the configuration to the Nginx sites-available directoryRemove the default symbolic link for the default Nginx configuration
sudo rm /etc/nginx/sites-enabled/defaultEnable the website configuration by creating a symbolic link
sudo ln -s /etc/nginx/sites-available/osm.shreeniraula.com.np /etc/nginx/sites-enabled/Test the Nginx configuration for any syntax errors
sudo nginx -tRestart Nginx to apply the changes
sudo systemctl restart nginxFix permission issues for Nominatim by ensuring the correct ownership
ls -ld /run
sudo chown -R www-data:www-data /srv/nominatimVerify the Gunicorn version for debugging purposes
sudo -u www-data /srv/nominatim/nominatim-venv/bin/gunicorn --versionManually run Gunicorn for troubleshooting if the service does not work
/srv/nominatim/nominatim-venv/bin/gunicorn -b 0.0.0.0:8088 -w 4 -k uvicorn.workers.UvicornWorker nominatim_api.server.falcon.server:run_wsgiCheck the Nominatim service logs for any errors or troubleshooting
sudo journalctl -u nominatim.serviceTest the functionality of the Nominatim Python frontend (API) by starting the server and performing a search query
# Manually start the Nominatim API server if it is not already running, to enable the frontend for search and geocoding requests
nominatim serve
# Test the search API with a query for Kathmandu and get the results in JSON format
curl http://127.0.0.1:8088/search?q=Kathmandu&format=jsonsudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d osm.shreeniraula.com.npQuery the Open Street Map API for the location 'Kathmandu' to get details in JSON format
curl https://osm.shreeniraula.com.np/search?q=Kathmandu&format=jsonPerform reverse geocoding by passing latitude and longitude to retrieve the address in Chandragiri Municipality, Kathmandu
curl https://osm.shreeniraula.com.np/reverse?lat=27.6892881&lon=85.2321257&format=json