Python learnings #2 - apache mod-python
This tells you how to install mod_python so that you can code for the web in python, in other words, stay in one language for the backend and frontend.
$ sudo apt-get update
$ sudo apt-get install libapache2-mod-python libapache2-mod-python-doc
Configure mod_python
You need to create directory to host your python scripts assuming that your root html directory is /var/www. Type the following command:
$ sudo mkdir /var/www/py $ sudo chown yourname:www-data /var/www/py
Now, open /etc/apache2/sites-available/default and add following code:
AddHandler mod_python .py PythonHandler hello PythonDebug On
Note that PythonHandler is hello which is hello.py. Save and close the file. Restart Apache2 server.
/etc/init.d/apache2 restart
$ vi /var/www/py/hello.py
The following simple mod_python program serves as a good test:
import sys sys.path.append('/var/www/html')from mod_python import apache def handler(req): req.log_error('handler') req.content_type = 'text/html' req.send_http_header() req.write('Testing mod_python') req.write('Hello World!') req.write('') return apache.OKSave and close the file. Now fire a webbrowser and type the url:
http://localhost/py/hello.py