- PostGIS: A PostgreSQL database optimize to store spatial information. It can easily import shapefiles and OSM data using command line tools as well as connect to mapping services like QGIS and Mapnik.
- Mapnik: A very powerful tool for automatically generating maps from geographic data with lots of control over cartographic display and rendering.
- TileStache: A simple way to efficiently serve geographic data to mapping applications. It can send tiled vector or raster data and will speed up any application that needs to load lots of data.
Basic SetupI’m going to be using a Rackspace Cloud Server for this example. It’s a cheap way to get a server up and running and I’ve found them to be great with support. They automatically build your server and install the operating system. You just need to select 3 things:
- Operating System: Ubuntu 11.10 (Oneric Ocelot)
- Server Name: tiles
- Server Size: 1024 MB of RAM and 40GB of disk
Terminal and Remote AccessSince this server is in the cloud (ooooooh), the only way to access it is remotely through SSH. Open any SSH client you like (Terminal is already installed on OSX) and get comfortable. First thing we need to do is to log on to our remote server. Make sure you have that email with your root password and IP address and type into the terminal window:
ssh root@(For future reference, don’t type any of the code sections in bold and italics surrounded by < >. Replace your stuff there.) That command tells the terminal to start an SSH session, logging in as root to the server at the specified IP address. The root username is the default admin of the server. We’ll do most of this setup as root since it has full control over the system. When it asks, just paste your root password from the email and you should be logged in and should see something like this:
root@tiles:~#The # tells you the system is ready to receive commands. Let’s now change the root password into something we’ll remember. Type:
passwdand hit enter and follow the prompts. Now we want to do a quick software update to make sure everything is secure. Run both of these commands:
sudo aptitude update
sudo aptitude upgradeWe’ll use the sudo command often. It tells the server to perform the task as a super-user which gives you extra permissions to modify the system. Here’s another top-tip: At the command prompt, you can hit the up arrow on your keyboard to cycle through your previous commands.
Installing the Web Server BitsThis part of the tutorial is taken from symana.labs The next step is to install the LAMP stack. LAMP stands for (Linux, Apache, MySQL, PHP) and has all the basics to turn your server into a web server. It can be installed with a single command:
sudo tasksel install lamp-serverand follow the prompts. To secure the MySQL server from common database attacks, run:
mysql_secure_installationEnter the password you set in the previous step and then enter “n” to keep it. Enter “Y” for the rest of the questions. To configure a fully qualified domain name for apache type:
echo "ServerName localhost" | sudo tee /etc/apache2/conf.d/fqdnIf you think you are going to be using MySQL for other applications on the server, you can install phpmyadmin to give you a graphical way to interact with the DB by running:
sudo apt-get install phpmyadminThis isn’t required since we’ll be using a different database to load our geodata into. When you’re done configuring everything, restart apache with:
sudo apachectl restartNow we have to configure the server’s hostname so we can contact it via a URL instead of the IP address. If you enter:
hostname -fyou should see the server name you entered when you setup the server. We want to turn that into a subdomain for our primary domain (for me it will be tiles.axismaps.com) but you can skip this if you don’t have your own domain (you’ll just keep using your IP address to connect to the server). To tell the server what the rest of the domain is, we’ll need to edit a few text files using nano, the built-in terminal text editor.
sudo nano /etc/hostnameWill show you something like:
tilesYou’ll want to change that to:
tiles..comWe are using tiles.axismaps.com for ours. When you’ve edited the file, hit Ctrl-X (not command if you’re on a Mac) then enter Y to save changes and hit Enter to overwrite the file. Do the same thing and add your domain to the second entry in:
sudo nano /etc/hostsTo make the changes stick, reboot your server with:
sudo rebootThe final step is to add a DNS record with your web host that points back to the server. From your server management page in Rackspace, click the DNS tab then click your domain name. Click “Add” to enter a new record with values:
- Type: A
- Name: tiles..com
- TTL: 3600
ssh root@(or just hit the up key to access your last command) and remember to use your new password. Now we’ll setup the firewall to only allow certain types of connections to the server. To allow established connections to continue to function:
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPTTo allow SSH traffic:
iptables -A INPUT -p tcp --dport 22 -j ACCEPTTo allow HTTP traffic:
iptables -A INPUT -p tcp --dport 80 -j ACCEPTTo allow HTTPS traffic:
iptables -A INPUT -p tcp --dport 443 -j ACCEPTTo allow remote database connections:
iptables -A INPUT -p tcp --dport 5432 -j ACCEPTDrop all remaining traffic:
iptables -A INPUT -j DROPSave all the rules to a file:
sudo iptables-save > /etc/iptables.rulesTo enable the firewall, we need to add the rules to the network adapter by editing the interfaces file:
sudo nano /etc/network/interfacesand add the line:
pre-up iptables-restore < /etc/iptables.rulesjust after iface eth0 inet static and make sure it is indented like the other lines. Save and exit the file then reboot the server. This is a good time to create a server backup. In Rackspace, click on “Images” then click “New On-Demand Image” to create a backup of your server. This way, if something goes wrong, you can be up and running again quickly.
Installing PostGISSome parts of this tutorial is taken from OpenStreetMap Wiki To give us access to all the software we’ll need, we need to add an additional software repository. First, enter:
sudo apt-get install python-software-propertiesto install the command that will allow us to add new repositories. Then we can add a GIS-specific software repository by doing:
sudo add-apt-repository ppa:ubuntugis/ubuntugis-unstableNow we need to update the system to allow it to pull down the available software from the new repository:
sudo aptitude updateWe should be ready to install all of the PostGIS packages with:
sudo apt-get install postgresql-8.4-postgis postgresql-contrib-8.4It will warn you about an old version of PostgreSQL but don’t worry about it. Now we need to setup PostGIS to make the newly installed database ready for GIS. Swtich to the database user:
sudo -u postgres -i -HNow we will create a user within the database that can access your maps:
createuser -SdRP gisuserEnter a password for connecting to the database (it should be different from your root password). Now we will create and configure a database to hold your spatial data:
createdb -E UTF8 -O gisuser gis
createlang plpgsql gis
psql -d gis -f /usr/share/postgresql/8.4/contrib/_int.sql
psql -d gis -f /usr/share/postgresql/8.4/contrib/postgis-1.5/postgis.sql
psql -d gis -f /usr/share/postgresql/8.4/contrib/postgis-1.5/spatial_ref_sys.sql
psql gis -c "ALTER TABLE geometry_columns OWNER TO gisuser"
psql gis -c "ALTER TABLE spatial_ref_sys OWNER TO gisuser"
exitNow we need to configure access to our database first by editing the access file:
sudo nano /etc/postgresql/8.4/main/pg_hba.confChange the words ident and md5 to “trust” (there should be 3). If you want to connect to this database remotely (to view your data in an external manager or view it in QGIS) you should add the line:
# Enable remote connections:
host all all 0.0.0.0/0 md5to the bottom of the file and then save and close. You’ll also need to enable remote listening by editing the main configuration file here:
sudo nano /etc/postgresql/8.4/main/postgresql.confand change the line:
#listen_addresses = 'localhost'to
listen_addresses = '*'(don’t forget to remove the “#” in front). Save and overwrite the file. To apply the changes, restart the database server:
sudo /etc/init.d/postgresql reloadTo test if everything has been installed properly, log into the database as the new user we created.
psql gis gisuserIf you type d you should be able to see all 3 tables. Then type q to return.
Installing MapnikCopied exactly from the Mapnik Wiki To install Mapnik, enter:
sudo apt-get install libmapnik0.7 mapnik-utils python-mapnikThat’s it!
Installing TileStacheSome parts of this tutorial is taken from TileStache on GitHub The first step in installing TileStache is to install mod_python which is the interface TileStache will use to communicate with the web server. You can install it with:
sudo apt-get install libapache2-mod-pythonThen restart your web server with:
sudo /etc/init.d/apache2 restartNow we need to install some more packages that TileStache depends on. First we’ll switch to the directory where we’ll keep the new applications:
cd /etcInstall packages Curl and Git via aptitude to help with the install:
sudo apt-get install curl
sudo apt-get install git-coreNow install some python tools and libraries that are required:
sudo apt-get install python-setuptools
sudo aptitude install python-dev
sudo apt-get install libjpeg8 libjpeg62-dev libfreetype6 libfreetype6-devWe’ll grab and install PIP to easily install python modules:
curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py
sudo python get-pip.pyNow install the required python modules
sudo pip install -U werkzeug
sudo pip install -U simplejson
sudo pip install -U modestmapsThe Python Image Library module has some quirks in Ubuntu 11.10 so we need to do some quick fixes:
sudo ln -s /usr/lib/x86_64-linux-gnu/libjpeg.so /usr/lib
sudo ln -s /usr/lib/x86_64-linux-gnu/libfreetype.so /usr/lib
sudo ln -s /usr/lib/x86_64-linux-gnu/libz.so /usr/libBefore we can install it:
sudo pip install -U pilFinally we’ll download TileStache from GitHub:
git clone https://github.com/migurski/TileStache.gitAnd install it globally by running the install script:
python setup.py installFinally, we’ll have to add the mod_python configuration to tell our web server which URLs to have TileStache process. Start by editing the apache configuration file:
sudo nano /etc/apache2/httpd.confand add this:
AddHandler mod_python .py
PythonOption config /etc/TileStache/tilestache.cfgThis will direct any web traffic to the “tiles” folder containing the file extension “.py” to TileStache. We just need to add a tiles directory to the web directory so we don’t get an error:
mkdir /var/www/tilesReboot your server to finish it off:
Testing Your ServerOnce your server reboots, we can test to make sure TileStache is installed correctly and is running through mod_python and receiving maps from Mapnik. In your browser, go to: http://tiles..com/tiles/tiles.py/osm/preview.html You should see a OSM tiled map fullscreen, confirming TileStache is correctly installed and running. When you work with TileStache, you can always preview your tilesets at …/tiles/tiles.py//preview.html Now go to: http://tiles..com/tiles/tiles.py/example/preview.html You should see a simple gray country map which confirms that TileStache is talking to Mapnik, rendering a shapefile stored in the TileStache directory.
FinishedNow that everything is installed, you can go nuts with TileStache, Mapnik and PostGIS to render your own tiled maps. We’ll come back to this point in the coming weeks to show examples of how you can actually use these tools to make some maps. Some sample topics might include:
- Thematic cartography with TileStache and Mapnik
- Combining raster and vector tiles to optimize mapping for iPad
- Custom map tiles from OSM data