# mod_python

Introduction

This tutorial shows you how to easily publish your PyAMF applications with the Apache 2 webserver and mod_python. Mod_python is an Apache module that embeds the Python interpreter within the server. This was tested with Python 2.4.3 and Ubuntu 6.06.1 LTS.

This tutorial assumes you already installed the Apache webserver running (on 192.168.1.100). Flash applications will be able to access your PyAMF remoting gateway on http://192.168.1.100/flashservices/gateway.

## Download WSGI gateway for mod_python

Create a folder for your application:

    mkdir /var/www/myApp

Grab the WSGI gateway for mod_python and put it in your application folder:

    wget http://www.aminus.net/browser/modpython_gateway.py?format=raw
    mv modpython_gateway.py?format=raw /var/www/myApp/wsgi.py

## Create your PyAMF application

Create a startup file for your application in `/var/www/myApp/startup.py`:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19

    from pyamf.remoting.gateway.wsgi import WSGIGateway

    import logging

    logging.basicConfig(
        level=logging.DEBUG,
        format='%(asctime)s %(levelname)-5.5s [%(name)s] %(message)s'
    )

    def echo(data):
       return data

    services = {
       'echo': echo,
       # Add other exposed functions here
    }

    application = WSGIGateway(services, logger=logging, debug=True)

Make sure your Apache user (`www-data`) has access to your application files.

## Setup Apache virtual host

Create a new virtual host or modify an existing one:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26

    <VirtualHost 192.168.1.100:80>

            ServerName example.server.com
            DocumentRoot /var/www/myApp

            CustomLog /var/log/apache2/myApp-access.log combined
            ErrorLog /var/log/apache2/myApp-error.log

            LogLevel warn
            ServerSignature Off

            # PyAMF gateway
            <Location /flashservices/gateway>
                    SetHandler mod_python
                    PythonPath "['/var/www/myApp', '/usr/src/pyamf']+sys.path"
                    PythonHandler wsgi
                    PythonOption wsgi.application startup::application
                    PythonOption SCRIPT_NAME /flashservices/gateway

                    # Only enable the options below when you're debugging the
                    # application. Turn them Off in a production environment.
                    PythonDebug On
                    PythonAutoReload On
            </Location>

    </VirtualHost>

## Restart Apache

That’s it! Your Adobe Flash Player and AMF clients will now be able to access your PyAMF application through http://192.168.1.100/flashservices/gateway.

## Useful Resources

http://wiki.pylonshq.com/display/pylonscookbook/Production+deployment+using+mod_python
  Production deployment using mod_python (pylonshq).
http://www.aminus.net//wiki/ModPythonGateway
  ModPythonGateway.
http://www.modpython.org/live/mod_python-3.2.8/doc-html/dir-other-pp.html
  mod_python manual for `PYTHONPATH`.