Web www.darandandunguen.org

HOWTO: MapServer 5 and PostGIS in RHEL5

February 1st, 2008 es

The goal of this document is to install Mapserver5 and PostGIS in RHEL5. This document is based upon René Viancos’ fantastic tutorial, Cómo compilar Mapserver y Postgis en Linux y no morir en el intento… Hacerlo. [Spanish]. René’s tutorial is aimed at the installation of Mapserver4 so I’ve written this article to update his.

UPDATE: I’ve also written a HOWTO on adding support for mrsid image format in MapServer5.

  • This first item is just for the sake of comfort, I don’t like to walk to our datacenter to change a CD/DVD, so I import the CD images using NFS and then loop mount them. So, first of all, we’ll mount the CD Images:
    for i in `seq 1 5`; do mount -t iso9660 -o loop /RedHatCDs/rhel-5-server-i386-disc$i.iso /mnt/cd$i; done
  • WEB SERVER

    We need a web server with PHP and freetds support. (FreeTDS is needed to access Microsoft SQLServer or Sybase databases). We’ll start with apache rpm’s and some dependencies:

    rpm -i /mnt/cd2/Server/unixODBC-2.2.11-7.1.i386.rpm
    rpm -i /mnt/cd2/Server/mx-2.0.6-2.2.2.i386.rpm
    rpm -i /mnt/cd2/Server/httpd-2.2.3-6.el5.i386.rpm
    rpm -i /mnt/cd2/Server/unixODBC-devel-2.2.11-7.1.i386.rpm

    Now we are ready to compile freetds from source. We must rename the tgz package as freetds-version.tar.gz else rpmbuild won’t work.

    cd
    wget http://ibiblio.org/pub/Linux/ALPHA/freetds/stable/freetds-stable.tgz
    mv freetds-stable.tgz freetds-0.64.tar.gz
    rpmbuild -ta freetds-0.64.tar.gz
    rpm -i /usr/src/redhat/RPMS/i386/freetds-0.64-1.i386.rpm
    rpm -i /usr/src/redhat/RPMS/i386/freetds-devel-0.64-1.i386.rpm

    rpm -i /usr/src/redhat/RPMS/i386/freetds-unixodbc-0.64-1.i386.rpm
    rpm -i /usr/src/redhat/RPMS/i386/freetds-devel-0.64-1.i386.rpm

    Now we’ll simply install PHP packages.

    rpm -i /mnt/cd2/Server/php-cli-5.1.6-5.el5.i386.rpm \
    /mnt/cd2/Server/php-common-5.1.6-5.el5.i386.rpm \
    /mnt/cd2/Server/php-5.1.6-5.el5.i386.rpm \
    /mnt/cd3/Server/php-gd-5.1.6-5.el5.i386.rpm \
    /mnt/cd3/Server/php-pgsql-5.1.6-5.el5.i386.rpm \
    /mnt/cd2/Server/php-odbc-5.1.6-5.el5.i386.rpm \
    /mnt/cd2/Server/php-pdo-5.1.6-5.el5.i386.rpm

    Some PHP parameters have to be tweaked in /etc/php.ini:

    max_execution_time = 150
    max_input_time = 200
    memory_limit = 256M
    error_reporting = E_ALL & ~E_NOTICE
    display_errors = On ; To debug errors
    register_long_arrays = On
    extension_dir = “/usr/lib/php/modules”

    /etc/httpd/conf/httpd.conf also needs some slight changes. First one is under dir_module section

    <IfModule dir_module>
    DirectoryIndex index.php index.html
    </if module>

    Second one is in AddType block, we’ll add php extensions there:

    AppType application/x-httpd-php .php .phtml
    AddType application/x-httpd-php-source .phps

    Now we’ll test the server.

    /etc/init.d/httpd start

    Any browser should be able to show Apache’s start page.

    lynx http://localhost

    To test php, we’ll write an ultra tiny page and browse there.

    echo "<?php phpinfo (); ?>" > /var/www/html/info.php
    lynx http://localhost/info.php

    All PHP options and Apache should be on that page. If everything is OK, it’s time to add apache to autostart scripts.

    chkconfig httpd on

    Our web server is ready for the fiesta ;) It’s time to move on and work on the database backend.

  • POSTGRES

    We’ll work from source here, so we have to download, configure, make and install. Easy stuff!

    cd
    wget http://ftp5.es.postgresql.org/mirror/postgresql//source/v8.2.6/postgresql-8.2.6.tar.bz2
    tar xjvf postgresql-8.2.6.tar.bz2 -C /usr/local/
    cd /usr/local/postgresql-8.2.6/
    LDFLAGS=-lstdc++ ./configure \
    --prefix=/usr/local/pgsql \
    --with-perl \
    --with-python \
    --with-krb5 \
    --with-openssl
    make
    make install

    We still have to configure everything.

    First of all, we create the data directory.

    mkdir /usr/local/pgsql/data

    This directory must be owned by user postgres and group postgres, so we need them both:

    groupadd -r postgres
    adduser postgres -g postgres
    chown postgres:postgres /usr/local/pgsql/data/

    We should use user postgres everytime we use the database. It’s a good time to set a password for it.

    passwd postgres
    su - postgres
    /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data/ -U postgres -W
    /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data/ -l /usr/local/pgsql/data/logfile
    exit

    If everything works, we also have to write an script for the autostart of the server in/etc/init.d/postgresql:


    #!/bin/sh
    # postgresql This is the init script for starting up the
    # PostgreSQL server
    # chkconfig: - 85 15
    # description: Starts and stops the PostgreSQL backend daemon that handles all database requests.
    # processname: postmaster
    # pidfile: /usr/local/pgsql/data/postmaster.pid
    #
    # Source function library.
    . /etc/rc.d/init.d/functions
    # Get config.
    . /etc/sysconfig/network
    # Check that networking is up.
    # Pretty much need it for postmaster.
    [ ${NETWORKING} = "no" ] && exit 0
    [ -f /usr/local/pgsql/bin/postmaster ] || exit 0
    # See how we were called.
    case “$1″ in
    start)
    pid=`pidof pg_ctl`
    if [ $pid ]
    then
    echo “Postgres already running.”
    else
    echo -n “Starting postgresql service: ”
    su -l postgres -c ‘/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data/ -l /usr/local/pgsql/data/logfile start’
    sleep 1
    echo
    exit
    fi
    ;;
    stop)
    echo -n “Stopping postgresql service: ”
    su -l postgres -c ‘/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data/ stop’
    sleep 2
    rm -f /usr/local/pgsql/data/postmaster.pid
    echo
    ;;
    restart)
    $0 stop
    $0 start
    ;;
    status)
    su -l postgres -c ‘/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data/ status’
    ;;
    *)
    echo “Usage: postgresql {start|stop|restart|status}”
    exit 1
    esac
    exit 0

    These are the orders to write it an add to autostart:

    vi /etc/init.d/postgresql
    chmod 700 /etc/init.d/postgresql
    /etc/init.d/postgresql restart
    /etc/init.d/postgresql status
    chkconfig --add postgresql

  • POSTGIS

    We’ll need Proj library, but it’s pretty easy to build.

    wget ftp://ftp.remotesensing.org/proj/proj-4.5.0.tar.gz
    tar -xvzf proj-4.5.0.tar.gz -C /usr/local/
    cd /usr/local/proj-4.5.0/
    ./configure
    make
    make install

    GEOS library should be easy too.

    cd
    wget http://geos.refractions.net/downloads/geos-3.0.0.tar.bz2
    tar xjvf geos-3.0.0.tar.bz2 -C /usr/local/
    cd /usr/local/geos-3.0.0/
    ./configure
    make
    make install

    PostGIS compilation won’t start the problems yet :P

    cd
    wget http://www.postgis.org/download/postgis-1.3.2.tar.gz
    tar xvzf postgis-1.3.2.tar.gz -C /usr/local/postgresql-8.2.6/contrib/
    cd /usr/local/postgresql-8.2.6/contrib/postgis-1.3.2/
    ./configure --with-pgsql=/usr/local/pgsql/bin/pg_config
    make
    make install

    Installation deserves some more talking. First we need to add one line to /etc/ld.so.conf so the libraries are usable in exec time.

    echo "/usr/local/lib" >> /etc/ld.so.conf

    Binaries are in /usr/local/pgsql/bin, it may be worth to add this to th path.

    Libraries are in /usr/local/pgsql/lib, so we add this to /etc/ld.so.conf too.
    echo "/usr/local/pgsql/lib" >> /etc/ld.so.conf
    rm /usr/local/pgsql/lib/libpq.so.5
    ln -s /usr/local/pgsql/lib/libpq.so.5.0 /usr/local/pgsql/lib/libpq.so.5
    ldconfig

    This ends PostGIS Installation. You can create a test db and add the extensions as a test:

    /usr/local/pgsql/bin/createdb --encoding latin1 test -u
    /usr/local/pgsql/bin/createlang plpgsql test -u
    /usr/local/pgsql/bin/psql -d test -f /usr/local/pgsql/share/lwpostgis.sql -u
    /usr/local/pgsql/bin/psql -d test -f /usr/local/pgsql/share/spatial_ref_sys.sql -u

    Last two orders should end with COMMIT and VACUUM respectively, if all went as expected.

  • MAPSERVER

    Now, all the infraestructure is in place so it’s time to start with the libraries that MapServer will use.

    GDAL permits to use most common GIS formats.

    cd
    wget http://download.osgeo.org/gdal/gdal-1.5.0.tar.gz
    tar xvzf gdal-1.5.0.tar.gz -C /usr/local/
    cd /usr/local/gdal-1.5.0/
    ./configure --with-png --with-libtiff --with-jpeg --with-gif --with-pg=/usr/local/pgsql/bin/pg_config --with-geos --with-odbc
    make
    make install
    ldconfig

    Every other dependency has a rpm package. One exception, AGG also has an rpm but MapServer won’t build with that version, we’ll build from source that one.

    rpm -i /mnt/cd3/Server/php-devel-5.1.6-5.el5.i386.rpm
    rpm -i /mnt/cd1/Server/gd-2.0.33-9.3.fc6.i386.rpm
    rpm -i /mnt/cd2/Server/gd-devel-2.0.33-9.3.fc6.i386.rpm \
    /mnt/cd2/Server/fontconfig-devel-2.4.1-6.el5.i386.rpm \
    /mnt/cd2/Server/freetype-devel-2.2.1-16.el5.i386.rpm \
    /mnt/cd2/Server/libX11-devel-1.0.3-8.el5.i386.rpm \
    /mnt/cd2/Server/libXpm-devel-3.5.5-3.i386.rpm \
    /mnt/cd2/Server/libjpeg-devel-6b-37.i386.rpm \
    /mnt/cd2/Server/libpng-devel-1.2.10-7.i386.rpm \
    /mnt/cd2/Server/libXau-devel-1.0.1-3.1.i386.rpm \
    /mnt/cd2/Server/libXdmcp-devel-1.0.1-2.1.i386.rpm \
    /mnt/cd2/Server/xorg-x11-proto-devel-7.1-9.fc6.i386.rpm \
    /mnt/cd2/Server/mesa-libGL-devel-6.5.1-7.2.el5.i386.rpm
    rpm -i /mnt/cd2/Server/SDL-1.2.10-8.el5.i386.rpm
    rpm -i /mnt/cd2/Server/SDL-devel-1.2.10-8.el5.i386.rpm \
    /mnt/cd2/Server/alsa-lib-devel-1.0.12-3.el5.i386.rpm \
    /mnt/cd2/Server/mesa-libGLU-devel-6.5.1-7.2.el5.i386.rpm \
    /mnt/cd2/Server/libXext-devel-1.0.1-2.1.i386.rpm \
    /mnt/cd2/Server/libXrandr-devel-1.1.1-3.1.i386.rpm \
    /mnt/cd2/Server/libXrender-devel-0.9.1-3.1.i386.rpm \
    /mnt/cd2/Server/libXt-devel-1.0.2-3.1.fc6.i386.rpm \
    /mnt/cd2/Server/libSM-devel-1.0.1-3.1.i386.rpm \
    /mnt/cd2/Server/libICE-devel-1.0.1-2.1.i386.rpm
    wget http://www.antigrain.com/agg-2.5.tar.gz
    tar xzvf agg-2.5.tar.gz -C /usr/local/
    cd /usr/local/agg-2.5/
    aclocal
    autoheader
    autoconf
    libtoolize --force
    automake --foreign --add-missing --ignore-deps
    ./configure
    make
    make install

    Almost done. Before compiling, one detail, max number of symbol objects in a dataset is defined in mapsymbol.h (/usr/local/mapserver-5.0.0/mapsymbol.h after untaring) in line:

    #define MS_SYMBOL_ALLOCSIZE 64 /* number of symbolObj ptrs to allocate for a symbolset at once */

    If you use more than 64 symbols (I do), change it before compiling.


    cd
    wget http://download.osgeo.org/mapserver/mapserver-5.0.0.tar.gz
    tar xvzf mapserver-5.0.0.tar.gz -C /usr/local/
    cd /usr/local/mapserver-5.0.0/
    ./configure \
    --with-proj=/usr/local/proj-4.5.0 \
    --with-geos=/usr/local/bin/geos-config \
    --with-ogr=/usr/local/bin/gdal-config \
    --with-gdal=/usr/local/bin/gdal-config \
    --with-postgis=/usr/local/pgsql/bin/pg_config \
    --with-curl-config=/usr/bin/curl-config \
    --with-httpd=/usr/sbin/httpd \
    --with-php=/usr/include/php \
    --with-wfs \
    --with-wfsclient \
    --with-wmsclient \
    --enable-debug \
    --with-threads \
    --with-wcs \
    --with-wcsclient \
    --with-sos \
    --with-gd \
    --with-freetype \
    --with-jpeg \
    --with-agg=/usr/local/
    make clean
    make

    Installation is made ‘by hand’:


    echo "cp mapserv legend scalebar shp2img shptree shptreetst shptreevis sortshp tile4ms /var/www/cgi-bin/"> install.sh
    echo "./mapserv -v" >> install.sh
    echo "service httpd reload" >> install.sh
    chmod +x install.sh
    ./install.sh

    To test it:

    ./mapserv -v

    The server will answer with its installled features. Then copy the PHP module to it’s place and reload Apache.

    cp /usr/local/mapserver-5.0.0/mapscript/php3/php_mapscript.so /usr/lib/php/modules/
    service httpd reload

    Done!!!.

  • DEMO

    If you want to test your brand new server, you can use Itasca demo:


    cd
    wget http://maps.dnr.state.mn.us/mapserver_demos/workshop-5.0.zip
    unzip workshop-5.0.zip
    mv workshop-5.0 /var/www/html/
    chown apache:apache /var/www/*
    chown -R apache:apache /var/www/html/workshop-5.0/
    mkdir /var/www/html/tmp
    chmod 777 /var/www/html/tmp

    Before toying with it, change these lines in /var/www/html/workshop-5.0/index.html:

    // EDIT THE NEXT 2 LINES TO MATCH YOUR SETUP
    var snippet = “IMAGEPATH ‘/var/www/html/tmp/’”;
    snippet += ” IMAGEURL ‘/tmp/’”;

    Browse to http://localhost/workshop-5.0/index.html and enjoy!

  • THE END
  • Meneame
  • Facebook
  • del.icio.us
  • BarraPunto
  • Slashdot
  • Technorati
  • Digg
  • e-mail
  • TwitThis
  • BlogMemes Sp
  • Reddit
menéame

6 Responses to “HOWTO: MapServer 5 and PostGIS in RHEL5”

  1. Rene Viancos Says:

    hola, podrías agregar como dar soporte para imagenes ECW de ERMapper en mapserver, ya que no solo se podrían utilizar estas imágenes como rasters, sino que se podrían utilizar las características WCS para descargar trozos de la misma imagen en formato nativo ECW (es una teoría). Saludos desde Chile. Rene Viancos

  2. manu Says:

    Hola René, no tengo imágenes en formato ECW para probar, pero próximamente voy a hacer otro proyecto con MapServer, así que probablemente, tendré más información para añadir aquí.

  3. Carlos Otero Says:

    hola que tal, gracias al manual de Rene Viancos, pude instalar mapserver sobre centos 5 ya hace vario meses atras, antes de eso era un dolor de cabeza.

    Aplicare la actualización del manual.

    Gracias a ambos por poner el conocimiento a disposición de la gente.

  4. Paulo Cesar Says:

    Hola a Todos.

    Gracias por el manual, muy bueno y efectivo.

    Asi deben ser los manuales. Cada pasa que se dice funciona.

    Gracias por ayudarnos a aprender un poco mas cada dia.

  5. Alejo Says:

    Muy bien explicado, muy bien los pasos, salvo por 2 paquetitos nomas EXELENTE el Howto. Espero que sigas ayudando a la gran comunidad de linux, un saludo. Y muchas gracias por la potente ayuda

  6. manu Says:

    Me alegro de que os haya ayudado el manual :)

Leave a Reply