Debian / Ubuntu Linux Apache mod_python Configuration

Here are the steps to configure mod_python on Apache web server on Debian or Ubuntu Linux:

  1. Install Apache web server and mod_python package:
    sudo apt-get install apache2 libapache2-mod-python
  2. Enable mod_python module:
    sudo a2enmod python
  3. Restart Apache web server to apply changes:
    sudo systemctl restart apache2
  4. Create a new directory for your mod_python application in /var/www:
    sudo mkdir /var/www/myapp
  5. Create a new Python script for your mod_python application, for example /var/www/myapp/hello.py. In this script, define a function called handler that takes two arguments, req and res. This function will be called by mod_python when a request is received:
    def handler(req, res):
    res.content_type = 'text/plain'
    res.write('Hello, world!\n')
    return apache.OK
  6. Configure Apache to handle requests to your mod_python application. Edit the Apache configuration file /etc/apache2/sites-available/000-default.conf and add the following lines:
    <Directory /var/www/myapp>
    AddHandler mod_python .py
    PythonHandler mod_python.publisher
    PythonDebug On
    </Directory>

    The AddHandler directive tells Apache to use mod_python to handle requests with the file extension .py. The PythonHandler directive tells mod_python to use the mod_python.publisher handler to process requests to your application. The PythonDebug directive enables debugging mode for your application.

  7. Reload Apache configuration to apply changes:
    sudo systemctl reload apache2
  8. Test your mod_python application by visiting http://localhost/myapp/hello.py in your web browser. You should see the message “Hello, world!”.

That’s it! You have successfully configured mod_python on Apache web server on Debian or Ubuntu Linux. Note that mod_python has been deprecated and is no longer actively maintained. It’s recommended to use a different technology, such as mod_wsgi, to run Python applications on Apache.

Leave a Comment