Debian / Ubuntu Linux Apache mod_python Configuration
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 calledhandlerthat takes two arguments,reqandres. 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! ') return apache.OK - Configure Apache to handle requests to your mod_python application. Edit the Apache configuration file
/etc/apache2/sites-available/000-default.confand add the following lines:<Directory /var/www/myapp> AddHandler mod_python .pyrn PythonHandler mod_python.publisherrn PythonDebug Onrn</Directory>The
AddHandlerdirective tells Apache to use mod_python to handle requests with the file extension.py. ThePythonHandlerdirective tells mod_python to use themod_python.publisherhandler to process requests to your application. ThePythonDebugdirective 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.pyin your web browser. You should see the message "Hello, world!".
