https://www.linuxbabe.com/ubuntu/set-up-tegola-vector-tile-server-ubuntu-20-04 LinuxBabe Read The Friendly Manual | Linux Sysadmin, Server & Desktop * Distros + Debian + Ubuntu + CentOS + openSUSE + Arch + Fedora * Sysadmin + Linux Server + Nginx + MariaDB + Mail Server + Backup + VPN Server + Server Monitoring + Security * Desktop Apps + Themes + Games + Multimedia + Soft phone + Cloud Storage + Instant Messenger + Finance * Self Hosted * Email Subscription * Buy me a beer * Community How to Set Up Tegola Vector Tile Server on Ubuntu 20.04 for OpenStreetMap Last Updated: July 27th, 2021 Xiao Guoan (Admin) 0 Comment Ubuntu Tegola is an open-source vector tile server for OpenStreetMap. Previously we explained the process of setting up OSM tile server, which is a raster-based tile server. This tutorial is going to show you how to set up Tegola vector tile server on Ubuntu 20.04. Benefits of Vector Tiles * Better display quality for high DPI devices (retina display) * Small efficient format (No 512 * 512 images needed) * Clearer, more readable text * On-the-fly labeling for heads up display * Separate content and styling, which allows for creating multiple styles pointing to the same tile stack. * Day and night mode Vector Tile Formats There are several formats for vector tiles. * GeoJSON * TopoJSON * Mapbox Vector Tile (MVT) * 05m * OpenScienceMap binary * Arc GeoServices JSON Tegola uses the Mapbox vector tile format. Prerequisites/Hardware Requirements The required RAM and disk space depend on which country's map you are going to use. For example, * The UK map requires at least 12G RAM and 100GB disk space. * The whole planet map requires at least 32G RAM and 1TB SSD (Solid State Drive). It's not viable to use a spinning hard disk for the whole planet map. It takes a long time to import large map data, like the whole planet, to PostgreSQL database. Consider adding more RAM and especially using SSD instead of spinning hard disk to speed up the import process. If you are going to host the entire world map, I recommend you buy the extra-large VPS from Contabo, which boasts * A 10 core CPU * 60 GB RAM * 1.6 TB Intel Optane SSD It costs just 26.99 EUR/month. Step 1: Upgrade Software It's always a good practice to update server software before doing any major work on your server. Log into your server via SSH and run the following command. sudo apt update; sudo apt upgrade Step 2: Install PostgreSQL Database Server and the PostGIS Extension We will use PostgreSQL to store map data. PostGIS is a geospatial extension to PostgreSQL. Run the following commands to install them. sudo apt install postgresql postgresql-contrib postgis postgresql-12-postgis-3 PostgreSQL database server will automatically start and listens on 127.0.0.1:5432. The postgres user will be created on the OS during the installation process. It's the super user for PostgreSQL database server. By default, this user has no password and there's no need to set one because you can use sudo to switch to the postgres user and log into PostgreSQL server. sudo -u postgres -i Now you can create a PostgreSQL database user osm. createuser osm Set a password for the osm user. psql -c "ALTER USER osm WITH PASSWORD 'secret_password';" Then create a database named osm and at the same time make osm as the owner of the database. -E UTF8 specifies the character encoding scheme to be used in the database is UTF8. createdb -E UTF8 -O osm osm Next, create the postgis and hstore extension for the osm database. psql -c "CREATE EXTENSION postgis;" -d osm psql -c "CREATE EXTENSION hstore;" -d osm Set osm as the table owner. psql -c "ALTER TABLE spatial_ref_sys OWNER TO osm;" -d osm Create a database named natural_earth and at the same time make osm as the owner of the database. createdb -E UTF8 -O osm natural_earth Next, create the postgis and hstore extension for the natural_earth database. psql -c "CREATE EXTENSION postgis;" -d natural_earth psql -c "CREATE EXTENSION hstore;" -d natural_earth Exit from the postgres user. exit Step 3: Optimize PostgreSQL Server Performance The import process can take some time. To speed up this process, we can tune some PostgreSQL server settings to improve performance. Edit PostgreSQL main configuration file. sudo nano /etc/postgresql/12/main/postgresql.conf First, we should change the value of shared_buffer. The default setting is: shared_buffers = 128MB This is too small. The rule of thumb is to set it to 25% of your total RAM (excluding swap space). For example, my VPS has 60G RAM, so I set it to: shared_buffers = 15GB Find the following line. #work_mem = 4MB #maintenance_work_mem = 64MB Again, the value is too small. I use the following settings. work_mem = 1GB maintenance_work_mem = 8GB Then find the following line. #effective_cache_size = 4GB If you have lots of RAM like I do, you can set a higher value for the effective_cache_size like 20G. effective_cache_size = 20GB Save and close the file. Restart PostgreSQL for the changes to take effect. sudo systemctl restart postgresql By default, PostgreSQL would try to use huge pages in RAM. However, Linux by default does not allocate huge pages. Check the process ID of PostgreSQL. sudo head -1 /var/lib/postgresql/12/main/postmaster.pid Sample output: 7031 Then check the VmPeak value of this process ID. grep ^VmPeak /proc/7031/status Sample output: VmPeak: 16282784 kB This is the peak memory size that will be used by PostgreSQL. Now check the size of huge page in Linux. cat /proc/meminfo | grep -i huge Sample output: AnonHugePages: 0 kB ShmemHugePages: 0 kB HugePages_Total: 0 HugePages_Free: 0 HugePages_Rsvd: 0 HugePages_Surp: 0 Hugepagesize: 2048 kB Hugetlb: 0 kB We can calculate how many huge pages we need. Divide the VmPeak value by the size of huge page: 16282784 kB / 2048 kB = 7950. Edit /etc/ sysctl.conf file. sudo nano /etc/sysctl.conf Add the following line to allocate 7950 huge pages. vm.nr_hugepages = 7950 Save and close the file. Then apply the changes. sudo sysctl -p If you check the meminfo again, cat /proc/meminfo | grep -i huge We can see there are 7950 huge pages available. AnonHugePages: 0 kB ShmemHugePages: 0 kB HugePages_Total: 7950 HugePages_Free: 7950 HugePages_Rsvd: 0 HugePages_Surp: 0 Hugepagesize: 2048 kB Restart PostgreSQL to use huge pages. sudo systemctl restart postgresql Use Screen on Remote Servers Since the import process can take a long time and your computer might be disconnected from Internet, it's recommended to use the screen utility to keep your session alive. Install screen on the Ubuntu 20.04 server: sudo apt install screen Then start screen: screen Upon first launch, you will see an introduction text, simply press Enter to end. Then you will be able to run commands as usual. Step 4: Import the Map Data to PostgreSQL To import map data, we will use imposm which converts OpenStreetMap data to postGIS-enabled PostgreSQL databases. Download it from Github. wget https://github.com/omniscale/imposm3/releases/download/v0.11.1/imposm-0.11.1-linux-x86-64.tar.gz Extract the archive. tar xvf imposm-0.11.1-linux-x86-64.tar.gz Move it to /opt/ directory. sudo mv imposm-0.11.1-linux-x86-64 /opt/imposm Download tegola-osm scripts. git clone https://github.com/go-spatial/tegola-osm.git Move it to /opt/ directory. sudo mv tegola-osm /opt/ Next, run the following command to download the map data of the whole planet (50G) in PBF (ProtoBufBinary) format. wget -c http://planet.openstreetmap.org/pbf/planet-latest.osm.pbf Note that download speeds for openstreetmap.org are currently restricted to 2048 KB/s. You can download the plant map from another mirror, like wget -c https://download.bbbike.org/osm/planet/planet-latest.osm.pbf If you want a map of individual country/state/province/city, go to http://download.geofabrik.de. Also, BBBike.org provides extracts of more than 200 cities and regions worldwide in different formats. For example, download the map data of Great Britain (1.1G) with the following command. wget -c http://download.geofabrik.de/europe/great-britain-latest.osm.pbf Run the following command to import map data. /opt/imposm/imposm import -connection postgis://osm:osm_password@localhost/osm -mapping /opt/tegola-osm/imposm3.json -read great-britain-latest.osm.pbf -write imposm import pbf to postgresql Now you probably don't need to do other things on your server. Since you are using Screen, you can press Ctrl+A, release those keys, and then press D key to detach from the current Screen session. You will see a message like below. [detached from 32113.pts-1.focal] This tells me that the previous Screen session ID is 32113. You can log out from the SSH session and even shut down your computer. Don't worry, the OSM import process is still running. When you need to come back and check the import progress, SSH into your server and run the following command to get the previous Screen Session ID. screen -ls Sample output: There is a screen on: 32113.pts-1.focal (05/19/2020 03:45:29 PM) (Detached) 1 Socket in /run/screen/S-linuxbabe. Then you can re-attach to the previous Screen session. screen -r 32113 And you will be able to continue your work. Once the map data has been imported, run the following command to deploy it for production. /opt/imposm/imposm import -connection postgis://osm:osm_password@localhost/osm -mapping /opt/tegola-osm/imposm3.json -deployproduction imposm delopy production Step 5: Download Tegola Go to the Tegola Github page and download the linux version. You can use the following command to download it in terminal. wget https://github.com/go-spatial/tegola/releases/download/v0.13.0/tegola_linux_amd64.zip Unzip it. sudo apt install unzip unzip tegola_linux_amd64.zip Move the binary to /usr/local/bin/ directory. sudo mv tegola /usr/local/bin/ Step 6: Import the OSM Land and Natural Earth dataset Edit the /opt/tegola-osm/osm_land.sh file. sudo nano /opt/tegola-osm/osm_land.sh Enter your database details. # database connection variables DB_NAME="osm" DB_HOST="localhost" DB_PORT="5432" DB_USER="osm" DB_PW="osm_password" Save and close the file. Install gdal. sudo apt install gdal-bin Generate relation land_polygons in the gis database. /opt/tegola-osm/osm_land.sh [osm-land-polygons] Next, edit the /opt/tegola-osm/natural_earth.sh file. sudo nano /opt/tegola-osm/natural_earth.sh Enter your database details. # database connection variables DB_NAME="natural_earth" DB_HOST="localhost" DB_PORT="5432" DB_USER="osm" DB_PW="osm_password" Save and close the file. Then generate tables in the natural_earth database. /opt/tegola-osm/natural_earth.sh Run the postgis_helpers SQL script. sudo -u postgres psql -d osm -a -f /opt/tegola-osm/postgis_helpers.sql Run the postgis_index.sql script to add indexes to OSM table columns to increase query performance. sudo -u postgres psql -d osm -a -f /opt/tegola-osm/postgis_index.sql Step 7: Start Tegola Edit the configuration file. sudo nano /opt/tegola-osm/tegola.toml Configure the listening port, cache type and data provider as follows. [webserver] port = ":8080" # Tegola offers three tile caching strategies: "file", "redis", and "s3" [cache] type = "file" basepath="/tmp/tegola-cache" # OpenStreetMap (OSM) [[providers]] name = "osm" type = "postgis" host = "127.0.0.1" port = "5432" database = "gis" user = "osm" password = "osm_password" # Natural Earth [[providers]] name = "ne" type = "postgis" host = "127.0.0.1" port = "5432" database = "natural_earth" user = "osm" password = "osm_password" Find the following line. center = [-76.275329586789, 39.153492567373, 8.0] # optional center value. part of the TileJSON spec You can set a custom center location (longitude and latitude) for your map and the default zoom level. Notice that you must use decimal values and can't use integer values. center = [0.8, 55.5, 5.0] # optional center value. part of the TileJSON spec Save and close the file. Then start Tegola. /usr/local/bin/tegola serve --config=/opt/tegola-osm/tegola.toml Now Tegola is listening on port 8080. Step 8: Create a Systemd Service for Tegola Tegola is running in the foreground. In order to run it in the background, we can create a systemd serivce, which also allow Tegola to automatically start at system boot time. Press Ctrl+C to stop the current Tegola process, then create the tegola.service file. sudo nano /etc/systemd/system/tegola.service Add the following lines to this file. [Unit] Description=Tegola Vector Tile Server [Service] Type=simple User=www-data ExecStart=/usr/local/bin/tegola serve --config=/opt/tegola-osm/tegola.toml Restart=on-failure RestartSec=5 [Install] WantedBy=multi-user.target Save and close the file. Make www-data as the owner of the /tmp/ tegola-cache/ directory. sudo chown www-data:www-data /tmp/tegola-cache/ -R Then enable and start this service. sudo systemctl enable tegola --now Check its status. Make sure it's running. systemctl status tegola tegola systemd service Then in your web browser address bar, type your-server-ip-address:8080 You should see the vector tile map. Congrats! You just successfully built your own vector tile server. Note that Firefox can't display these vector tiles. You need to use a third-party library to display vector tile based maps, which is explained at the end of this tutorial. tegola vector tile server Step 9: Setting Up Reverse Proxy To access the Tegola using a domain name, we can set up a reverse proxy for Tegola with Nginx or Apache. This will also allow us to enable HTTPS with free Let's Encrypt certificate. Nginx Nginx is a very popular web server and reverse proxy. If you prefer to use Nginx, run the following command to install it. sudo apt install nginx Then create a server block file for Tegola. sudo nano /etc/nginx/conf.d/tegola.conf Add the following content to this file. Replace tile.example.com with your own domain name. You should also create DNS A record for this sub-domain. If you don't have a real domain name, I recommend going to NameCheap to buy one. The price is low and they give whois privacy protection free for life. server { listen 80; listen [::]:80; server_name tile.example.com; access_log /var/log/nginx/tegola.access; error_log /var/log/nginx/tegola.error; location / { proxy_pass http://127.0.0.1:8080; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Protocol $scheme; proxy_set_header X-Forwarded-Host $http_host; } } Save and close this file. Then test Nginx configuration. sudo nginx -t If the test is successful, reload Nginx for the change to take effect. sudo systemctl reload nginx Now you can access Tegola via tile.example.com. Apache If you prefer Apache over Nginx, then install Apache web server by using the following command. sudo apt install apache2 To use Apache as a reverse proxy, we need to enable the proxy modules and the header module. sudo a2enmod proxy proxy_http headers Then create a virtual host file for Tegola. sudo nano /etc/apache2/sites-available/tegola.conf Put the following configurations into the file. Replace tile.example.com with your actual domain name. Don't forget to create DNS A record for this sub-domain. If you don't have a real domain name, I recommend going to NameCheap to buy one. The price is low and they give whois privacy protection free for life. ServerName tile.example.com ErrorDocument 404 /404.html #HTTP proxy ProxyPass / http://127.0.0.1:8080/ ProxyPassReverse / http://127.0.0.1:8080/ ProxyPreserveHost On Save and close the file. Then enable this virtual host. sudo a2ensite tegola.conf Restart Apache sudo systemctl restart apache2 Now you can access Tegola using the domain name tile.example.com. Step 10: Enable HTTPS To encrypt the HTTP traffic when you visit Tegola server from outside, we can enable HTTPS by installing a free TLS certificate issued from Let's Encrypt. Run the following command to install Let's Encrypt client (certbot) on Ubuntu 20.04. sudo apt install certbot If you use Nginx, then you also need to install the Certbot Nginx plugin. sudo apt install python3-certbot-nginx Next, run the following command to obtain and install TLS certificate. sudo certbot --nginx --agree-tos --redirect --hsts --staple-ocsp --email [email protected] -d tile.example.com If you use Apache, then you need to install the Certbot Apache plugin. sudo apt install python3-certbot-apache Next, run the following command to obtain and install TLS certificate. sudo certbot --apache --agree-tos --redirect --hsts --staple-ocsp --uir --email [email protected] -d tile.example.com Where: * --nginx: Use the nginx plugin. * --apache: Use the Apache plugin. * --agree-tos: Agree to terms of service. * --redirect: Force HTTPS by 301 redirect. * --hsts: Add the Strict-Transport-Security header to every HTTP response. Forcing browser to always use TLS for the domain. Defends against SSL/TLS Stripping. * --staple-ocsp: Enables OCSP Stapling. A valid OCSP response is stapled to the certificate that the server offers during TLS. * --uir: upgrade insecure requests. The certificate should now be obtained and automatically installed. And you can access Tegola via HTTPS: https://tile.example.com. set up tegola vector tile server on ubuntu 20.04 Set Up an Example Map You need to use a third-party library to display vector tile-based map. I use OpenLayer as an example. Create a map.html file on your server and put the following codes. OpenLayers example
Save and close the file. Here's how it looks. tegola vector tile server openlayer Conclusion I hope this article helped you set up Tegola Vector Tile Server on Ubuntu 20.04. As always, if you found this post useful, then subscribe to our free newsletter to get more tips and tricks. Take care Rate this tutorial [Total: 0 Average: 0] * OpenStreetMap Leave a Comment Cancel reply * Comments with links are moderated by admin before published. * Your email address will not be published. * Use
 ... 
