Here are the steps to configure mod_python on Apache web server on Debian or Ubuntu Linux:
- Install Apache web server and mod_python package:
sudo apt-get install apache2 libapache2-mod-python
- Enable mod_python module:
sudo a2enmod python
- Restart Apache web server to apply changes:
sudo systemctl restart apache2
- Create a new directory for your mod_python application in
/var/www
:sudo mkdir /var/www/myapp
- Create a new Python script for your mod_python application, for example
/var/www/myapp/hello.py
. In this script, define a function calledhandler
that takes two arguments,req
andres
. 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
- 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
. ThePythonHandler
directive tells mod_python to use themod_python.publisher
handler to process requests to your application. ThePythonDebug
directive enables debugging mode for your application. - Reload Apache configuration to apply changes:
sudo systemctl reload apache2
- 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.