Deploying MapProxy on CentOS5 x86_64 using apache2 with mod_fastcgi or mod_fcgid
This is a detailed HowTo on deploying a MapProxy instance on Enterprise Linux (CentOS 5.5) for production. I worked this out for the deployment of the CRC806-Database MapProxy instance. It will describe a setup using Apache with FastCGI (mod_fastcgi or mod_fcgid) on a blank CentOS 5.5 amd64, which is also described shortly and more generally (focused on debian/ubuntu) in the MapProxy documentation.
It will be described how to do the setup using the mod_fastcgi and also for using the mod_fcgid as an alternative. The reason for this two different setups is, that I first worked it out for mod_fastcgi, but it turned out that I can't apply this procedure on the production servers of our university, because I was not allowed to compile the module from source as described in this HowTo. This forced me to work it out using the officially packaged mod_fcgid also.
In this HowTo every single step or command line entry is documented, so if you just follow every single step you should get it working like it is described here.
1. MapProxy installation
At first setup the MapProxy instance.
1.1 Install libraries needed for the MapProxy installation
# rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/x86_64/epel-release-5-4.noa... # yum install libffi # rpm -Uvh http://elgis.argeo.org/repos/5/elgis-release-5-5_0.noarch.rpm # yum install proj proj-epsg geos # rpm -Uvh http://yum.chrislea.com/centos/5/i386/chl-release-5-3.noarch.rpm # rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CHL # yum install make gcc gcc-c++ python26 python26-devel zlib-devel freetype freetype-devel libjpeg-devel
1.2 Setup the Python2.6 virtual environment
Create a directory in your file system where you want to deploy mapproxy into. For this tutorial I choosed '/var/local/mapproxy'.
# wget http://bitbucket.org/ianb/virtualenv/raw/1.5.1/virtualenv.py
Distribute the environment ot a location of your choise, I choosed '/var/local/mapproxy' as the base folder for the MapProxy setup in this tutorial.
# python26 virtualenv.py --distribute /var/local/mapproxy/venv
Start the virtual environment.
# source /var/local/mapproxy/venv/bin/activate
1.3 Install Python dependencies
# pip install pyproj # pip install Shapely # pip install flup
1.4 Install MapProxy
# pip install MapProxy
1.5 Configure MapProxy
# paster create -t mapproxy_conf /var/local/mapproxy
Now you can adjust the /var/local/mapproxy/etc/config.ini to your needs. But the defaults are just fine.
2. Setup Apache2 with mod_fastcgi
The setup using the mod_fcgid will be described in Section 4. This means, if you want to use mod_fcgid you can skip section 2 and 3 of this HowTo.
2.1 At first install the Apache2 HTTP Server
# yum install httpd chkconfig
Make the server boot at startup.
2.2 Install mod_fastcgi
I was not able to find an .rpm for mod_fastcgi for CentOS (amd64), so we need to compile and install it from source.
# yum -y install glibc-devel httpd-devel apr apr-devel libtool # cd /opt # wget http://www.fastcgi.com/dist/mod_fastcgi-current.tar.gz # tar -xvzf mod_fastcgi-current.tar.gz # cd mod_fastcgi-2.4.6 # cp Makefile.AP2 Makefile # make top_dir=/usr/lib64/httpd # make install top_dir=/usr/lib64/httpd
2.3 Create a mapproxy folder under the www-root dir
# mkdir /var/www/mapproxy
2.4 Edit the apache fastcgi.conf
# nano /etc/httpd/conf.d/fastcgi.conf
<IfModule mod_fastcgi.c> FastCGIExternalServer /var/www/mapproxy/sock -socket /var/www/mapproxy/var/fcgi-socket Alias /proxy /var/www/mapproxy/sock </IfModule> <Directory "/var/www/mapproxy"> Order allow,deny Allow from all </Directory>
3. Create an MapProxy initscript for automated startup at system boot.
3.1 first we need a script wrapping the startup command
# nano /var/local/mapproxy/mapproxy_start.sh
#!/bin/sh MAPPROXY_HOME=/var/www/mapproxy export MAPPROXY_USE_PYPROJ=1 source /var/local/mapproxy/venv/bin/activate paster serve --pid-file=$MAPPROXY_HOME/paster.pid $MAPPROXY_HOME/etc/config.ini start
3.2 second we need a script wrapping the stop command
# nano /var/local/mapproxy/mapproxy_stop.sh
#!/bin/sh MAPPROXY_HOME=/var/www/mapproxy export MAPPROXY_USE_PYPROJ=1 source /var/local/mapproxy/venv/bin/activate paster serve --pid-file=$MAPPROXY_HOME/paster.pid $MAPPROXY_HOME/etc/config.ini stop
3.3 install the initscript
# nano /etc/init.d/mapproxyd
#!/bin/bash # # /etc/rc.d/init.d/mapproxyd # # This is a Enterprise Linux initscript for starting # the MapProxy (http://mapproxy.org) daemon. # # The installtion of MapProxy for Enterprise Linux in a # production environment is describet at: # URL-To-Blog # Source function library. . /etc/init.d/functions RUN_AS_USER=mapproxy MAPPROXY_HOME=/var/www/mapproxy start() { echo -n "Starting MapProxy: " if [ "x$USER" != "x$RUN_AS_USER" ]; then su - $RUN_AS_USER -c "$MAPPROXY_HOME/mapproxy_start.sh" else $MAPPROXY_HOME/mapproxy_start.sh fi echo "Startup MapProxy done." } stop() { echo -n "Shutting down MapProxy: " if [ "x$USER" != "x$RUN_AS_USER" ]; then su - $RUN_AS_USER -c "$MAPPROXY_HOME/mapproxy_stop.sh" else $MAPPROXY_HOME/mapproxy_stop.sh fi echo "Stopping MapProxy done." } case "$1" in start) start ;; stop) stop ;; *) echo "Usage: mapproxyd {start|stop}" exit 1 ;; esac exit $?
3.3.1 Make MapProxy start on system boot
# chkconfig --levels 235 mapproxyd on
Thats it so far. After an reboot you should be able to access your MaProxy instance from http://www.yourdomain.com/proxy.
4. Apache2 setup using mod_fcgid
4.1 Install the mod_fcgid from the repository
# yum -y install mod_fcgid
4.2 Create an mapproxy.fcgi script
This Script will be called from the apache mod_fcgi to startup/prefork the MapProxy on request.
#!/var/local/mapproxy/venv/bin/python26
import os
from paste.deploy import loadapp
application = loadapp('config:config.ini', relative_to=os.path.dirname(__file__))
#Deploy using FastCGI
if __name__ == '__main__':
from flup.server.fcgi import WSGIServer
WSGIServer(application).run()Make sure that you point the Shebang (first line) of the script to your mapproxy python virtual environment.
4.3 Set appropriate access flags
Set the mapproxy.fcgi script to executable:
# chmod 755 mapproxy.fcgi
Give the Apache user acces to the Mapproxy (this can of course be done more fine grained...):
# chown apache /var/local/mapproxy/ -R
4.4 Configure Apache
Edit the '/etc/httpd/conf.d/fcgid.conf'
Just add the following line:
Just add the following line:
ScriptAlias /mapproxy /var/local/mapproxy/etc/mapproxy.fcgi/
After an Apache restart your MapProxy instance should be accessible from: http://<yourserver>/mapproxy/
Have fun!