HTML tag to quote the output from your terminal/console. * Please use the community (https://community.linuxbabe.com) for questions unrelated to this article. * I don't have time to answer every question. Making a donation would incentivize me to spend more time answering questions. [ ] [ ] [ ] [ ] [ ] [ ] [ ] Comment [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] Attachment [ ] The maximum upload file size: 2 MB. You can upload: image. Links to YouTube, Facebook, Twitter and other services inserted in the comment text will be automatically embedded. [*] Receive notification via e-mail when someone replies to my comment. [Post Comment] [ ] [Search] linux system security course lpic-2 pluralsight Featured Tutorials pCloud 65% Off Cloud Storage pcloud 2TB lifetime Follow us * facebook * twitter * email-alt * rss Claim Your $50 Hosting Credit [vultr-vps] * Recent Posts + Linux Server Performance Monitoring with Netdata + How to Fix Common Nginx Web Server Errors + How to Install Mega.nz Cloud Drive on Ubuntu - Free 20GB Storage + Set Up Unbound DNS Resolver on Ubuntu 20.04 Server + How to Install Syncthing on Linux Mint 20 * [ ] [Search] * Recent Ratings Vote 5 from anonymous on How to Easily Set Up a Full-Featured Mail Server on Ubuntu 18.04 with iRedMail Vote 5 from anonymous on Run Your Own Email Server on CentOS 8/ RHEL 8 - Postfix SMTP Server Vote 5 from anonymous on How to Quickly Set Up a Mail Server on Debian 10 Buster With Modoboa Vote 5 from anonymous on Integrate Collabora Online with Nextcloud on Ubuntu without Docker Vote 5 from anonymous on Linux Server Performance Monitoring with Netdata (c) LinuxBabe.Com | Read The Friendly Manual * Home * HTML Sitemap * Donation * About Me * Contact Me * Privacy Policy * Terms & Conditions * Disclaimer Subscribe to our list x [matomo]