About this post

ABOUT: This entry was posted April 29, 2007 at 6 p.m. It is 1198 words long, which, in case you're curious, translates to about 34 inches. There are currently 1 comments on this post. Click here to add your own.

SUMMARY: In which I explain how to install MapServer on a CentOS/Red Hat machine.

TAGS: GIS


Spread the love


Recent posts

Sunday, September 28th, 2008
In which I explain the advantages of using a popular fraud-detection tool in reporting.

Monday, March 17th, 2008
In which I describe how to use MySQL's spatial functions and Python to do point-in-polygon detection.

Sunday, July 1st, 2007
Some tidbits I've collected over the last month and a half.

Saturday, May 5th, 2007
Backing up your databases is easy with S3, boto and mysqldump.

Monday, April 30th, 2007
In which I plead with you to learn from our deployment mistakes.

Open source GIS with MapServer, part two: Installation

Posted Sunday, April 29th, 2007 at 6 p.m.

Last time, I promised to lay out how to install MapServer on a production-ready Linux machine.

The instructions below are designed for a CentOS or Red Hat Enterprise Linux machine and are modified slightly from this document, which saved my life when I tried to install MapServer cold. It’s my understanding that Debian and Ubuntu have MapServer packages available through apt-get, synaptic or some such. I don’t know to what extent they take the edge off, but they might be worth checking out.

Also, Matt asked whether I’d explain how to do this under shared hosting. The answer is yes, but later. For now, you’ll need sudo or root access to a VPS or some other sandbox machine. It will also help if you have Yum installed and you’re comfortable with the standard ./configure, make, make install dance. If you’ve got a handle on those things, let’s get started:

Step 1: Cut a hole in a box …

The first thing you should do is su to root, so you don’t have to precede all of your commands with sudo:

su -

MapServer asks for a lot of dependencies, some critical and some not. We’re going to throw a few of the most useful ones in for good measure, but first we need to get some requirements out of the way. If you don’t already have Yum installed, download it and get it working. This is an older version, but one I know works:

wget yum-2.0.3-0.fdr.1.rh90.noarch.rpm
rpm -i yum*.noarch.rpm


You’ll be thankful you did that, I promise. Yum is a Python-based RPM package manager (we’re using an RPM-friendly distro, remember) that automates the installation and removal of hundreds of useful packages, including some of the ones you’ll need for MapServer. It fetches these packages out of repositories – some official and others not. You’ll need to enable one of the extra repositories like so:

vi /etc/yum.repos.d/CentOS-Base.repo
Find [CentOSplus]
Change "enabled=0" to "enabled=1"


Now let’s get some basic packages out of the way.

yum install \
httpd \
httpd-devel \
byacc \
flex \
php \
php-devel \
postgresql \
postgresql-contrib \
postgresql-server \
postgresql-devel \
python \
python-devel \
gcc-c++ \
curl \
curl-devel \
gd \
gd-devel \
php-gd \
php-dbase


If Yum throws an error related to reading or finding CentOSplus repositories, do this:

vi /etc/yum.repos.d/CentOS-Base.repo
Find [CentOSplus]
Comment out baseurl=whatever_url by placing a # in front of it
Add this line:
mirrorlist=http://mirror.centos.org/centos/$releaseserver/centosplus/$basearch/


Some of that stuff, like PHP, PostgreSQL, etc. you probably recognize, if you don’t have it installed already. A few of the lesser-known packages, like gd, are graphics-related. Curl is similar to wget and can be used by MapServer itself. Suffice to say that this stuff is important. At this point, take note of the version of PHP you’re using – it’ll be important later.

Now we’ll set up PostgreSQL – a database server similar to MySQL – and its amazing PostGIS extension. MapServer can handle shapefiles as its geospatial data source, but you never know when PostGIS data might come in handy. First, we'll do some inital setup:

mkdir /usr/local/pgsql/
mkdir /usr/local/pgsql/data/
chown postgres /usr/local/pgsql/data/
su - postgres
initdb -D /usr/local/pgsql/data/
pg_ctl -D /usr/local/pgsql/data -l
createdb test
psql test
\q
exit
/sbin/chkconfig --add postgresq


Now to install PostGIS:

wget http://postgis.refractions.net/download/postgis-1.1.1.tar.gz
tar -xzvf postgis-1.1.1.tar.gz
cd postgis-1.1.1
./configure
make
make install


And set it up:

/sbin/ldconfig
cp -r postgis-1.1.1 /usr/local
su - postgres
createlang plpgsql test
psql -d test -f /usr/local/postgis-1.1.1/lwpostgis.sql
psql -d test -f /usr/local/postgis-1.1.1/spatial_ref_sys.sql
exit


Now to compile a few more widgets. First, we’ll start with proj4, which helps MapServer handle various projection types.

wget ftp://ftp.remotesensing.org/proj/proj-4.4.9.tar.gz
tar -xzvf proj-4.4.9.tar.gz
cd proj-4.4.9
./configure
make
make install


Just a couple more to go, starting with GDAL, which helps MapServer render raster data:

wget http://www.gdal.org/dl/gdal-1.3.1.tar.gz
tar -xzvf gdal-1.3.1.tar.gz
cd gdal-1.3.1
./configure
make
make install
cp /usr/local/lib/libgdal.so.1 /usr/lib


One more: The GEOS geometry library:

wget http://geos.refractions.net/geos-2.2.1.tar.bz2
bunzip geos-2.2.1.tar.bz2
tar -xzvf geos-2.2.1.tar
cd geos-2.2.1
./configure
make
make install


Installing MapServer

If you made it through the dependencies, congratulations – that was the hard part. MapServer itself is pretty easy to install, so long as you did everything else correctly. Now might be a good time to be sure all your services are running properly. You should also run ldconfig one last time to be sure the server knows where everything is:

/sbin/ldconfig

Now, let’s build. Notice that you’ll be compiling a lot of your dependencies into the software, hence the flags:

wget http://cvs.gis.umn.edu/dist/mapserver-4.8.3.tar.gz
tar -xzvf mapserver-4.8.3.tar.gz
cd mapserver-4.8.3
./configure \
--with-proj \
--with-geos \
--with-ogr \
--with-gdal \
--with-postgis \
--with-curl-config \
--with-httpd=/usr/sbin/httpd \
--with-php=/usr/include/php
make
make install


The install process is a little different from the others you’ve just done. If you run a quick ls, you’ll notice a few new executable files located in your MapServer directory. These are actually the products of your make install, and they need to be copied into your Web server’s cgi-bin so you can access them via URL:

cp mapserv /var/www/cgi-bin
cp legend /var/www/cgi-bin
cp scalebar /var/www/cgi-bin
cp shp2img /var/www/cgi-bin
cp shp2pdf /var/www/cgi-bin
cp shptree /var/www/cgi-bin
cp shptreest /var/www/cgi-bin
cp shptreevis /var/www/cgi-bin
cp sortshp /var/www/cgi-bin
cp tile4ms /var/www/cgi-bin


The last thing (for now) is to copy over some PHP MapScript modules, which allow access to many of MapServer's functions and classes from within PHP. Remember earlier when I said to take note of the PHP version you installed? Here's where that becomes important. If you're using PHP 3 or 4, do this:

cd mapscript/php3
cp php_mapscript.so /usr/lib/php/modules/


Or if you installed PHP 5:

cd mapscript/php3
cp php_mapscript /usr/lib64/php/modules/


Until next time …

To test whether your setup is working, create a new file called phpinfo.php and place it in your Web document root (/var/www/html if you haven't changed it). Paste this code into the file (minus the leading slash):

<\? dl("php_mapscript.so"); phpinfo(); ?>

Next navigate to www.yourdomain.com/phpinfo.php. Search the resulting page for a MapServer entry. If you find it, you're good to go. If you don't, drop me an e-mail or leave me a comment and I'll try to help.

You should now have a working version of MapServer installed in your cgi-bin, along with MapScript for good measure. Next time, I’ll explain how to use what we just installed to build a simple static map, which we will eventually expand with AJAX.

Until then, play around with these docs if you want to jump ahead.

Comments | Post yours

  1. Qifan Xi 10:35 a.m. on July 3

    under “Installing MapServer”, I believe one of the lines should read “cp shptreest /var/www/cgi-bin”, not “cp shptreest /var/www/cgi-bin”.

    Unless, of course, the name of the compiled binary has changed from 4.8.3 to 4.10.2

Post your comment

Optional