Compiling the stylesheet
We now have a fully working CartoCSS stylesheet. Before Mapnik can use it, we need to compile it into XML using the command-line carto compiler. First of all, we use OSM Bright’s own preprocessor, which we need to edit for our setup:
cp configure.py.sample configure.py gedit configure.py
Change the config line pointing to ~/Documents/Mapbox/project to /usr/local/share/maps/style instead, and change dbname from osm to gis. Save and exit.
Run the pre-processor and then carto:
./make.py cd ../OSMBright/ carto project.mml > OSMBright.xml
This will create a new folder called “build” with your new project, customized with the variables you set in
configure.py and install a copy of this build to your MapBox project folder. If you open up TileMill you should see your new map in the project listing.
Click on the map to view it in the editing interface.
You now have a Mapnik XML stylesheet at /usr/local/share/maps/style/OSMBright/OSMBright.xml .
Setting up your webserver
Next we need to plug renderd and mod_tile into the Apache webserver, ready to receive tile requests.
Change the the renderd settings by editing the /usr/local/etc/renderd.conf (you’ll need to do it as root via “sudo”) and change the following five lines, uncommenting (removing the ‘;’) when required. They are found in the [renderd], [mapnik] and [default] sections.
socketname=/var/run/renderd/renderd.sock plugins_dir=/usr/local/lib/mapnik/input font_dir=/usr/share/fonts/truetype/ttf-dejavu XML=/usr/local/share/maps/style/OSMBright/OSMBright.xml HOST=localhost
Create the files required for the mod_tile system to run (remember to change username to your user’s name):
sudo mkdir /var/run/renderd sudo chown username /var/run/renderd sudo mkdir /var/lib/mod_tile sudo chown username /var/lib/mod_tile
Next, we need to tell the Apache web server about our new mod_tile installation.
Using your favourite text editor, create the file /etc/apache2/conf-available/mod_tile.conf and add one line:
LoadModule tile_module /usr/lib/apache2/modules/mod_tile.so
Apache’s default website configuration file needs to be modified to include mod_tile settings. Modify the file /etc/apache2/sites-available/000-default.conf to include the following lines immediately after the admin e-mail address line:
LoadTileConfigFile /usr/local/etc/renderd.conf ModTileRenderdSocketName /var/run/renderd/renderd.sock # Timeout before giving up for a tile to be rendered ModTileRequestTimeout 0 # Timeout before giving up for a tile to be rendered that is otherwise missing ModTileMissingRequestTimeout 30
Tell Apache that you have added the new module, and restart it:
sudo a2enconf mod_tile sudo service apache2 reload
Tuning your system
A tile server can put a lot of load on hardware and software. The default settings may therefore not be appropriate and a significant improvement can potentially be achieved through tuning various parameters.
The default configuration for PostgreSQL 9.4 needs to be tuned for the amount of data you are about to add to it. Edit the file /etc/postgresql/9.4/main/postgresql.conf and make the following changes:
shared_buffers = 128MB checkpoint_segments = 20 maintenance_work_mem = 256MB autovacuum = off
These changes require a kernel configuration change, which needs to be applied every time that the computer is rebooted. As root via “sudo”, edit /etc/sysctl.conf and add these lines near the top after the other “kernel” definitions:
# Increase kernel shared memory segments - needed for large databases kernel.shmmax=268435456
Reboot your computer. Run this:
sudo sysctl kernel.shmmax
and verify that it displays as 268435456.